• JPA---一对一关系


    在JPA中,使用@oneToOne来标示。

    package com.yl.demo1.bean.oneToone;
    
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToOne;
    
    @Entity
    public class User {
        private Integer id;
        private String name;
        private IDCard idcard;
        
        public User(){}
        
        public User(String name) {
            super();
            this.name = name;
        }
        @Id @GeneratedValue
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        @Column(length=10, nullable=false)
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        @OneToOne(optional=false, cascade=CascadeType.ALL)
        @JoinColumn(name="idcard_id")
        public IDCard getIdcard() {
            return idcard;
        }
        public void setIdcard(IDCard idcard) {
            this.idcard = idcard;
        }
        
        
    }
     1 package com.yl.demo1.bean.oneToone;
     2 
     3 import javax.persistence.CascadeType;
     4 import javax.persistence.Column;
     5 import javax.persistence.Entity;
     6 import javax.persistence.GeneratedValue;
     7 import javax.persistence.Id;
     8 import javax.persistence.OneToOne;
     9 
    10 @Entity
    11 public class IDCard {
    12     private Integer id;
    13     private String cardno;
    14     private User user;
    15     
    16     public IDCard(){}
    17     
    18     public IDCard(String cardno) {
    19         super();
    20         this.cardno = cardno;
    21     }
    22     @Id @GeneratedValue
    23     public Integer getId() {
    24         return id;
    25     }
    26     public void setId(Integer id) {
    27         this.id = id;
    28     }
    29     @Column(length=18, nullable=false)
    30     public String getCardno() {
    31         return cardno;
    32     }
    33     public void setCardno(String cardno) {
    34         this.cardno = cardno;
    35     }
    36     /**
    37      * 在一对一的关系中,出现mappedBy的对象为关系的被维护端
    38      * 
    39      */
    40     @OneToOne(mappedBy="idcard", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
    41     public User getUser() {
    42         return user;
    43     }
    44     public void setUser(User user) {
    45         this.user = user;
    46     }
    47     
    48     
    49 }

     常见操作:

     1 @Test
     2     public void save() {
     3         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");
     4         EntityManager em = factory.createEntityManager();
     5         em.getTransaction().begin();//事务开始
     6         
     7         User user = new User("ylll");
     8         user.setIdcard(new IDCard("123456789012345678"));
     9         
    10         em.persist(user);
    11         
    12         em.getTransaction().commit();
    13         em.close();
    14         factory.close();
    15     }
  • 相关阅读:
    [Python] Python2 、Python3 urllib 模块对应关系
    [Python] Mac pip安装的模块包路径以及常规python路径
    git 使用详解
    版本控制工具简介
    python基础练习题(二)
    python简介,安装
    python基础练习题(一)
    python练习题之面向对象(三)
    python之input函数,if,else条件语句使用的练习题(一)
    C++ 静态变量
  • 原文地址:https://www.cnblogs.com/dreamfree/p/4077098.html
Copyright © 2020-2023  润新知