[英]Hibernate refresh in many-to-one relation
I have this 2 domain Model :我有这个 2 域模型:
Employee员工
public class Employee {
private Long id;
private String name;
private String family;
private Company company;
//getter & setter
}
and Company :和公司:
public class Company {公共类公司{
private Long id;
private String name;
private Set<Employee> employees;
//getter & setter
} }
and Employee has a many-to-one relation with Company , and from Company side there is one-to-many relation . Employee 和 Company 是多对一的关系,从 Company 端来看是一对多的关系。
i save a Company Object in transaction and after save the Employee with that company :我在交易中保存了一个公司对象,然后在该公司保存了员工:
@Transactional
puplic Long Save (){
Company cmp=new Company();
cmp.setName("Oracle");
session.save(cmp);
Employee employee=new Employee();
employee.setName("james");
employee.setFamily("dep");
employee.setCompany(cmp);
cmp.getEmployees();//will raise NullPointerException
cmp=session.get(cmp.getId());//because of first level cache it doesnt load it
}
after save employee if i want to get the collection of employee from company it it will raise the null pointer exception and if i want to load it because of first level cache hibernate will not touch the database and the set of employee is still null .保存员工后,如果我想从公司获取员工的集合,它将引发空指针异常,如果我想加载它,因为一级缓存休眠将不会触及数据库并且员工集仍然为空。
the first solution is that i can refresh the Company object after save the employee , but i think hibernate must have solution except of it .第一个解决方案是我可以在保存员工后刷新 Company 对象,但我认为 hibernate 必须有除此之外的解决方案。
You should never use null
collections, so you should change your mapping to this:您永远不应该使用null
,因此您应该将映射更改为:
private Set<Employee> employees = new HashSet<>();
This way, you don't even need a setter, since a getter is sufficient.这样,您甚至不需要 setter,因为 getter 就足够了。 Also, make sure you synchronize both sides of the bidirectional association:另外,请确保同步双向关联的双方:
public void addEmployee(Employee employee) {
employees.add(employee);
employee.setCompany(this);
}
public void removeEmployee(Employee employee) {
employees.remove(employee);
employee.setCompany(null);
}
You also need to implement equals
and hashCode
properly in your child entity classes.您还需要在子实体类中正确实现equals
和hashCode
。
Look into my code ,看看我的代码,
After commit() the transaction only u can get persistant object,在 commit() 事务之后,只有你可以获得持久对象,
public class Employee {
private Long id;
private String name;
private String family;
@ManyToOne
@JoinColumn(name = "company_id_fk")
private Company company;
//getter & setter
}
public class Company {
private Long id;
private String name;
@OneToMany(mappedBy="company")
List<Employee> listOfEmployees = new ArrayList<Employee>();
//getter & setter
}
puplic Long Save (){
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
try{
Company cmp=new Company();
cmp.setName("Oracle");
session.save(cmp);
Employee employee=new Employee();
employee.setName("james");
employee.setFamily("dep");
employee.setCompany(cmp);
session.save(employee);
transaction.commit();
List<Employee> listOfEmployees = cmp.getListOfEmployees();
if(listOfEmployees !=null && !listOfEmployees .isEmpty()){
for(Employee employ:listOfEmployees){
//You can wt you want
}
}
}catch(Exception e){
transaction.rollback();
}finally{
session.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.