• hibernate一对一映射


    package loaderman.c_one2one;
    
    // 身份证
    public class IdCard {
    
        // 身份证号(主键)
        private String cardNum;// 对象唯一表示(Object Identified, OID)
        private String place; //  身份证地址
        // 身份证与用户,一对一的关系
        private User user;
    
    
        public String getCardNum() {
            return cardNum;
        }
        public void setCardNum(String cardNum) {
            this.cardNum = cardNum;
        }
        public String getPlace() {
            return place;
        }
        public void setPlace(String place) {
            this.place = place;
        }
        public User getUser() {
            return user;
        }
        public void setUser(User user) {
            this.user = user;
        }
    
    
    }
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="loaderman.c_one2one">
        
        <class name="IdCard" table="t_IdCard">
            <id name="cardNum">
                <generator class="assigned"></generator>
            </id>    
            <property name="place" length="20"></property>
            
            <!-- 
                一对一映射,有外键方
                unique="true"   给外键字段添加唯一约束
             -->
             <many-to-one name="user" unique="true" column="user_id" class="User" cascade="save-update"></many-to-one>
                
        </class>
        
    
    </hibernate-mapping>
    package loaderman.c_one2one;
    
    // 用户
    public class User {
    
        private int userId;
        private String userName;
        // 用户与身份证信息, 一对一关系
        private IdCard idCard;
    
    
        public IdCard getIdCard() {
            return idCard;
        }
        public void setIdCard(IdCard idCard) {
            this.idCard = idCard;
        }
        public int getUserId() {
            return userId;
        }
        public void setUserId(int userId) {
            this.userId = userId;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
    
    }
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="loaderman.c_one2one">
        
        <class name="User" table="t_user">
            <id name="userId">
                <generator class="native"></generator>
            </id>    
            <property name="userName" length="20"></property>
            <!-- 
                一对一映射: 没有外键方
             -->
             <one-to-one name="idCard" class="IdCard"></one-to-one>
                 
        </class>
        
    
    </hibernate-mapping>
    package loaderman.c_one2one;
    
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.classic.Session;
    import org.junit.Test;
    
    public class App {
    
        private static SessionFactory sf;
        static {
            sf = new Configuration()
                    .configure()
                    .addClass(IdCard.class)
                    .addClass(User.class)   // 测试时候使用
                    .buildSessionFactory();
        }
    
        @Test
        public void getSave() {
    
            Session session = sf.openSession();
            session.beginTransaction();
    
            // 用户
            User user = new User();
            user.setUserName("Jack");
            // 身份证
            IdCard idCard = new IdCard();
            idCard.setCardNum("441202XXX");
            idCard.setPlace("广州XXX");
            // 关系
            idCard.setUser(user);
    
            // ----保存----
            session.save(idCard);
    
            session.getTransaction().commit();
            session.close();
    
        }
    
    }


    package loaderman.c_one2one2;
    
    
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.classic.Session;
    import org.junit.Test;
    
    public class App {
    
        private static SessionFactory sf;
        static {
            sf = new Configuration()
                    .configure()
                    .addClass(IdCard.class)
                    .addClass(User.class)   // 测试时候使用
                    .buildSessionFactory();
        }
    
        @Test
        public void getSave() {
    
            Session session = sf.openSession();
            session.beginTransaction();
    
            // 用户
            User user = new User();
            user.setUserName("Jack");
            // 身份证
            IdCard idCard = new IdCard();
            idCard.setCardNum("441202XXX");
            idCard.setPlace("广州XXX");
            // 关系
            idCard.setUser(user);
    
            // ----保存----
            session.save(idCard);
    
            session.getTransaction().commit();
            session.close();
    
        }
    
    }
    package loaderman.c_one2one2;
    
    // 身份证
    public class IdCard {
    
        private int user_id;
        // 身份证号
        private String cardNum;
        private String place; //  身份证地址
        // 身份证与用户,一对一的关系
        private User user;
    
    
    
        public int getUser_id() {
            return user_id;
        }
        public void setUser_id(int userId) {
            user_id = userId;
        }
        public String getCardNum() {
            return cardNum;
        }
        public void setCardNum(String cardNum) {
            this.cardNum = cardNum;
        }
        public String getPlace() {
            return place;
        }
        public void setPlace(String place) {
            this.place = place;
        }
        public User getUser() {
            return user;
        }
        public void setUser(User user) {
            this.user = user;
        }
    
    
    }
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="loaderman.c_one2one2">
        
        <class name="IdCard" table="t_IdCard">
            <id name="user_id">
                <!-- 
                    id 节点指定的是主键映射, 即user_id是主键
                    主键生成方式: foreign  即把别的表的主键作为当前表的主键;
                            property (关键字不能修改)指定引用的对象     对象的全名 cn..User、  对象映射 cn.User.hbm.xml、   table(id)
                 -->
                <generator class="foreign">
                    <param name="property">user</param>
                </generator>
            </id>    
            <property name="cardNum" length="20"></property>
            <property name="place" length="20"></property>
            
            <!-- 
                一对一映射,有外键方
                (基于主键的映射)
                 constrained="true"  指定在主键上添加外键约束
             -->
            <one-to-one name="user" class="User" constrained="true"  cascade="save-update"></one-to-one>
                
        </class>
        
    
    </hibernate-mapping>
    package loaderman.c_one2one2;
    // 用户
    public class User {
    
        private int userId;
        private String userName;
        // 用户与身份证信息, 一对一关系
        private IdCard idCard;
    
    
        public IdCard getIdCard() {
            return idCard;
        }
        public void setIdCard(IdCard idCard) {
            this.idCard = idCard;
        }
        public int getUserId() {
            return userId;
        }
        public void setUserId(int userId) {
            this.userId = userId;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
    
    }
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="loaderman.c_one2one2">
        
        <class name="User" table="t_user">
            <id name="userId">
                <generator class="native"></generator>
            </id>    
            <property name="userName" length="20"></property>
            <!-- 
                一对一映射: 没有外键方
             -->
             <one-to-one name="idCard" class="IdCard"></one-to-one>
                 
        </class>
        
    
    </hibernate-mapping>
  • 相关阅读:
    WCF Restful调用跨域解决方案
    [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你!
    人体呼吸信号的数据挖掘
    Spark编译及spark开发环境搭建
    诗两首------重庆项目出差有感
    eclipse安装和中文汉化,以及配置
    Querying CRM data with LINQ
    oracle pl/sql之在java中怎么调用oracle函数
    oracle pl/sql之oracle函数
    oracle pl/sql之java中调用oracle有参存储过程
  • 原文地址:https://www.cnblogs.com/loaderman/p/10037930.html
Copyright © 2020-2023  润新知