• hibernate基础07:关联映射之基于外键单向一对一


    1、Java实体Bean类

    package com.project.pojo;
    
    import java.io.Serializable;
    
    public class Card implements Serializable{
        private int id;
        private String address;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        
    
    }
    package com.project.pojo;
    
    import java.io.Serializable;
    
    public class Person implements Serializable{
        private int id ;
        private String name;
        private String sex;
        private Card card;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public Card getCard() {
            return card;
        }
        public void setCard(Card card) {
            this.card = card;
        }
    
    }

    2、hbm.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.project.pojo">
        <class name="Card" table="t_card">
            <id name="id" column="id" type="int">
                <generator class="native"></generator>
            </id>
            <property name="address" />
        </class>
    
    </hibernate-mapping>
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.project.pojo">
        <class name="Person" table="t_person">
            <id name="id" column="id" type="int">
                <generator class="native"></generator>
            </id>
            <property name="name" />
            <property name="sex" />
            <many-to-one name="card" column="card_id" unique="true" />
        </class>
    
    </hibernate-mapping>

    3、hibernate.cfg.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        
        <session-factory>
            <!-- 1、数据库连接信息 -->
            <!-- 指定数据方言 -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://192.168.1.59:3306/hibernate?characterEncoding=UTF8</property>
            <property name="connection.username">root</property>
            <property name="connection.password">1234</property>
            
            <!-- 2、通用配置信息 -->
            <!-- 打印sql语句 -->
            <property name="show_sql">true</property>
            <!-- 格式化sql语句 -->
            <property name="format_sql">true</property>
            
            <!-- 映射文件信息 -->
            <mapping resource="com/project/pojo/Card.hbm.xml" />
            <mapping resource="com/project/pojo/Person.hbm.xml" />
        </session-factory >
    </hibernate-configuration>

    4、测试

    package com.project.test;
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    import com.project.pojo.Card;
    import com.project.pojo.Person;
    import com.project.util.HibernateUtil;
    
    public class HibernateTest {
        Session session = null;
        Transaction ts = null;
        
        @Before
        public void setUp(){
            session = HibernateUtil.getSession();
            ts = session.beginTransaction();
        }
        
        @After
        public void tearDown(){
            HibernateUtil.closeSession();
        }
        
        @Test
        public void testCreateDB(){
            Configuration cfg = new Configuration().configure();
            //使得hibernate映射信息转换为数据库识别的dll语言
            SchemaExport se = new SchemaExport(cfg);
            //第一个参数:是否打印dll语句
            //第二个参数:是否将dll到数据库中执行
            se.create(true, true);
        }
        
        @Test
        public void testInit(){
            try {
                Card c1 = new Card();
                c1.setAddress("xx市1区");
                Card c2 = new Card();
                c2.setAddress("xx市2区");
                session.save(c1);
                session.save(c2);
                
                Person p1 = new Person();
                p1.setName("张三");
                p1.setSex("男");
                p1.setCard(c1);
                Person p2 = new Person();
                p2.setName("李四");
                p2.setSex("男");
                p2.setCard(c2);
                
                session.save(p1);
                session.save(p2);
                ts.commit();
            } catch (Exception e) {
                e.printStackTrace();
                if(ts!=null)ts.rollback();
            }
        }
        
        @Test
        public void testSelect(){
            Person p = (Person)session.get(Person.class, 1);
            System.out.println(p.getName()+"	"+p.getSex());
            System.out.println("---------------");
            System.out.println(p.getCard().getAddress());
            
        }
    }
  • 相关阅读:
    打造自己的性能测试类
    网站配置之最佳实践
    C#控制台窗口居中显示(转)
    将PDM文件导出成CHM帮助文件
    分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限
    Oracle 给已创建的表增加自增长列
    托管调试助手“NonComVisibleBaseClass”检测到问题
    翻页效果
    Excel表格转Json数据结构
    关于2D渲染的一些小想法
  • 原文地址:https://www.cnblogs.com/chai-blogs/p/12934292.html
Copyright © 2020-2023  润新知