举报投诉联系我们 手机版 热门标签 编程学
您的位置:编程学 > jpa单向一对多 JPA 单向一对一映射示例

jpa单向一对多 JPA 单向一对一映射示例

2023-03-10 06:18 Java教程

jpa单向一对多 JPA 单向一对一映射示例

jpa单向一对多

JPA单向一对多是指一个实体类(Entity)关联另一个实体类(Entity)的关系。在单向一对多的关系中,一方拥有多方的引用。这意味着一方可以访问多方的数据,而多方不能访问一方的数据。

JPA单向一对多关系是通过@OneToMany注释来实现的。@OneToMany注释用于表明实体之间存在单向一对多的关系。@OneToMany注释有三个属性,分别是targetEntity、mappedBy和cascade。targetEntity属性用于指定目标实体;mappedBy属性用于指定另一端使用了@ManyToOne注释的属性名称;cascade属性用于指定需要在当前端执行的操作。

@Entity 
public class Student { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id; 

    private String name; 

    @OneToMany(targetEntity = Course.class, mappedBy = "student", cascade = CascadeType.ALL) 
    private List<Course> courses; 

    // getters and setters... 
} 

JPA 单向一对一映射示例

JPA教程 - JPA单向一对一映射示例


以下代码显示了如何进行单向一对一映射。

例子

下面的代码来自PersonDaoImpl.java。

package cn..common;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class PersonDaoImpl {
  public void test() {

    Professor emp = new Professor();
    emp.setName("name");
    emp.setSalary(12345);
    

    ParkingSpace ps = new ParkingSpace();
    ps.setLot(1);
    ps.setLocation("East");
    emp.setParkingSpace(ps);
    
    em.persist(ps);
    em.persist(emp);
  }

  @PersistenceContext
  private EntityManager em;
}

以下代码来自Professor.java。

package cn..common;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Professor {
  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;
  private String name;
  private long salary;
  
  @OneToOne 
  @JoinColumn(name="PSPACE_ID") 
  private ParkingSpace parkingSpace;

  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }

  public long getSalary() {
      return salary;
  }

  public void setSalary(long salary) {
      this.salary = salary;
  }
  
  public ParkingSpace getParkingSpace() {
      return parkingSpace;
  }
  
  public void setParkingSpace(ParkingSpace parkingSpace) {
      this.parkingSpace = parkingSpace;
  }

  public String toString() {
      return "Employee id: " + getId() + " name: " + getName() + 
             " with " + getParkingSpace();
  }
}

下面的代码来自ParkingSpace.java。

package cn..common;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="PARKING_SPACE")
public class ParkingSpace {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private int lot;
    private String location;

    public int getId() {
        return id;
    }
    
    public void setId(int 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 deptName) {
        this.location = deptName;
    }

    public String toString() {
        return "ParkingSpace id: " + getId() + " lot: " + getLot() +
               ", location: " + getLocation();
    }

}
下载 OneToOne_Unidirectional.zip

以下是数据库转储。

Table Name: PARKING_SPACE
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: LOCATION,
    Column Type: VARCHAR:
    Column Value: East

    Column Name: LOT,
    Column Type: INTEGER:
    Column Value: 1

Table Name: PROFESSOR
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: NAME,
    Column Type: VARCHAR:
    Column Value: name

    Column Name: SALARY,
    Column Type: BIGINT:
    Column Value: 12345

    Column Name: PSPACE_ID,
    Column Type: INTEGER:
    Column Value: 1


阅读全文
以上是编程学为你收集整理的jpa单向一对多 JPA 单向一对一映射示例全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 编程学 bianchengxue.com 版权所有 联系我们
桂ICP备19012293号-7 返回底部