Java教程 第62页

JPA ElementCollection通用目标实体示例

JPA教程 – JPA ElementCollection通用目标实体示例 以下部分显示如何将Java集合映射到数据库。它使用@ElementCollection注释来标记集合中的元素类型。 例子 以下代码来自Employee.java。 package cn.w3cschool.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; }...

JPA ElementCollection映射关键实体示例

JPA教程 – JPA ElementCollection映射关键实体示例 在JPA中,我们可以将通用类型的Map映射到数据库。 以下代码定义了一个通用映射,其键值为Employee,值类型为Integer。 @ElementCollection @CollectionTable(name="EMP_SENIORITY") @MapKeyJoinColumn(name="EMP_ID") @Column(name="SENIORITY") private Map<Employee, Integer> seniorities; 例子 下面的代码来自Department.java。 package cn.w3cschool.common; import java.util.HashMap; import java.util.Map; 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.MapKeyJoinColumn; @Entity public class Department { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; @ElementCollection @CollectionTable(name="EMP_SENIORITY") @MapKeyJoinColumn(name="EMP_ID") @Column(name="SENIORITY") private Map<Employee, Integer> seniorities; public Department() { seniorities = new HashMap<Employee, Integer>(); } 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<Employee, Integer> getEmployees() { return seniorities; } public void setEmployeeSeniority(Employee employee, int seniority) { seniorities.put(employee, seniority); } public void...

JPA ElementCollection枚举映射示例

JPA教程 – JPA ElementCollection枚举映射示例 以下代码显示如何将带​​有枚举键值的Java java.util.Map保存到数据库。 PhoneType 是枚举类型。 @ElementCollection @CollectionTable(name="EMP_PHONE") @MapKeyEnumerated(EnumType.STRING) @MapKeyColumn(name="PHONE_TYPE") @Column(name="PHONE_NUM") private Map<PhoneType, String> phoneNumbers = new HashMap(); 例子 以下代码来自Employee.java。 package cn.w3cschool.common; import java.util.HashMap; import java.util.Map; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.MapKeyColumn; import javax.persistence.MapKeyEnumerated; enum PhoneType { Home, Mobile, Work } @Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @ElementCollection @CollectionTable(name="EMP_PHONE") @MapKeyEnumerated(EnumType.STRING) @MapKeyColumn(name="PHONE_TYPE") @Column(name="PHONE_NUM") private Map<PhoneType, String> phoneNumbers = new HashMap(); @ManyToOne private Department department; 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;...

JPA ElementCollection CollectionTable覆盖示例

JPA教程 – JPA ElementCollection CollectionTable覆盖示例 以下代码显示了如何将集合与目标实体映射到数据库。 // 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.w3cschool.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...

JPA Lob延迟加载示例

JPA教程 – JPA Lob延迟加载示例 我们可以通过仅提取频繁访问的数据来优化检索实体时的性能。如果需要,可以提取剩余的数据。 当使用@Lob注释将字节数组或字符数组字段映射到数据库时,我们可以设置是否对该字段执行延迟加载。 延迟加载使应用程序运行速度更快,因为JPA只在加载字节数组或字符数组时才使用它们。 基本映射的获取类型可以配置为通过在相应的@Basic注释中指定fetch元素来延迟或急切加载。 FetchType枚举类型定义此元素的值,可以是EAGER或LAZY。 @Basic(fetch=LAZY) @Lob @Column(name="PIC") private byte[] picture; 例子 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Professor emp = new Professor(); emp.setId(1); emp.setName("name"); emp.setSalary(12345); em.persist(emp); } @PersistenceContext private EntityManager em; } 以下代码来自Professor.java。 package cn.w3cschool.common; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Lob; import static javax.persistence.FetchType.LAZY; @Entity public class Professor { @Id private int id; private String name; private long salary; @Basic(fetch=LAZY) @Lob @Column(name="PIC") private byte[] picture; 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 =...

JPA Lob列示例-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

JPA Lob列示例

JPA教程 – JPA Lob列示例 以下代码显示了如何使用@Lob注释将字节数组保存到数据库。 LOB在数据库中有两种类型:字符大对象(称为CLOB)和二进制大对象(或BLOB)。 CLOB列保存大字符序列,BLOB列可存储大字节序列。 映射到BLOB列的Java类型是byte[] Byte[]和Serializable类型,而char[] Character[]和String对象映射到CLOB列。 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Lob; import javax.persistence.Table; @Entity @Table(name="EMP") public class Person { @Id @Column(name = "EMP_ID") private long id; @Basic private String name; private String surname; @Lob private byte[] picture; public Person() {} public Person(String name, String surname) { this.name = name; this.surname = surname; } 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; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public byte[] getPicture() { return picture;...

JPA ID生成策略示例

JPA教程 – JPA ID生成策略示例 当使用id字段的自动生成值时,我们可以选择生成策略。我们使用的一个常见策略是IDENTITY。 例子 以下代码来自Professor.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Professor { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; 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(); } } 以下代码来自App.java。 package cn.w3cschool.common; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) {...

JPA ID表生成器示例

JPA教程 – JPA ID表生成器示例 我们可以使用一个表作为id生成表。 以下代码使用@TableGenerator创建表并设置id值的初始值。 然后它使用表来在@GeneratedValue注释中生成值。 @TableGenerator( name = "Address_Gen", table = "ID_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "Addr_Gen", initialValue = 10000, allocationSize = 100) @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "Address_Gen") private long id; 例子 下面的代码来自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(){ Person p1 = new Person("Tom"); p1.setName("Tom"); Department d = new Department(); d.setName("Design"); p1.setDepartment(d); em.persist(p1); em.persist(d); } @PersistenceContext private EntityManager em; } 下面的代码来自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.TableGenerator; @Entity public class Person { @TableGenerator(name = "Address_Gen", table = "ID_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "Addr_Gen", initialValue = 10000, allocationSize = 100) @Id...

JPA ID序列生成器示例

JPA教程 – JPA ID序列生成器示例 我们可以使用序列为数据库中的实体生成id。 以下代码显示了如何使用序列来进行id生成。它首先使用SequenceGenerator从序列创建序列生成器,那么它在@GeneratedValue注释中标记序列生成器的名称。 @SequenceGenerator(name="Emp_Gen", sequenceName="Emp_Seq") @Id @GeneratedValue(generator="Emp_Gen") private int id; 例子 以下代码来自Professor.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.SequenceGenerator; @Entity public class Professor { @SequenceGenerator(name="Emp_Gen", sequenceName="Emp_Seq") @Id @GeneratedValue(generator="Emp_Gen") private int id; private String name; private long salary; 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(); } } 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import javax.persistence.EntityManager;...

JPA ID自动生成器示例-国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码

JPA ID自动生成器示例

JPA教程 – JPA ID自动生成器示例 我们可以将id字段标记为自动生成的主键列。 数据库将在插入时自动为id字段生成一个值数据到表。 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Person { @Id @GeneratedValue private Long id; private String name; private String surname; public Person() {} public Person(String name, String surname) { this.name = name; this.surname = surname; } 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; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", surname=" + surname + "]"; } }...