I'm new to JPA/Hibernate. Suppose I have these two tables:
and Entities like 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 application DAO, 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.
The best option is to make the collection lazy loaded and then change the queries where the collection is needed to eager load (using join fetch). If you have a very good reason not to do that then you can try the following workarounds.
You can use a projection query. This will result in a [employee,name] array for each result.
select employee, employee.dept.name from Employee employee
You can use @Formula to map an attribute in Employee table to a column in Department table (note that this solution is Hibernate-specific)
class Employee {
@Formula("(select deptName from Department where Department.id = DEPT_ID)"
String deptName;
}
The other option is to create an new class DeptLite which doesn't have the collection. Map 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 {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With