ITeye博客

hibernate双向关联hbm.xml和annotation方式

clover灬 · 2015-04-09 15:25 · hibernate · 编程语言 · Java · Hibernate · Markdown

1、one-to-many/many-to-one双向关联 hbm.xml配置方式: [code="xml"] [/code] inverse="true" 表示不在己方维护关系,但是如果not-null="true"已经设置了,这里就没有作用了。 因为not-null="true"表示维护关系的一方该字段不能为空,必须首先插入一的一方。 ---------------------------------------------------------------------------------------------------------------------------------------- What about the inverse mapping attribute? For you, and for Java, a bi-directional link is simply a matter of setting the references on both sides correctly. Hibernate, however, does not have enough information to correctly arrange SQL INSERT and UPDATE statements (to avoid constraint violations). Making one side of the association inverse tells Hibernate to consider it a mirror of the other side. That is all that is necessary for Hibernate to resolve any issues that arise when transforming a directional navigation model to a SQL database schema. The rules are straightforward: all bi-directional associations need one side as inverse. In a one-to-many association it has to be the many-side, and in many-to-many association you can select either side. ---------------------------------------------------------------------------------------------------------------------------------------- annotation注解方式: [code="java"] @Entity public class Troop { @OneToMany(mappedBy="troop") public Set getSoldiers() { ... } @Entity public class Soldier { @ManyToOne @JoinColumn(name="troop_fk") public Troop getTroop() { ... } [/code] 2、one-to-one基于外键关联: hbm.xml配置: [code="xml"] [/code] annotation注解方式: [code="java"] @Entity public class Customer implements Serializable { @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name="passport_fk") public Passport getPassport() { ... } @Entity public class Passport implements Serializable { @OneToOne(mappedBy = "passport") public Customer getOwner() { ... } [/code] one-to-one基于主键关联: [code="xml"] person [/code] [color=red]================================================================================== ================================以下是表连接方式==================================== [/color] 1、one-to-many/many-to-one表连接 hbm.xml配置方式: [code="xml"] [/code] annotation注解方式: [code="java"] @Entity public class Troop { @OneToMany(mappedBy="troop") public Set getSoldiers() { ... } @Entity public class Soldier { @ManyToOne @JoinTable(name="soldierTroop", joinColumns=@JoinColumn(name="sid"), inverseJoinColumns=@JoinColumn(name="tid")) public Troop getTroop() { ... } [/code] 2、one-to-one:配置文件基本一致,只不过在不需要维护关系的一方添加inverse="true".不常用!!!不推荐!!! [code="xml"] [/code] annotation注解方式: [code="java"] @Entity public class Customer implements Serializable { @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name="passport_fk") public Passport getPassport() { ... } @Entity public class Passport implements Serializable { @OneToOne(mappedBy = "passport") public Customer getOwner() { ... } [/code] 3、many-to-many: hbm.xml注解方式: [code="xml"] [/code] annotation注解方式: [code="java"] @Entity public class Employer implements Serializable { @ManyToMany( targetEntity=org.hibernate.test.metadata.manytomany.Employee.class, cascade={CascadeType.PERSIST, CascadeType.MERGE} ) @JoinTable( name="EMPLOYER_EMPLOYEE", joinColumns=@JoinColumn(name="EMPER_ID"), inverseJoinColumns=@JoinColumn(name="EMPEE_ID") ) public Collection getEmployees() { return employees; } ... } @Entity public class Employee implements Serializable { @ManyToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "employees", targetEntity = Employer.class ) public Collection getEmployers() { return employers; } } [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。