Java教程 第60页

JPA 一对多映射键示例

JPA教程 – JPA一对多映射键示例 以下代码显示如何在一对多映射中设置映射的键。 @OneToMany(targetEntity=Person.class, mappedBy="department") @MapKey(name="id") private Set<Person> persons = new HashSet<Person>(); @MapKey(name =“id")设置为使用person id作为person的映射键组。 例子 下面的代码来自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.ManyToOne; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToOne 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=" +...

JPA 单向一对多映射示例

JPA教程 – JPA 单向一对多映射示例 以下代码显示了如何进行单向一对多映射。 例子 下面的代码来自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() { Professor emp = new Professor(); emp.setName("name"); emp.setSalary(12345); Phone p = new Phone(); p.setNumber("1234509876");; emp.addPhone(p); em.persist(p); em.persist(emp); } @PersistenceContext private EntityManager em; } 以下代码来自Professor.java。 package cn.w3cschool.common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.OneToMany; @Entity public class Professor { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @OneToMany @JoinTable(name="EMP_PHONE", joinColumns=@JoinColumn(name="EMP_ID"), inverseJoinColumns=@JoinColumn(name="PHONE_ID")) private Collection<Phone> phones; public Professor() { phones = new ArrayList<Phone>(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name;...

JPA 一对多映射表示例

JPA教程 – JPA一对多映射表示例 以下代码显示如何使用连接表来链接一对多映射。 它使用 @JoinTable 注释来设置连接表。 @OneToMany @JoinTable(name = "PERSON_DEPT", joinColumns = @JoinColumn(name = "EMP_ID"), inverseJoinColumns = @JoinColumn(name = "PHONE_ID")) private Set<Person> persons = new HashSet<Person>(); 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.OneToMany; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToMany @JoinTable(name = "PERSON_DEPT", joinColumns = @JoinColumn(name = "EMP_ID"), inverseJoinColumns = @JoinColumn(name = "PHONE_ID")) private Set<Person> persons = new HashSet<Person>(); public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() {...

JPA 多对一连接列示例

JPA教程 – JPA多对一连接列示例 以下代码标记了在多对一映射中使用的连接列。 @ManyToOne (cascade=CascadeType.ALL) @JoinColumn(name="DEPT_ID") private Department department; 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OrderBy; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @OneToMany(targetEntity=Person.class, mappedBy="department") @OrderBy("name ASC") private Set<Person> persons = new HashSet<Person>(); public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; } 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 + ", persons=" +...

JPA 多对一映射示例

JPA教程 – JPA多对一映射示例 以下代码显示了如何进行多对一映射。 它创建两个实体,人员和部门。一个部门可以有很多人。 在Person类的部门字段中,它用@ManyToOne注释标记部门。 @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToOne (cascade=CascadeType.ALL) 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.ManyToOne; import javax.persistence.CascadeType; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToOne (cascade=CascadeType.ALL) 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...

JPA 多对多映射示例

JPA教程 – JPA 多对多映射示例 以下代码显示如何创建多对多映射。 它描述了人与部门之间的关系。一个部门可以有很多人,一个人可以为许多部门工作。 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Department implements java.io.Serializable{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToMany private Set<Person> persons = new HashSet<Person>(); public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; } 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 + ", persons=" + persons + "]"; } } 下面的代码来自Person.java。 package cn.w3cschool.common;...

JPA 多对多连接表示例

JPA教程 – JPA 多对多连接表示例 以下代码显示如何为多对多映射设置连接表。 例子 下面的代码来自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(){ Department d1 = new Department(); d1.setName("Design"); Department d2 = new Department(); d2.setName("Testing"); Person p1 = new Person("Tom"); p1.getDepartments().add(d1); p1.getDepartments().add(d2); Person p2 = new Person("Jack"); p2.getDepartments().add(d1); p2.getDepartments().add(d2); d1.getPersons().add(p1); d1.getPersons().add(p2); em.persist(p1); em.persist(p2); em.persist(d1); em.persist(d2); } @PersistenceContext private EntityManager em; } 下面的代码来自Person.java。 package cn.w3cschool.common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToMany @JoinTable(name="PERSON_DEPT", joinColumns=@JoinColumn(name="PERSON_ID"), inverseJoinColumns=@JoinColumn(name="DEPT_ID")) private Set<Department> departments = new HashSet<Department>(); public Person() {} public Person(String name) { this.name = name; } public Set<Department> getDepartments() {...

JPA 双向多对多映射示例

JPA教程 – JPA双向多对多映射示例 以下代码显示如何创建双向多对多映射。 例子 以下代码来自Professor.java。 package cn.w3cschool.common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Professor { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @ManyToMany private Collection<Project> projects; public Professor() { projects = new ArrayList<Project>(); } 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 void addProject(Project project) { if (!getProjects().contains(project)) { getProjects().add(project); } if (!project.getEmployees().contains(this)) { project.getEmployees().add(this); } } public Collection<Project> getProjects() { return projects;...

JPA 覆盖多对多映射示例

JPA教程 – JPA覆盖多对多映射示例 以下代码使用注释来覆盖多对多映射的默认列。 @ManyToMany @JoinTable(name="DEPT_EMP", joinColumns=@JoinColumn(name="DEPT_ID"), inverseJoinColumns=@JoinColumn(name="EMP_ID")) @AttributeOverrides({ @AttributeOverride( name="first_Name", column=@Column(name="EMP_FNAME")), @AttributeOverride( name="last_Name", column=@Column(name="EMP_LNAME")) }) private Map<EmployeeName, Employee> employeesByName; 例子 以下代码来自Employee.java。 package cn.w3cschool.common; import java.util.Collection; import java.util.ArrayList; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Embedded; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Embedded private EmployeeName name; private long salary; @ManyToMany(mappedBy="employeesByName") private Collection<Department> departments; public Employee() { name = new EmployeeName(); departments = new ArrayList<Department>(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public EmployeeName getName() { return name; } public void setName(EmployeeName name) { this.name = name; } public String getFirstName() { return name.getFirst_Name(); } public void...

JPA 单向一对一映射示例

JPA教程 – JPA单向一对一映射示例 以下代码显示了如何进行单向一对一映射。 例子 下面的代码来自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() { 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.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 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;...