How to change table and column name in mongodb springboot application? - mongodb

I am trying to change the name of collection and corresponding columns in mongodb-springboot application.
My entity class is:
#Entity
#Table(name = "user_profile")
public class UserProfile implements Serializable {
#Id
#Column(name = "user_profile_id")
private Long userProfileId;
#Column(name = "name")
private String name;
}
My Repository class is:
#RepositoryRestResource(collectionResourceRel = "user_profile")
public interface UserProfileRepository extends MongoRepository<UserProfile, Long> {
...
}
But the collection created is userProfile instead of user_profile. Also, there is an _id filed instead of user_profile_id in the collection.
What am I doing wrong?

use #Document in place of #Entity in case of MongoDB.
use #Document(collection = "User_detail") to name table.

Related

Query for joins in Spring JPA

I have the below entities
#Entity
#Getter
#Setter
public class Aggregate {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#OneToMany(mappedBy = "aggregate")
private Set<Single> singleSet;
}
#Entity
#Getter
#Setter
public class Single {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private String id;
private Integer number;
#ManyToOne
#JoinColumn(name = "agg_id")
private Aggregate aggregate;
}
I also have the below repository
public interface AggregateRepo extends CrudRepository<Aggregate, Long> {
}
I want to return all associated Single records where number in object Single is equal to some random number
I am assuming that the query will be something like this
public interface AggregateRepo extends CrudRepository<Aggregate, Long> {
public List<Single> findBySingleSet_Number(Integer number);
}
However when I try to use Intellij to complete my named query it always populates like this
public interface AggregateRepo extends CrudRepository<Aggregate, Long> {
public List<Single> findBySingleSet_Empty_Number(Integer number);
}
I am wondering what the Empty stands for ?
Also should I create another Single repository since the query is related to returning Single records.

Effects of Apache Shiro's roles and permissions in existing Data Access Objects classes

I am using spring-boot-dependencies 1.3.5.RELEASE for my application and it runs on Java SE 1.8. I am using Apache Shiro' to mapusergroups inrolestouserpermissionswhereas I am usingDAO(Data Access Object`) for accessing data from database.
Let's say we have an entities such that
Employee "has-a" Department
Department "has-a" Domain
User "has-a" Domain
Entity Classes: Employee
#Entity
#Table(name = "EMPLOYEE")
#Data
#EqualsAndHashCode(of = "id", callSuper = false)
public class Employee {
#Id
#GeneratedValue
private int id;
private String name;
#ManyToOne
#JoinColumn(name="department_id")
private Department dept
}
Entity Classes: Department
#Entity
#Table(name="DEPARTMENT")
#Data
#EqualsAndHashCode(of = "id", callSuper = false)
public class Department {
#Id
#GeneratedValue
private int id;
private String name;
#ManyToOne
#JoinColumn(name="domain_id")
private Domain domain
#OneToMany(mappedBy="department")
private Set<Employee> employees;
}
Entity Classes: Domain
#Entity
#Table(name="DOMAIN")
#Data
#EqualsAndHashCode(of = "id", callSuper = false)
public class Domain{
#Id
#GeneratedValue
private int id;
private String name;
}
Now I would like to restrict User (a login user) to see only those Employees which are associated with the Departments whose Domain has an access to the User. Is there any way to achieve this without changing queries in DAO classes OR to do this with minimum code changes? Thank You.
Which Realm are you using? If you are using the JdbcRealm you should be able to set userRolesQuery to a query of your choice.

Override an #Embedded from a #MappedSuperclass

I'm working with a legacy database and have no DDL privileges.
I created a #MappedSuperclass that have #Embedded attributes:
#MappedSuperclass
public abstract class MyEntity {
#Embedded
private CreateInfo createInfo;
#Embedded
private UpdateInfo updateInfo;
}
Unfortunately, one table has a different column name for the above properties.
Is it possible to override the attributes on the actual Entity, if yes how?
Turns out that #AttributeOverride can be nested.
#Entity
#AttributeOverrides({
#AttributeOverride(name = "updateInfo.lastModifiedBy", column = #Column(name = "DIFF_NAME1"))
, #AttributeOverride(name = "updateInfo.lastModifiedDate", column = #Column(name = "DIFF_NAME2"))
})
public class Child extends MyEntity {...}

Table per concrete class with Hibernate OGM and mongodb

I'm using mongodb to store json documents, and since I'm using Hibernate ORM for my relational models I've decided to use the OGM for the mongo ones.
Currently all of my OGM entities share the same parent class, it looks something like:
#Entity
public abstract class Document {
private static final Gson GSON = new Gson();
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Type(type = "objectid")
protected String id;
public String id() {
return this.id;
}
#Override
public String toString() {
return Document.GSON.toJson(this);
}
}
#Entity
public class Address extends Document {
private String city;
private String street;
private int house;
}
#Entity
public class Person extends Document {
private String name;
#OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Address> addresses;
}
(simplified of course)
What I expected that would happen when I persist a Person instance is that two collections will be created in the db, one for Person and the other for Address, which I inferred:
The various inheritance strategies are not supported by Hibernate OGM,
only the table per concrete class strategy is used
(Supported entity mapping - Hibernate OGM documentation)
But what happens in reality is that only one collection is created with the name Document with two documents in it:
{
_id : id1,
DTYPE : Person,
name : name of person
}
{
_id : id2,
DTYPE : Address,
city : City of address,
street : Street of address
house : 3
}
What am I missing?
Thanks
I think, it should be:
#Entity
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Document {
...
}

Hibernate-search search from any indexed entity

I am using Hibernate-search for searching data in my Jboss application. I have 3 JPA entity classes that all extend BaseEntity class and each are indexed by Lucene. For example:
#MappedSuperclass
public abstract class BaseEntity implements Serializable {
#Temporal(TemporalType.TIMESTAMP)
private Date created;
public abstract Long getId();
}
#Entity
#Table(name = "DVD")
public class Dvd extends BaseEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Field
private String title;
}
#Entity
#Table(name = "BOOK")
public class Book extends BaseEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Field
private String author;
}
Now I would like to search for either DVD title or Book author by wildcard search query and get the result list as List. This is what I have this far:
public List<BaseEntity> search(String query, int firstResult, int maxResults) {
List<BaseEntity> results = null;
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
Query luceneQuery = new WildcardQuery(new Term("*", "*" + query + "*"));
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, BaseEntity.class);
fullTextQuery.setFirstResult(firstResult);
fullTextQuery.setMaxResults(maxResults);
results = fullTextQuery.getResultList();
return results;
}
But with this I am not getting any results. How is it possible to get this to work or is there even way without using buildQueryBuilder for each entity? Thanks!
You'll want to use the varargs-style method for the classes, like so:
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, DVD.class, Book.class);
This is because when Hibernate Search creates the search query, it adds the class name(s) to the query (for the _hibernate_class field, which is the indexed class' name).