JPA中one-to-one关系的单向映射示例

设有2个实体,雇员 和停车位 。一个雇员只能拥有一个停车位,一个停车位只能属于一个雇员,因此他们是1对1的关系。在1对1关系中,我们需要区分主导者和从属者,所谓的主导者就是拥有外键的实体。本例中我们将雇员设置为主导者。下面我们用sql语句建立者两个表(基于MySql5.1数据库系统):

 

 

--  创建EMPLOYEE表  -- 
CREATE TABLE EMPLOYEE(
    ID INTEGER NOT NULL AUTO_INCREMENT ,
    PSPACE_ID INTEGER NOT NULL ,
    NAME VARCHAR (20) NOT NULL ,
    SALARY INTEGER NOT NULL ,
    LAST_UPDATED_TIME TIMESTAMP NOT NULL ,
    PRIMARY KEY (ID),
    FOREIGN KEY (PSPACE_ID) REFERENCES PARKING_SPACE (ID)
);
 


--  创建PARKING_SPACE表  -- 
CREATE TABLE PARKING_SPACE(
    ID INTEGER NOT NULL AUTO_INCREMENT ,
    LOT INTEGER NOT NULL ,
    LOCATION VARCHAR (100) NOT NULL ,
    LAST_UPDATED_TIME TIMESTAMP NOT NULL ,
    PRIMARY KEY (ID)
);

 

接下来编写Employee类和ParkingSpace类:

[java]  view plain copy
  1. package andycpp;  
  2. import java.io.Serializable;  
  3. import java.util.Date;  
  4. import javax.persistence.*;  
  5. @Entity(name="parking_space")  
  6. public class ParkingSpace implements Serializable {  
  7.     @Id  
  8.     @Column(name="ID",  nullable=false)  
  9.     @GeneratedValue(strategy=GenerationType.IDENTITY)  
  10.     private Long id;  
  11.       
  12.     @Column(name="LOT", nullable=false)  
  13.     private int lot;  
  14.       
  15.     @Column(name="LOCATION", nullable=false)  
  16.     private String location;  
  17.       
  18.     @Version  
  19.     @Column(name="LAST_UPDATED_TIME", nullable=false)  
  20.     private Date updateTime;  
  21.     public Long getId() {  
  22.         return id;  
  23.     }  
  24.     public void setId(Long id) {  
  25.         this.id = id;  
  26.     }  
  27.     public int getLot() {  
  28.         return lot;  
  29.     }  
  30.     public void setLot(int lot) {  
  31.         this.lot = lot;  
  32.     }  
  33.     public String getLocation() {  
  34.         return location;  
  35.     }  
  36.     public void setLocation(String location) {  
  37.         this.location = location;  
  38.     }  
  39.     public Date getUpdateTime() {  
  40.         return updateTime;  
  41.     }  
  42.     public void setUpdateTime(Date updateTime) {  
  43.         this.updateTime = updateTime;  
  44.     }  
  45.       
  46.     private static final long serialVersionUID = 1760382073923264461L;  
  47. }  

 

[java]  view plain copy
  1. package andycpp;  
  2. import java.io.Serializable;  
  3. import javax.persistence.*;  
  4. @Entity(name="employee")  
  5. public class Employee implements Serializable {  
  6.       
  7.     @Id  
  8.     @Column(name="ID", nullable=false)  
  9.     @GeneratedValue(strategy=GenerationType.IDENTITY)  
  10.     private Long id;  
  11.       
  12.     @Column(name="NAME", nullable=false)  
  13.     private String name;  
  14.       
  15.     @Column(name="SALARY", nullable=false)  
  16.     private int salary;  
  17.       
  18.     @OneToOne(cascade=CascadeType.ALL)  
  19.     @JoinColumn(name="PSPACE_ID")  
  20.     private ParkingSpace pSpace;  
  21.     public Long getId() {  
  22.         return id;  
  23.     }  
  24.     public void setId(Long id) {  
  25.         this.id = id;  
  26.     }  
  27.     public String getName() {  
  28.         return name;  
  29.     }  
  30.     public void setName(String name) {  
  31.         this.name = name;  
  32.     }  
  33.     public int getSalary() {  
  34.         return salary;  
  35.     }  
  36.     public void setSalary(int salary) {  
  37.         this.salary = salary;  
  38.     }  
  39.     public ParkingSpace getpSpace() {  
  40.         return pSpace;  
  41.     }  
  42.     public void setpSpace(ParkingSpace pSpace) {  
  43.         this.pSpace = pSpace;  
  44.     }  
  45.       
  46.       
  47. }  

[java]  view plain copy
  1. //逆向连接的Inverse端  
  2. package andycpp;  
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5. import javax.persistence.*;  
  6. @Entity  
  7. @Table(name="parking_space")  
  8. public class ParkingSpace implements Serializable {  
  9.     @Id  
  10.     @Column(name="ID",  nullable=false)  
  11.     @GeneratedValue(strategy=GenerationType.IDENTITY)  
  12.     private Long id;  
  13.       
  14.     @Column(name="LOT", nullable=false)  
  15.     private int lot;  
  16.       
  17.     @Column(name="LOCATION", nullable=false)  
  18.     private String location;  
  19.       
  20.     @Version  
  21.     @Column(name="LAST_UPDATED_TIME", nullable=false)  
  22.     private Date updateTime;  
  23.       
  24.     @OneToOne(mappedBy="pSpace")  
  25.     private Employee employee;  
  26.     public Long getId() {  
  27.         return id;  
  28.     }  
  29.     public void setId(Long id) {  
  30.         this.id = id;  
  31.     }  
  32.     public int getLot() {  
  33.         return lot;  
  34.     }  
  35.     public void setLot(int lot) {  
  36.         this.lot = lot;  
  37.     }  
  38.     public String getLocation() {  
  39.         return location;  
  40.     }  
  41.     public void setLocation(String location) {  
  42.         this.location = location;  
  43.     }  
  44.     public Date getUpdateTime() {  
  45.         return updateTime;  
  46.     }  
  47.     public void setUpdateTime(Date updateTime) {  
  48.         this.updateTime = updateTime;  
  49.     }  
  50.       
  51.     public Employee getEmployee() {  
  52.         return employee;  
  53.     }  
  54.     public void setEmployee(Employee employee) {  
  55.         this.employee = employee;  
  56.     }  
  57.   
  58.     private static final long serialVersionUID = 1760382073923264461L;  
  59. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值