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... }
以下代码显示了如何进行单向一对一映射。
下面的代码来自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更新示例以下代码显示了如何通过仅使用映射实体的setter方法来更新实体。下面的代码在persist方法调用后用setter方...
JPA教程 -JPA删除关系示例我们可以通过将引用设置为null来删除映射中的关系,如下所示。以下代码删除了地址和员工之间的链接,然...
JPA教程 -JPA查询连接提取示例以下代码显示了JPQL的连接提取语法。List l = em.createQuery(SELECT e FROM Professor e JOIN FET...
JPA教程 -JPA查询Like示例JPQL支持SQL LIKE操作符,以提供有限形式的字符串模式匹配。每个LIKE表达式由要搜索的字符串表达式,以...