zoukankan      html  css  js  c++  java
  • hibermate一对一关联

    在hibernate.cfg.xml配置<mapping class="oneToOne.IDCard" />,以及实体类的get和set方法省略了.

    User类

    @Entity
    @Table(name = "t_user")
    public class User {
        @Id
        @GenericGenerator(name = "myuuid", strategy = "uuid")
        @GeneratedValue(generator = "myuuid")
        private String id;
        private String name;
        @OneToOne(targetEntity = IDCard.class, mappedBy = "user")
        private IDCard idCard;

    IDCard类

    @Entity
    @Table(name = "t_idcard")
    public class IDCard {
        @Id
        @GenericGenerator(name = "myuuid", strategy = "uuid")
        @GeneratedValue(generator = "myuuid")
        private String id;
        private String cardNum;
        @OneToOne(targetEntity = User.class)
        @JoinColumn(name = "c_user_id")
        @Cascade(CascadeType.SAVE_UPDATE)
        private User user;

    Test类

    @Test
        public void test1() {
            Session session = HibernateUtils.openSession();
            session.beginTransaction();
            // 1.创建一个用户
            User user = new User();
            user.setName("张三");
            // 2.创建身份证号
            IDCard card = new IDCard();
            card.setCardNum("123321");
            // 3.身份证号关联人
            card.setUser(user);
            // 4.储存身份证号
            session.save(card);
            session.getTransaction();
            session.close();
        }
  • 相关阅读:
    webpack 添加 jquery 插件
    openldap安装配置
    生成ssl证书
    vyos (三) HA
    vyatta的fork开源版本
    vyos (一) 基础配置
    iptable软路由
    keepalive实现web服务器active/passive
    docker学习(二)
    docker学习(一)
  • 原文地址:https://www.cnblogs.com/wwwzzz/p/7880704.html
Copyright © 2011-2022 走看看