博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Hibernate] - one to one
阅读量:7027 次
发布时间:2019-06-28

本文共 5252 字,大约阅读时间需要 17 分钟。

两种不同方式的一对一映射关系:


 

1)配置文件:

hibernate.cfg.xml

com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1/testdb
root
1
org.hibernate.dialect.MySQL5Dialect
thread
org.hibernate.cache.internal.NoCacheProvider
true
update

 

Employer.hbm.xml

 

Address.hbm.xml

employer

 

Detial.hbm.xml

 

2)Java bean:

package com.my.bean;import java.util.Date;public class Employer {    private long id;    private String companyName;    private Date createTime;    private Address address;    private Detial detial;        public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getCompanyName() {        return companyName;    }    public void setCompanyName(String companyName) {        this.companyName = companyName;    }    public Date getCreateTime() {        return createTime;    }    public void setCreateTime(Date createTime) {        this.createTime = createTime;    }    public Address getAddress() {        return address;    }    public void setAddress(Address address) {        this.address = address;    }    public Detial getDetial() {        return detial;    }    public void setDetial(Detial detial) {        this.detial = detial;    }    }
package com.my.bean;public class Address {    private long id;    private String location;    private Employer employer;        public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getLocation() {        return location;    }    public void setLocation(String location) {        this.location = location;    }    public Employer getEmployer() {        return employer;    }    public void setEmployer(Employer employer) {        this.employer = employer;    }    }
package com.my.bean;public class Detial {    private long id;    private String info;    private Employer employer;        public long getId() {        return id;    }    public void setId(long id) {        this.id = id;    }    public String getInfo() {        return info;    }    public void setInfo(String info) {        this.info = info;    }    public Employer getEmployer() {        return employer;    }    public void setEmployer(Employer employer) {        this.employer = employer;    }}

 

3)测试:

package com.my.init;import java.util.Date;import java.util.HashSet;import java.util.List;import java.util.Set;import javax.xml.soap.Detail;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import com.my.bean.Address;import com.my.bean.Detial;import com.my.bean.Employer;import com.my.bean.User;import com.my.bean.UserAccount;import com.my.dao.util.HibernateUtil;public class Test {    @SuppressWarnings("unchecked")    public static void main(String[] args) {        Session session = HibernateUtil.getSessionFactory().openSession();        Transaction tx = session.beginTransaction();                // new employer        Employer employer = new Employer();        employer.setCompanyName("my company");        employer.setCreateTime(new Date());                // new address        Address address = new Address();        address.setLocation("china");                // new detail        Detial detail = new Detial();        detail.setInfo("info");                // add relation        employer.setAddress(address);        employer.setDetial(detail);        address.setEmployer(employer);        detail.setEmployer(employer);                session.save(employer);        session.save(address);        session.save(detail);                // Get employer        String hsqlSelect = "from Employer";        Query query = session.createQuery(hsqlSelect);        List
es = query.list(); if(es != null && es.size() > 0){ Employer e = es.get(0); System.out.println(e.getCompanyName()); System.out.println(e.getDetial().getInfo()); } tx.commit(); session.close(); }}

 


 

关注点:

1)Employer.hbm.xml

这个generator的class="native"值有好几种,具体使用方法可参见:

这里有两个one-to-one,两种一对一的不同映射方式。

其中第二种one-to-one中有一个属性引用,对应的是在detial中的employer属性。

property-ref的使用说明可参见:

 

2)Address.hbm.xml

employer

这是声明一个外部键字段的一对一关系用法。

这里这个contrained设为true是必需的。

 

3)Detial.hbm.xml

这是一对一关系的另一种现实方式,使用的是many-to-one,但unique设为true是必需的。

 

这两种不同的一对一关系映射,参考资料:

 

转载地址:http://cdrxl.baihongyu.com/

你可能感兴趣的文章
Exception异常处理机制
查看>>
复杂的web---web中B/S网络架构
查看>>
编写文档的时候各种问题
查看>>
Eclipse里maven的project报Unbound classpath variable: 'M2_REPO/**/***/***.jar
查看>>
新旅程CSS 基础篇分享一
查看>>
查看内核函数调用的调试方法【原创】
查看>>
个人项目中遇到的问题
查看>>
byte与base64string的相互转化以及加密算法
查看>>
20145103 《Java程序设计》第3周学习总结
查看>>
ubuntu声音系统
查看>>
哈哈更新资源列表2
查看>>
冲刺第一周第五天
查看>>
Java 接口
查看>>
Android 微信第三方登录
查看>>
硬盘的读写原理
查看>>
实例 centos自动挂载、备份windows共享文件夹,并删除第7日前当天的备份
查看>>
LNMP下动静分离部署phpmyadmin软件包
查看>>
如何写更好的自动化测试用例
查看>>
60再谈指针
查看>>
repost
查看>>