以下代码显示了如何将集合与目标实体映射到数据库。
// Using a targetClass instead of generics @ElementCollection(targetClass=VacationEntry.class) @CollectionTable(name="VACATION",joinColumns=@JoinColumn(name="EMP_ID")) @AttributeOverride(name="daysTaken",column=@Column(name="DAYS_ABS")) private Collection vacationBookings = new LinkedList();
以下代码来自Employee.java。
package cn..common; import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; import java.util.Set; import javax.persistence.AttributeOverride; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; @Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; // Using a targetClass instead of generics @ElementCollection(targetClass=VacationEntry.class) @CollectionTable(name="VACATION", joinColumns=@JoinColumn(name="EMP_ID")) @AttributeOverride(name="daysTaken", column=@Column(name="DAYS_ABS")) private Collection vacationBookings = new LinkedList(); // Using generics in place of a targetClass @ElementCollection @Column(name="NICKNAME") 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 String toString() { return "Employee id: " + getId() + " name: " + getName() + " nickNames: " + nickNames; } }
下面的代码来自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"); VacationEntry v = new VacationEntry(); v.setDaysTaken(10); emp.getVacationBookings().add(v); emp.getNickNames().add("Tester"); em.persist(emp); } @PersistenceContext private EntityManager em; }
以下代码来自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(); } }下载 ElementCollection_CollectionTable_Override.zip
以下是数据库转储。
Table Name: EMPLOYEE Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: name Table Name: EMPLOYEE_NICKNAMES Row: Column Name: EMPLOYEE_ID, Column Type: INTEGER: Column Value: 1 Column Name: NICKNAME, Column Type: VARCHAR: Column Value: Tester Table Name: VACATION Row: Column Name: EMP_ID, Column Type: INTEGER: Column Value: 1 Column Name: DAYS_ABS, Column Type: INTEGER: Column Value: 10 Column Name: STARTDATE, Column Type: DATE: Column Value: null
JPA教程 -JPA查询分页示例PersonDaoImpl在下面的部分显示如何逐页显示结果。例子下面的代码来自Department.java。package cn..co...
Lucene教程 -Lucene搜索文件搜索过程是进行搜索的过程。下表列出了在搜索过程中使用的类。类描述IndexSearcher读取/搜索索引处理...
Java XML教程 -Java DOM编辑属性以下代码显示如何向元素添加属性。import java.io.StringWriter;import javax.xml.parsers.Docum...
Java线程教程 -Java线程优先级所有线程都有优先级。优先级由1到10之间的整数表示。优先级为1的线程优先级最低。优先级为10的线程...
JavaFX教程 -JavaFX滑块滑块可以显示和与一系列数值进行交互。滑块控件有两件事:一个轨道和一个可拖动的拇指。在轨道上,我们可...