I'm new to JPA/Hibernate. Suppose I have these two tables:
- Employee (Id, Name, DeptId, ..) // DeptId is foreign key.
- Department (Id, DeptName, ..) // Department persisted separately
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.
- How to fetch the dept. name column ONLY and not the entire Department row in the employee entity (need to avoid eager fetches for huge collections made by department)? if so, what annotations should I use?
- How to handle cascade in this scenario?