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)
- hibernate-对一映射
- hibernate多对一映射
- Hibernate 多对一映射
- 【Hibernate】多对一映射
- hibernate多对一映射
- Hibernate映射之多对一关联映射
- Hibernate关联映射--多对一映射
- Hibernate多对一单向关联映射
- hibernate多对一关联映射
- hibernate多对一关联映射
- hibernate之多对一关联映射
- hibernate映射多对一关联
- hibernate 数据多对一映射
- hibernate:多对一单向关联映射
- hibernate多对一关联映射
- hibernate---->多对一关联映射
- Hibernate映射----(一对多,多对一)
- Hibernate多对一映射单向关联
- 象素shader入门(Introduction to Pixel Shaders)- NO
- 蒙特卡洛(Monte Carlo)积分的入门
- 职场里面一些发现的事情、规律
- linux 内核线程
- asp 中判断变量为空的方法及isempty、isnull与空字符串
- hibernate-对一映射
- 分析SQL语句性能3种方法
- Java 异常处理
- 关于tomcat虚拟路径配置
- IOS开发(65)之GCD任务分组
- 马云卸任阿里巴巴CEO演讲全文:因为信任,所以简单!
- 使用ZeroClipboard解决跨浏览器复制到剪贴板的问题
- 使用EXP命令实现逻辑备份实验
- 八大地图API开发平台大比拼