• Hibernate中的一对一注解配置


    Card类

    package cn.OneToOne2017109.entity;
    
    import javax.persistence.*;
    
    /**
     * Created by YSS on 2017/10/9.
     */

    @Entity
    @Table(name = "Card")
    public class Card {
        @Id
        @GeneratedValue
        private Integer cid;
        @Column
        private String cnum;
        @OneToOne(mappedBy = "card",cascade = CascadeType.ALL)
        private People people;


        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 People getPeople() {
            return people;
        }

        public void setPeople(People people) {
            this.people = people;
        }
    }

     

    People类

    @Entity
    @Table(name = "People")
    public class People {
        @Id
        @GeneratedValue
        private Integer pid;
        @Column
        private String pname;
        @OneToOne
        @JoinColumn(name = "cid")
        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;
        }
    }

    测试类

    public class testonetoone {
    
        Configuration cfg;
        SessionFactory factory;
        Session session;
        Transaction tx;
    
        @Before
        public void mybefor(){
            cfg=new Configuration().configure();
            factory=cfg.buildSessionFactory();
            session=factory.openSession();
            tx=session.beginTransaction();
        }
        @After
        public void myafter(){
            tx.commit();
        }
    
        @Test
        public void test03() {
            People people=new People();
            Card card=new Card();
            people.setPname("123");
            card.setCnum("123133");
            card.setPeople(people);
            people.setCard(card);
            session.save(card);
        }
    }

    将大配置中的mapping节点改为

    <mapping class=" "/>

  • 相关阅读:
    「学习笔记」杂项算法学习笔记
    「CF484E」Sign on Fence「整体二分」「线段树」
    「BZOJ 2653」middle「主席树」「二分」
    「BZOJ 5010」「FJOI 2017」矩阵填数「状压DP」
    jmeter阶梯式加压测试
    jmeter监控内存,CPU等方法
    jmeter 读取多个用户名并同时发
    APP性能测试工具
    Android--iOS抓取崩溃日志
    安装并使用PICT,生成测试用例
  • 原文地址:https://www.cnblogs.com/hfddz/p/7642094.html
Copyright © 2020-2023  润新知