• 【Hibernate】---【注解】一对一


    一、核心配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.password">root</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
            <property name="hibernate.connection.username">root</property>
            
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.format_sql">true</property>
            <property name="hibernate.hbm2ddl.auto">update</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            
            <property name="current_session_context_class">thread</property>
    
            <!-- 
            <mapping resource="com/chinasofti/entity/User.hbm.xml"/>
            <mapping resource="com/chinasofti/entity/Role.hbm.xml"/>
             -->
            
            <mapping class="com.chinasofti.entity.Person"/>
            <mapping class="com.chinasofti.entity.Card"/>
            
        </session-factory>
    </hibernate-configuration>

    二、实体类

      person

    package com.chinasofti.entity;
    
    
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToOne;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="T_Person")
    public class Person {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer pid;
        private String pname;
    
        @OneToOne(cascade=CascadeType.ALL,mappedBy="person")
        @JoinColumn(name="pid")
        private Card card;
    
        public Integer getPid() {
            return pid;
        }
        public void setPid(Integer pid) {
            this.pid = pid;
        }
        public String getPname() {
            return pname;
        }
        public void setPname(String pname) {
            this.pname = pname;
        }
        public Card getCard() {
            return card;
        }
        public void setCard(Card card) {
            this.card = card;
        }
        @Override
        public String toString() {
            return "Person [pid=" + pid + ", pname=" + pname + ", card=" + card
                    + "]";
        }
        
    }

    Card

    package com.chinasofti.entity;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToOne;
    import javax.persistence.Table;
    
    
    @Entity
    @Table(name="T_Card")
    public class Card {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer cid;
        private String cnum;
        
        @OneToOne
        @JoinColumn(name="pid")
        private Person person;
    
        public Integer getCid() {
            return cid;
        }
        public void setCid(Integer cid) {
            this.cid = cid;
        }
        public String getCnum() {
            return cnum;
        }
        public void setCnum(String cnum) {
            this.cnum = cnum;
        }
        public Person getPerson() {
            return person;
        }
        public void setPerson(Person person) {
            this.person = person;
        }
        @Override
        public String toString() {
            return "Card [cid=" + cid + ", cnum=" + cnum + ", person=" + person
                    + "]";
        }
    }

    三、封装类

    package com.chinasofti.entity;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtil {
        static Configuration CONFIGURATION = null;
        static SessionFactory SESSION_FACTORY = null;
        static Session SESSION = null;
        static{
            CONFIGURATION = new Configuration();
            CONFIGURATION.configure();
            SESSION_FACTORY = CONFIGURATION.buildSessionFactory();
            SESSION    = SESSION_FACTORY.openSession();
        }
        public static SessionFactory getSessionFactory(){
            return SESSION_FACTORY;
        }
        public static Session openSession(){
            return SESSION;
        }
    }

    四、测试类

    package com.chinasofti.entity;
    
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.junit.Test;
    
    
    public class TestOneOne {
        @Test
        public void test1to1(){
            Session session = HibernateUtil.openSession();
            Transaction transaction = session.beginTransaction();
            Person person = session.get(Person.class, 1);
            session.delete(person);
            transaction.commit();
            session.close();
        }
        
    }

    补充

  • 相关阅读:
    android.os.NetworkOnMainThreadException 异常处理
    java.lang.SecurityException: Permission denied (missing INTERNET permission?) 解决
    javah 生成header file 报错 问题解决
    AAPT: libpng error: Not a PNG file 问题解决
    Tomcat启动报错 Failed to start component [StandardServer[8005]]解决
    Hibernate
    Linux常用命令总结
    模拟奇数乱码请求问题
    http国际化模拟请求
    db2数据库安装注意几个问题
  • 原文地址:https://www.cnblogs.com/angelye/p/7464867.html
Copyright © 2020-2023  润新知