hibernate双向关联hbm.xml和annotation方式_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > hibernate双向关联hbm.xml和annotation方式

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

 2015/4/9 18:30:57  clover灬  程序员俱乐部  我要评论(0)
  • 摘要:1、one-to-many/many-to-one双向关联hbm.xml配置方式:<classname="Person"><idname="id"column="personId"><generatorclass="native"/></id><many-to-onename="address"column="addressId"not-null="true"/></class><
  • 标签:not Annotation 方式 hibernate XML
1、one-to-many/many-to-one双向关联
hbm.xml配置方式:
class="xml" name="code">
<class name="Person">
	<id name="id" column="personId">
		<generator class="native"/>
	</id>
	<many-to-one name="address" column="addressId" not-null="true"/>
</class>
<class name="Address">
	<id name="id" column="addressId">
		<generator class="native"/>
	</id>
	<set name="people" inverse="true">
		<key column="addressId"/>
		<one-to-many class="Person"/>
	</set>
</class>

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注解方式:
@Entity
public class Troop {
    @OneToMany(mappedBy="troop")
    public Set<Soldier> getSoldiers() {
    ...
}

@Entity
public class Soldier {
    @ManyToOne
    @JoinColumn(name="troop_fk")
    public Troop getTroop() {
    ...
}


2、one-to-one基于外键关联:
hbm.xml配置:
<class name="Person">
	<id name="id" column="personId">
		<generator class="native"/>
	</id>
	<many-to-one name="address" column="addressId" unique="true" not-null="true"/>
</class>

<class name="Address">
	<id name="id" column="addressId">
		<generator class="native"/>
	</id>
	<one-to-one name="person" property-ref="address"/>
</class>

annotation注解方式:
@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() {
    ...
}


one-to-one基于主键关联:
<class name="Person">
	<id name="id" column="personId">
		<generator class="native"/>
	</id>
	<one-to-one name="address"/>
</class>
<class name="Address">
	<id name="id" column="personId">
		<generator class="foreign">
			<param name="property">person</param>
		</generator>
	</id>
	<one-to-one name="person" constrained="true"/>
</class>


==================================================================================
================================以下是表连接方式====================================

1、one-to-many/many-to-one表连接
hbm.xml配置方式:
<class name="Person">
	<id name="id" column="personId">
		<generator class="native"/>
	</id>
	<set name="addresses" table="PersonAddress">
		<key column="personId"/>
		<many-to-many column="addressId" unique="true" class="Address"/>
	</set>
</class>

<class name="Address">
	<id name="id" column="addressId">
		<generator class="native"/>
	</id>
	<join table="PersonAddress" inverse="true" optional="true">
		<key column="addressId"/>
		<many-to-one name="person" column="personId" not-null="true"/>
	</join>
</class>

annotation注解方式:
@Entity
public class Troop {
    @OneToMany(mappedBy="troop")
    public Set<Soldier> getSoldiers() {
    ...
}

@Entity
public class Soldier {
    @ManyToOne
   @JoinTable(name="soldierTroop",
	   joinColumns=@JoinColumn(name="sid"),
	   inverseJoinColumns=@JoinColumn(name="tid"))
    public Troop getTroop() {
    ...
} 

2、one-to-one:配置文件基本一致,只不过在不需要维护关系的一方添加inverse="true".不常用!!!不推荐!!!
<class name="Person">
	<id name="id" column="personId">
		<generator class="native"/>
	</id>
	<join table="PersonAddress" optional="true">
		<key column="personId" unique="true"/>
		<many-to-one name="address" column="addressId" not-null="true" unique="true"/>
	</join>
</class>

<class name="Address">
	<id name="id" column="addressId">
		<generator class="native"/>
	</id>
	<join table="PersonAddress" optional="true" inverse="true">
		<key column="addressId" unique="true"/>
		<many-to-one name="person" column="personId" not-null="true" unique="true"/>
	</join>
</class>

annotation注解方式:
@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() {
    ...
}

3、many-to-many:
hbm.xml注解方式:
<class name="Person">
	<id name="id" column="personId">
		<generator class="native"/>
	</id>
	<set name="addresses" table="PersonAddress">
		<key column="personId"/>
		<many-to-many column="addressId" class="Address"/>
	</set>
</class>

<class name="Address">
	<id name="id" column="addressId">
		<generator class="native"/>
	</id>
	<set name="people" inverse="true" table="PersonAddress">
		<key column="addressId"/>
		<many-to-many column="personId" class="Person"/>
	</set>
</class>

annotation注解方式:
@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;
    }
}
发表评论
用户名: 匿名