`
kylinsoong
  • 浏览: 240099 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate 备忘录

阅读更多

1. 生成表

两种方法,方法一,在Hibernate.cfg.xml配置文件中添加如下配置,则App运行过程中如数据库中表不存在,或根据相关条件生成新表,配置如下

<property name="hbm2ddl.auto">update</property>


 

 方法二,运用SchemaExport生成新表,如下代码

public static void main(String[] args) {
SchemaExport se = new SchemaExport(new AnnotationConfiguration().configure());
se.create(true, true);
}

      为了和Hibernate 备忘录-续风格统一,这里对本文做一修改,本文包括一对一外键单向关联、一对一外键双向关联、一对一主键单向关联、一对一主键双向关联、联合主键5个部分,每部分包括:描述、Annotation配置、结果说明,老版本Hibernate的XML配置;

 2. 一对一单向外键关联

2.1 如下图所示为一个一对一关系的映射,这里所有都用这个映射为例; 

 如上图,Husband和Wife为一对一关系,

2.2Annotation配置代码

@Entity
public class Husband {
	private int id;
	private String name;
	private Wife wife;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne(cascade=CascadeType.ALL)
	@JoinColumn(name="wifeId")
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}	
}

 

@Entity
public class Wife {
	private int id;
	private String name;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

  2.3、此配置下数据库中生成表结构如下:



 2.4、老版本HibernateXML配置

<hibernate-mapping>
	<class name="com.kylin.test.Husband" dynamic-update="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<many-to-one name="wife" column="wideId" unique="true"></many-to-one>
    </class>
</hibernate-mapping>

 

<hibernate-mapping>
	<class name="com.kylin.test.Wife" dynamic-update="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
    </class>
</hibernate-mapping>

  

 3. 一对一双向外键关联

3.1 同上一Husband Wife来描述一对一双向外键关联

3.2 给出Annotation配置代码

@Entity
public class Husband {
	private int id;
	private String name;
	private Wife wife;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne
	@JoinColumn(name="wifeId") 
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}	
}

 

@Entity
public class Wife {
	private int id;
	private String name;
	private Husband husband;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne(mappedBy="wife") 
	public Husband getHusband() {
		return husband;
	}
	public void setHusband(Husband husband) {
		this.husband = husband;
	}
}

3.3、数据库中生成表如下



 3.4、老版本XML

<hibernate-mapping>
	<class name="com.kylin.test.Husband" dynamic-update="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<many-to-one name="wife" column="wideId" unique="true"></many-to-one>
    </class>
</hibernate-mapping>

 

<hibernate-mapping>
	<class name="com.kylin.test.Wife" dynamic-update="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<one-to-one name="husband" property-ref="wife"></one-to-one>
    </class>
</hibernate-mapping>

 3.5比较 。 运行结果和上面2导出的表相同;

总结比较Annotation配置和XML配置,

第一、使用XML配置核心要注意点:

<many-to-one name="wife" column="wideId" unique="true"></many-to-one>

  

<one-to-one name="husband" property-ref="wife"></one-to-one>

 

      如上分别为Husband.hbm.xml和Wife.hbm.xml上指定一对一双向映射的配置,其中Wife中property-ref值必须与Husband中name值相同

第二、明显Annotation配置更容易明白,XML配置比较难理解,所以一般采取Annotation;

 

4. 一对一单向主键关联

4.1 用Husband及Wife为例说明

4.2 Annotation配置代码

@Entity
public class Husband {
	private int id;
	private String name;
	private Wife wife;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne(cascade=CascadeType.ALL)
	@PrimaryKeyJoinColumn
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}	
}

 

@Entity
public class Wife {
	private int id;
	private String name;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

4.3.导出的表结构



 4.4XML配置

<hibernate-mapping>
	<class name="com.kylin.test.Husband" dynamic-update="true">
		<id name="id">
			<generator class="foreign">
				<param name="property">wife</param>
			</generator>
		</id>
		<property name="name"/>
		<one-to-one name="wife" constrained="true"></one-to-one>
    </class>
</hibernate-mapping>

 

<hibernate-mapping>
	<class name="com.kylin.test.Wife" dynamic-update="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
    </class>
</hibernate-mapping>

 

 5. 一对一双向主键关联

5.1、同上一Husband Wife为例

5.2、给出配置代码

@Entity
public class Husband {
	private int id;
	private String name;
	private Wife wife;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne(cascade=CascadeType.ALL)
	@PrimaryKeyJoinColumn
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}	
}

 

@Entity
public class Wife {
	private int id;
	private String name;
	private Husband husband;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne  
    @PrimaryKeyJoinColumn 
	public Husband getHusband() {
		return husband;
	}
	public void setHusband(Husband husband) {
		this.husband = husband;
	}
}

 5.3表结构



 5.4、老版本XML

<hibernate-mapping>
	<class name="com.kylin.test.Husband" dynamic-update="true">
		<id name="id">
			<generator class="foreign">
				<param name="property">wife</param>
			</generator>
		</id>
		<property name="name"/>
		<one-to-one name="wife" constrained="true"></one-to-one>
    </class>
</hibernate-mapping>

 

<hibernate-mapping>
	<class name="com.kylin.test.Wife" dynamic-update="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<one-to-one name="husband" property-ref="wife"></one-to-one>
    </class>
</hibernate-mapping>

  

  6. 联合主键

6.1 同上,但这里只给出Annotation配置例子:

6.2.配置代码

@Entity
public class Husband {
	private int id;
	private String name;
	private Wife wife;
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne
	@JoinColumns(
			{
				@JoinColumn(name="wifeId", referencedColumnName="id"),
				@JoinColumn(name="wifeName", referencedColumnName="name")
			}
		)
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}
}

 

@Entity
@IdClass(WifePK.class)
public class Wife {
	private int id;
	private String name;
	private int age;
	@Id
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Id
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

 

public class WifePK {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

 6.3,表结构如下



 

 

PS……待续

 

  • 大小: 3.3 KB
  • 大小: 10 KB
  • 大小: 9.6 KB
  • 大小: 9.7 KB
  • 大小: 8.8 KB
  • 大小: 9 KB
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics