zoukankan      html  css  js  c++  java
  • 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 生成器 迭代器
    廖---高级特性 切片 迭代 列表生成式
    汉诺塔
    廖---函数
    廖---控制流
    廖---list tuple dic set
    廖---字符串和编码
    MySQL常见的三种存储引擎
    mysql悲观锁以及乐观锁总结和实践
    数据库事务的四大特性以及事务的隔离级别
  • 原文地址:https://www.cnblogs.com/dreamfree/p/4077098.html
Copyright © 2011-2022 走看看