JPA / Hibernate Join and Fetch single column

I am new to JPA / Hibernate. Suppose I have these two tables:

  • Employee (Id, Name, DeptId, ..) // DeptId is a foreign key.
  • Department (Id, DeptName, ..) // Department is stored separately

and Entities as shown below:

@Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @OneToOne(cascade = {CascadeType.PERSIST}) @JoinColumn(name="DEPT_ID") private Dept dept; ... } @Entity public class Dept { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; ... **other huge collections with eager fetch *** } 

In my DAO application, whenever I access the Employee entity, I just need the department name as part of the employee entity and nothing else from the department table.

  • How to get a department. the column name is ONLY, and not the whole line of the department, in the essence of the employee (should the desired selections for huge collections made by the department be avoided)? if so, what annotations should I use?
  • How to handle the cascade in this scenario?
+4
source share
2 answers

The best option is to make the assembly lazy loaded, and then change the queries in which the collection is needed for active loading (using connection interception). If you have a very good reason not to do this, you can try the following workarounds.

You can use projection query. This will result in an [employee, name] array for each result.

 select employee, employee.dept.name from Employee employee 

You can use @Formula to map an attribute in the Employee table to a column in the Department table (note that this solution applies to Hibernate)

 class Employee { @Formula("(select deptName from Department where Department.id = DEPT_ID)" String deptName; } 

Another option is to create a new DeptLite class that does not have a collection. Match it as readonly - @org.hibernate.annotations.Entity(mutable=false) .

 @Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @OneToOne(cascade = {CascadeType.PERSIST}) @JoinColumn(name="DEPT_ID") private Dept dept; @OneToOne(updatable=false,insertable=false) @JoinColumn(name="DEPT_ID") private DeptLite deptLite; ... } @Entity @org.hibernate.annotations.Entity(mutable=false) class DeptLite { } 
+7
source

If you want to limit the properties loaded in an object, I think there are two ways to achieve this:

  • Use lazy property fetching and comment on your model properties that you do not want to load every time the object is received.

  • Use Hibernate predictions and limit which properties you need in each query by adding a PropertyProjection for each property. Something like that:

     Projections.projectionList().add(Projections.property("prop1")).add(Projections.property("prop2")).. 

I hope for this help.

0
source

Source: https://habr.com/ru/post/1432838/


All Articles