以下部分显示如何将Java集合映射到数据库。它使用@ElementCollection
注释来标记集合中的元素类型。
以下代码来自Employee.java。
package cn..common; import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; import java.util.Set; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; // Using a targetClass instead of generics @ElementCollection(targetClass=VacationEntry.class) private Collection vacationBookings = new LinkedList(); // Using generics in place of a targetClass @ElementCollection private Set<String> nickNames = new HashSet(); public Collection getVacationBookings() { return vacationBookings; } public void setVacationBookings(Collection vacationBookings) { this.vacationBookings = vacationBookings; } public Set<String> getNickNames() { return nickNames; } public void setNickNames(Set<String> nickNames) { this.nickNames = nickNames; } 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 String toString() { return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary() + " nickNames: " + nickNames; } }
以下代码来自VacationEntry.java。
package cn..common; import java.util.Calendar; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Embeddable public class VacationEntry { @Temporal(TemporalType.DATE) private Calendar startDate; @Column(name="DAYS") private int daysTaken; public VacationEntry() {} public VacationEntry(Calendar aStartDate, int daysTakenInt) { startDate = aStartDate; daysTaken = daysTakenInt; } public Calendar getStartDate() { return startDate; } public void setStartDate(Calendar startDate) { this.startDate = startDate; } public int getDaysTaken() { return daysTaken; } public void setDaysTaken(int daysTaken) { this.daysTaken = daysTaken; } public String toString() { return "VacationEntry startDate: " + getStartDate().getTime() + ", daysTaken: " + getDaysTaken(); } }
下面的代码来自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() { Employee emp = new Employee(); emp.setName("name"); emp.setSalary(12345); VacationEntry v = new VacationEntry(); v.setDaysTaken(10); emp.getVacationBookings().add(v); emp.getNickNames().add("Tester"); em.persist(emp); } @PersistenceContext private EntityManager em; }下载 ElementCollection_Generic_TargetEntity.zip
这里是数据库表转储。
Table Name: EMPLOYEE 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 Table Name: EMPLOYEE_NICKNAMES Row: Column Name: EMPLOYEE_ID, Column Type: INTEGER: Column Value: 1 Column Name: NICKNAMES, Column Type: VARCHAR: Column Value: Tester Table Name: EMPLOYEE_VACATIONBOOKINGS Row: Column Name: EMPLOYEE_ID, Column Type: INTEGER: Column Value: 1 Column Name: DAYS, Column Type: INTEGER: Column Value: 10 Column Name: STARTDATE, Column Type: DATE: Column Value: null
JPA教程 -JPA 嵌入式ID示例以下代码显示了如何将类用作嵌入式ID。首先它创建一个Embeddable实体。@Embeddablepublic class Profe...
JPA教程 -JPA双向一对一映射示例以下代码显示了如何进行双向一对一映射。例子下面的代码来自Department.java。package cn..commo...
JPA教程 -JPA一对多映射表示例以下代码显示如何使用连接表来链接一对多映射。它使用 @JoinTable 注释来设置连接表。@OneToMany@J...
JPA教程 -JPA查询对象函数示例关键字OBJECT可以用于指示查询的结果类型是绑定到标识变量的实体。它对查询没有影响,但它可以用作...
JPA教程 -JPA查询AVG简单示例JPQL中的聚合查询的语法与SQL的语法非常相似。有五个支持的聚合函数AVGCOUNTMINMAXSUM结果可以分组...