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类:

package andycpp; import java.io.Serializable; import java.util.Date; import javax.persistence.*; @Entity(name="parking_space") public class ParkingSpace implements Serializable { @Id @Column(name="ID", nullable=false) @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @Column(name="LOT", nullable=false) private int lot; @Column(name="LOCATION", nullable=false) private String location; @Version @Column(name="LAST_UPDATED_TIME", nullable=false) private Date updateTime; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public int getLot() { return lot; } public void setLot(int lot) { this.lot = lot; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } private static final long serialVersionUID = 1760382073923264461L; }

 

package andycpp; import java.io.Serializable; import javax.persistence.*; @Entity(name="employee") public class Employee implements Serializable { @Id @Column(name="ID", nullable=false) @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @Column(name="NAME", nullable=false) private String name; @Column(name="SALARY", nullable=false) private int salary; @OneToOne(cascade=CascadeType.ALL) @JoinColumn(name="PSPACE_ID") private ParkingSpace pSpace; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } public ParkingSpace getpSpace() { return pSpace; } public void setpSpace(ParkingSpace pSpace) { this.pSpace = pSpace; } }

//逆向连接的Inverse端 package andycpp; import java.io.Serializable; import java.util.Date; import javax.persistence.*; @Entity @Table(name="parking_space") public class ParkingSpace implements Serializable { @Id @Column(name="ID", nullable=false) @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @Column(name="LOT", nullable=false) private int lot; @Column(name="LOCATION", nullable=false) private String location; @Version @Column(name="LAST_UPDATED_TIME", nullable=false) private Date updateTime; @OneToOne(mappedBy="pSpace") private Employee employee; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public int getLot() { return lot; } public void setLot(int lot) { this.lot = lot; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public Employee getEmployee() { return employee; } public void setEmployee(Employee employee) { this.employee = employee; } private static final long serialVersionUID = 1760382073923264461L; }

你可能感兴趣的:(Date,String,jpa,table,null,Integer)