hibernate-对一映射

来源:互联网 发布:统计数据库 编辑:程序博客网 时间:2024/05/17 00:00

一对一映射

 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 FKBB979BF476B8AEC4drop table if exists addressdrop table if exists customercreate 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 FKEE4206E64CDCFCAFdrop table if exists d_idcarddrop table if exists d_studentcreate 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)