hibernate-对一映射

一对一映射

 1:主键关联




ADDRESSS表中的ID字段主键,同时作为外键参照CUSTOMERS表中的主键。

package model;

public class Customer {
	private Long id;
	
	private String name;
	
	private Address address;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
	
}


package model;

public class Address {
	private Long id;
	
	private String city;
	
	private String province;
	
	private Customer customer;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
}

相应的映射文件为:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name = "entity.Customer" table = "customer" lazy="true">
		<id name="id" type = "long">
			<generator class="increment"></generator>
		</id>
		<property name="name">
			<column name="name" length="20"></column>
			<type name="string"></type>
		</property>
		<property name = "comAddress" type = "long"  column = "COM_ADDRESS_ID"/>
		<one-to-one name="address" class = "model.Address" cascade="all"></one-to-one>
	</class>
</hibernate-mapping>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name = "entity.Address" table = "address" lazy="true">
		<id name="id" type = "long">
			<generator class="foreign">
				<param name="property">customer</param>
			</generator>
		</id>
		<property name="city" type = "string" column = "CITY" length="25"></property>
		<property name = "province" type = "string" column = "PROVINCE" length = "25"></property>
		<one-to-one name="customer" class = "entity.Customer" constrained="true"></one-to-one>
	</class>
</hibernate-mapping>

<one-to-one>元素的constrained属性为true,表示ADDRESSES表的ID同时

作为外键参照CUSTOMERS表。

package model;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/*
 * alter table address drop foreign key FKBB979BF476B8AEC4
	drop table if exists address
	drop table if exists customer
	create table address (id bigint not null, CITY varchar(25), PROVINCE varchar(25), primary key (id))
	create table customer (id bigint not null, name varchar(20), COM_ADDRESS_ID bigint, primary key (id))
	alter table address add index FKBB979BF476B8AEC4 (id), add constraint FKBB979BF476B8AEC4 foreign key (id) references customer (id)

 */
public class CreateTest {
	public static void main(String[] args) {
		SchemaExport export = new SchemaExport(new Configuration().configure());
		export.create(true, false);
	}
}


2:外键关联。本质上就是一对多的蜕化形式。

将many-to-one元素增加unipue="true"的属性就变成了一对一。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name = "entity.Student" table = "d_student" lazy="true">
		<id name="id" type = "string">
			<generator class="uuid"></generator>
		</id>
		<property name="name">
			<column name="name" length="20"></column>
			<type name="string"></type>
		</property>
		<!-- 建立一对一的  -->
		<one-to-one name="idCard"  class="entity.IdCard" cascade="all" property-ref="student"></one-to-one>
	</class>
</hibernate-mapping>



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name = "entity.IdCard" table = "d_idcard" lazy="true">
		<id name="id" type = "string">
			<generator class="uuid"></generator>
		</id>
		<property name="number">
			<column name="number" length="15" not-null="true"></column>
			<type name="int"></type>
		</property>
		<many-to-one name = "student" class = "entity.Student" column="student_id" unique="true"/>
	</class>
</hibernate-mapping>
sql:

alter table d_idcard drop foreign key FKEE4206E64CDCFCAF
drop table if exists d_idcard
drop table if exists d_student
create table d_idcard (id varchar(255) not null, number integer not null, student_id varchar(255), primary key (id))
create table d_student (id varchar(255) not null, name varchar(20), primary key (id))
alter table d_idcard add index FKEE4206E64CDCFCAF (student_id), add constraint FKEE4206E64CDCFCAF foreign key (student_id) references d_student (id)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值