[英]CRUD in Hibernate with one-to-many mapping
I have three entities and three tables in database with their relation: 我在数据库中有三个实体和三个表及其关系:
Class person {
int id;
@OneToMany(mappedBy = "person", fetch = FetchType.LAZY)
List<Comment> comments;
@OneToMany(mappedBy = "person", fetch = FetchType.LAZY)
List<CellPhone> cellPhones;
}
Class Comment {
String content;
@ManyToOne
@JoinColumn(name = "id")
Person person;
}
Class CellPhone {
String mark;
@ManyToOne
@JoinColumn(name = "id")
Person person;
}
Does Hibernate support me to do something like that ? Hibernate是否支持我做类似的事情?
In general, I just want to set/get CellPhone and Comment for person object, and then call getPerson(person), save(person), or delete(person) ect without get(comment), save(comment) or delete(cellPhone).. 通常,我只想为person对象设置/获取CellPhone和Comment,然后调用getPerson(person),save(person)或delete(person)ect,而无需get(comment),save(comment)或delete(cellPhone) )..
Please help me to clarify it. 请帮我澄清一下。 Thanks.
谢谢。
The answer is yes to all of your questions. 答案是肯定的。
Specifically, for questions 1, 2, and 4 take a look at Hibernate cascading: 具体来说,对于问题1、2和4,请看一下Hibernate级联:
For question 3, Hibernate will automatically fetch the other entities once you request them (by calling their get method). 对于问题3,Hibernate将在您请求其他实体后自动获取它们(通过调用它们的get方法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.