1. 映射实体的属性
#1. 使用@Transient修饰不想持久保存的Field
#2. 使用@Enumerated修饰枚举类型的Field
#3. 使用@Lob, @Basic修饰枚举类型的Field
#4. 使用@Temporal修饰枚举类型的Field
2. 将实体映射到多个数据表
3. 映射复合类型的属性
4. 映射实体的主键
#1. IdClass
#2. EmbeddedId
------------------------------------------------
1. 映射实体的属性
#1. 使用@Transient修饰不想持久保存的Field(Net Beans创建Java Project, 项目名称:Transient)
News.java
package org.crazyit.model;import javax.persistence.*;@Entity@Table(name = "news_table")public class News { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "news_title", length = 50) private String title; @Transient private String content; public News() { }
// getter // setter
}
persistence.xml(参见12.2)
JpaQs.java(参见12.2)
#2. 使用@Enumerated修饰枚举类型的Field(Net Beans创建Java Project, 项目名称:Enumerated)
Season.java
package org.crazyit.model;public enum Season { Spring,Summer,Autumn,Winter}
News.java
package org.crazyit.model;import javax.persistence.*;@Entity@Table(name = "news_table")public class News { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "news_title", length = 50) private String title; private String content; @Enumerated(EnumType.ORDINAL) @Column(name = "happen_season") private Season happenSeason; public News() { } public News(int id, String title, String content, Season happenSeason) { this.id = id; this.title = title; this.content = content; this.happenSeason = happenSeason; } // getter // setter }
persistence.xml(参见12.2)
JpaQs.java(参见12.2)
#3. 使用@Lob, @Basic修饰枚举类型的Field(Net Beans创建Java Project, 项目名称:Lob)
Person.java
package org.crazyit.model;import javax.persistence.*;@Entity@Table(name = "person_table")public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "person_name", length = 50) private String name; @Lob @Basic(fetch = FetchType.LAZY) private byte[] pic; public Person() { } public Person(int id, String name, byte[] pic) { this.id = id; this.name = name; this.pic = pic; } // setter // getter}
persistence.xml(参见12.2)
JpaQs.java
package lee;import javax.persistence.*;import org.crazyit.model.*;import java.io.*;public class JpaQs { private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("qs"); public static void main(String[] args) throws IOException { final EntityManager em = emf.createEntityManager(); File file = new File("img.png"); byte[] content = new byte[(int) file.length()]; new FileInputStream(file).read(content); Person person = new Person(); person.setName("crazyit.org"); person.setPic(content); try { em.getTransaction().begin(); em.persist(person); em.getTransaction().commit(); } finally { em.close(); } }}
#4. 使用@Temporal修饰枚举类型的Field(Net Beans创建Java Project, 项目名称:Temporal)
Person.java
package org.crazyit.model;import javax.persistence.*;import java.util.*;@Entity@Table(name = "person_table")public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "person_name", length = 50) private String name; @Temporal(TemporalType.DATE) private Date birth; public Person() { } public Person(int id, String name, Date birth) { this.id = id; this.name = name; this.birth = birth; } // setter // getter}
persistence.xml(参见12.2)
JpaQs.java(参见12.2)
2. 将实体映射到多个数据表(Net Beans创建Java Project, 项目名称:SecondaryTable)
Person.java
package org.crazyit.model;import javax.persistence.*;@Entity@Table(name = "person_table")@SecondaryTable(name = "person_detail", pkJoinColumns = @PrimaryKeyJoinColumn(name = "person_id"))public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "person_name", length = 50) private String name; @Column(table = "person_detail", name = "email") private String email; @Column(table = "person_detail", name = "phone") private String phone; public Person() { } public Person(int id, String name, String email, String phone) { this.id = id; this.name = name; this.email = email; this.phone = phone; } // setter // getter}
persistence.xml(参见12.2)
JpaQs.java(参见12.2)
3. 映射复合类型的属性(Net Beans创建Java Project, 项目名称:Embedded)
Person.java
package org.crazyit.model;import javax.persistence.*;@Entity@Table(name = "person_table")public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "person_name", length = 50) private String name; @Column(name = "person_email") private String email; @Embedded @AttributeOverrides({ @AttributeOverride(name = "name", column = @Column(name = "cat_name", length = 35)), @AttributeOverride(name = "color", column = @Column(name = "cat_color")) }) private Cat cat; public Person() { } public Person(int id, String name, String email, Cat cat) { this.id = id; this.name = name; this.email = email; this.cat = cat; } // setter // getter}
Cat.java
package org.crazyit.model;import javax.persistence.*;@Embeddablepublic class Cat { private String name; private String color; public Cat() { } public Cat(String name, String color) { this.name = name; this.color = color; } // setter // getter}
persistence.xml(参见12.2)
JpaQs.java(参见12.2)
4. 映射实体的主键
#1. IdClass (Net Beans创建Java Project, 项目名称:IdClass)
Person.java
package org.crazyit.model;import javax.persistence.*;@Entity@Table(name = "person_table")@IdClass(Name.class)public class Person { //两个@Id定义了联合主键 @Id private String first; @Id private String last; @Column(name = "person_email") private String email; public Person() { } public Person(String first, String last, String email) { this.first = first; this.last = last; this.email = email; } // setter // getter}
Name.java
package org.crazyit.model;import javax.persistence.*;public class Name implements java.io.Serializable { private String first; private String last; public Name() { } public Name(String first, String last) { this.first = first; this.last = last; } // setter // getter public boolean equals(Object obj) { if (this == obj) { return true; } if (obj.getClass() == Name.class) { Name target = (Name) obj; if (target.getFirst().equals(first) && target.getLast().equals(last)) { return true; } } return false; } public int hashCode() { return first.hashCode() + last.hashCode() * 17; }}
persistence.xml(参见12.2)
JpaQs.java(参见12.2)
#2. EmbeddedId (Net Beans创建Java Project, 项目名称:EmbeddedId)
Person.java
package org.crazyit.model;import javax.persistence.*;@Entity@Table(name = "person_table")public class Person { @EmbeddedId @AttributeOverrides({ @AttributeOverride(name = "first", column = @Column(name = "person_first")), @AttributeOverride(name = "last", column = @Column(name = "person_last", length = 20)) }) private Name name; @Column(name = "person_email") private String email; public Person() { } public Person(Name name, String email) { this.name = name; this.email = email; } // setter // getter}
Name.java(参见#1)
persistence.xml(参见12.2)
JpaQs.java(参见12.2)