博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关系映射的1to1
阅读量:5261 次
发布时间:2019-06-14

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

关系映射的1to1

 1中是基于外键的一对一

配置文件

//配置级联操作

在这两个配置文件中person是主表,address是字表。

package bean;public class Address {    private Integer id;    private String descs;    private int code;        private Person person;                public Address(){}    public Address(String descs,int code)    {        this.code = code;        this.descs = descs;    }    @Override    public String toString() {        return "Address [id=" + id + ", descs=" + descs + ", code=" + code                + "]";    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getDescs() {        return descs;    }    public void setDescs(String descs) {        this.descs = descs;    }    public int getCode() {        return code;    }    public void setCode(int code) {        this.code = code;    }    public Person getPerson() {        return person;    }    public void setPerson(Person person) {        this.person = person;    }                                            }
package bean;import java.util.Date;public class Person {    private Integer id;    private String name;    private int password;    private Date birthday;    private Address address;        public Person(){}    public Person(String name,int password,Date birthday)    {        this.name= name;        this.password = password;        this.birthday = birthday;    }    @Override    public String toString() {        return "Person [id=" + id + ", name=" + name + ", password=" + password                + ", birthday=" + birthday + ", address=" + address + "]";    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getPassword() {        return password;    }    public void setPassword(int password) {        this.password = password;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }    public Address getAddress() {        return address;    }    public void setAddress(Address address) {        this.address = address;    }                                }

测试的方法

package Test;import java.util.Date;import bean.Address;import bean.Person;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;public class HibernateTest {    SessionFactory  sessionFactory =null;    Session session =null;    Transaction tx = null;    /**     * 初始化测试数据      * @throws Exception     */    public void setUp() throws Exception {        System.out.println("------setUp---初始化测试资源-----");        Configuration config = new Configuration().configure();        ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();        sessionFactory = config.buildSessionFactory(sr);        session = sessionFactory.openSession();    }            public void testAdd(){        Address p = new Address("郑州",456465);                tx = session.beginTransaction();            session.persist(p);        tx.commit();    }        public void testAdd2(){        Address address = new Address("郑州",456465);        Person person = new Person("tiger",123,new Date());        address.setPerson(person);        person.setAddress(address);        tx = session.beginTransaction();            session.persist(person);        session.persist(address);        tx.commit();    }                public void testGet(){                Person p = (Person)session.get(Person.class, 1);                System.out.println(p);            }                    /**     * 释放测试数据      * @throws Exception     */        public void tearDown() throws Exception {        System.out.println("------tearDown---释放测试数据---");        session.close();        sessionFactory.close();    }        public static void main(String [] args) throws Exception    {        HibernateTest h = new HibernateTest();        h.setUp();        h.testAdd2();        h.tearDown();    }}

 一对一关联查询的时候,如果是根据主表查找字表的数据就立即查询(一条查询语句就搞定根据左外连接),如果从字表查找主表的数据的时候是延迟查询(两条sql语句,也是左外连接实现

public void testGet() {        //通过人获得地址        Person p = (Person)session.get(Person.class, 1);        System.out.println(p);        System.out.println(p.getAddress());    }        @Test    public void testGet2() {        //通过地址获得人        Address addrss = (Address)session.get(Address.class, 1);        System.out.println(addrss);        System.out.println(addrss.getPerson());    }

 

public void testAdd2(){        Address address = new Address("郑州",456465);        Person person = new Person("tiger",123,new Date());        address.setPerson(person);        //设置了级联操作所以可以不需要再 手动保存 address类了        //person.setAddress(address);        tx = session.beginTransaction();            session.persist(person);        session.persist(address);        tx.commit();    }

 

)。

 

基于主键的一对一配置文件

-
-
-
-
-
-
person

 

转载于:https://www.cnblogs.com/aicpcode/p/4196722.html

你可能感兴趣的文章
Multiplication Game(博弈论)
查看>>
DDD:如何处理“唯一性”业务逻辑
查看>>
MySQL(多表的表记录的查询)
查看>>
js学习进阶-页面覆盖
查看>>
python作用域和名称空间
查看>>
java 多线程
查看>>
关于mysql的索引原理与慢查询优化
查看>>
Java源码学习备份
查看>>
ComboBox相关操作
查看>>
第六章 第一个嵌入式Linux驱动程序 统计单词个数 心得体会
查看>>
c++开发规范
查看>>
日期时间处理类
查看>>
Centos安装wine等组件的问题
查看>>
No module named 'flask_sqlalchemy'
查看>>
Android中,如何提升Layout的性能?
查看>>
HNOI2019 游记
查看>>
服务器上Tomcat启动慢的问题
查看>>
php7新特性
查看>>
hdu5304 基环 外向树
查看>>
爬虫制作入门学习笔记2:[转]python爬虫实例项目大全
查看>>