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

阅读更多
1、one-to-many/many-to-one双向关联
hbm.xml配置方式:

	
		
	
	


	
		
	
	
		
		
	


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 getSoldiers() {
    ...
}

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


2、one-to-one基于外键关联:
hbm.xml配置:

	
		
	
	



	
		
	
	


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基于主键关联:

	
		
	
	


	
		
			person
		
	
	



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

1、one-to-many/many-to-one表连接
hbm.xml配置方式:

	
		
	
	
		
		
	



	
		
	
	
		
		
	


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

2、one-to-one:配置文件基本一致,只不过在不需要维护关系的一方添加inverse="true".不常用!!!不推荐!!!

	
		
	
	
		
		
	



	
		
	
	
		
		
	


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注解方式:

	
		
	
	
		
		
	



	
		
	
	
		
		
	


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;
    }
}

你可能感兴趣的:(hibernate,java)