Java教程 第61页 JPA教程 – JPA双向一对一映射示例 以下代码显示了如何进行双向一对一映射。 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne(mappedBy="department") private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } 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; } } 下面的代码来自Person.java。 package cn.w3cschool.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 Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne @JoinColumn(name="DEPT_ID") private Department department; public Person() {}...
2024-04-01
JPA教程 – JPA一对一映射级联示例 以下代码显示如何为一对一映射设置级联操作。 @OneToOne(cascade=CascadeType.ALL,mappedBy="department") private Person person; 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne(cascade=CascadeType.ALL,mappedBy="department") private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } 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; } @Override public String toString() { return "Department [id=" + id + ", name=" + name + ", person=" + person + "]"; } } 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.List; import javax.persistence.EntityManager;...
2024-04-01
JPA教程 – JPA一对一主键连接列示例 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne @PrimaryKeyJoinColumn private Department department; public Person() {} public Person(String name) { this.name = name; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } 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; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + "]"; } } 下面的代码来自Department.java。 package cn.w3cschool.common; import javax.persistence.Entity;...
2024-04-01
JPA教程 – JPA一对一连接列示例 以下部分显示如何在JPA中为一对一映射设置连接列。 我们使用 @JoinColumn 注释设置连接列。 @OneToOne @JoinColumn(name="DEPT_ID") private Department department; 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne(mappedBy="department") private Person person; public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } 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; } @Override public String toString() { return "Department [id=" + id + ", name=" + name + ", person=" + person + "]"; } } 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity;...
2024-04-01
JPA教程 – JPA一对一延迟加载示例 以下代码显示如何使用延迟加载设置执行一对一映射。 @OneToOne(fetch=LAZY) private Department department; 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; import static javax.persistence.FetchType.LAZY; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne(fetch=LAZY) private Department department; public Person() {} public Person(String name) { this.name = name; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } 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; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + "]"; } }...
2024-04-01
JPA教程 – JPA 一对一映射示例 本节介绍如何在JPA映射中执行一对一映射。 此示例假设一个人只能是一个部门的一部分,而一个部门只能有一个人。 在Person实体中,我们使用@OneToOne注释标记Department引用属性。 @Entity public class Person { ... @OneToOne private Department department; ... 这里是设置两个实体并将它们保存到数据库的简单代码。 Person p1 = new Person("Tom"); p1.setName("Tom"); Department d = new Department(); d.setName("Design"); p1.setDepartment(d); em.persist(p1); em.persist(d); 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToOne; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToOne private Department department; public Person() {} public Person(String name) { this.name = name; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) {...
2024-04-01
JPA教程 – JPA 嵌入式键映射示例 以下代码显示如何在一对多映射中使用嵌入式键。 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MapKey; import javax.persistence.OneToMany; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @OneToMany(mappedBy="department") private Map<EmployeeName, Employee> employeesByName; public Department() { employeesByName = new HashMap<EmployeeName,Employee>(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String deptName) { this.name = deptName; } public Map<EmployeeName, Employee> getEmployees() { return employeesByName; } public void addEmployee(Employee employee) { EmployeeName empName = new EmployeeName(); empName.setFirst_Name(employee.getFirstName()); empName.setLast_Name(employee.getLastName()); employeesByName.put(empName, employee); if (employee.getDepartment() != null) { employee.getDepartment().removeEmployee(employee); } employee.setDepartment(this); } public void removeEmployee(Employee employee)...
2024-04-01
JPA教程 – JPA 嵌入式ID示例 以下代码显示了如何将类用作嵌入式ID。 首先它创建一个Embeddable实体。 @Embeddable public class ProfessorId implements Serializable{ private String country; @Column(name = "EMP_ID") private int id; 它用 @EmbeddedId 注释标记 ProfessorId 。 @Entity public class Professor { @EmbeddedId private ProfessorId id; 例子 以下代码来自Professor.java。 package cn.w3cschool.common; import javax.persistence.EmbeddedId; import javax.persistence.Entity; @Entity public class Professor { @EmbeddedId private ProfessorId id; private String name; private long salary; public Professor() { } public Professor(String country, int id) { this.id = new ProfessorId(country, id); } public int getId() { return id.getId(); } public String getCountry() { return id.getCountry(); } 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; }...
2024-04-01
JPA教程 – JPA嵌入式共享示例 对于嵌入式实体,我们可以在不同实体之间共享它们。 当嵌入它时,我们也可以覆盖可嵌入的实体。 以下代码首先创建一个Address实体。 @Embeddable @Access(AccessType.FIELD) public class Address { 然后创建一个Company类并引用Embeddable实体。 @Entity public class Company { @Id private int id; @Embedded private Address address; 之后,它再次在Employee类中嵌入Address类。这一次它覆盖了来自Address类的属性。 @Entity public class Employee { @Id private int id; private String name; private long salary; @Embedded @AttributeOverrides({ @AttributeOverride(name = "state", column = @Column(name = "PROVINCE")), @AttributeOverride(name = "zip", column = @Column(name = "POSTAL_CODE")) }) private Address address; 例子 以下代码来自Address.java。 package cn.w3cschool.common; import javax.persistence.Access; import javax.persistence.AccessType; import javax.persistence.Column; import javax.persistence.Embeddable; @Embeddable @Access(AccessType.FIELD) public class Address { private String street; private String city; private String state; @Column(name="ZIP_CODE") private String zip; public String getStreet() { return street; } public void setStreet(String address) { this.street = address; } public String getCity() { return city; } public void...
2024-04-01
JPA教程 – JPA嵌入式示例 以下部分演示如何将实体嵌入到另一个实体。 首先,我们通过用@Embeddable注释标记类来创建一个Embeddable实体。 @Embeddable @Access(AccessType.FIELD) public class Address { 然后,当将Embeddable实体添加到其容器类时,使用@Embedded标记它。 @Entity public class Employee { @Id private int id; private String name; private long salary; @Embedded private Address address; 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Employee emp = new Employee(); emp.setName("name"); emp.setSalary(12345); Address p = new Address(); p.setCity("New York"); emp.setAddress(p); em.persist(emp); } @PersistenceContext private EntityManager em; } 以下代码来自Employee.java。 package cn.w3cschool.common; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Employee { @Id private int id; private String name; private long salary; @Embedded private Address address; public Employee() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public...
2024-04-01