博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaEE(13) - JPA属性映射
阅读量:6073 次
发布时间:2019-06-20

本文共 7775 字,大约阅读时间需要 25 分钟。

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)

转载于:https://www.cnblogs.com/thlzhf/p/4249680.html

你可能感兴趣的文章
Linux 配置vnc,开启linux远程桌面
查看>>
NLog文章系列——如何优化日志性能
查看>>
Hadoop安装测试简单记录
查看>>
CentOS6.4关闭触控板
查看>>
ThreadPoolExecutor线程池运行机制分析-线程复用原理
查看>>
React Native 极光推送填坑(ios)
查看>>
Terratest:一个用于自动化基础设施测试的开源Go库
查看>>
修改Windows远程终端默认端口,让服务器更安全
查看>>
扩展器必须,SAS 2.0未必(SAS挺进中端存储系统之三)
查看>>
Eclipse遇到Initializing Java Tooling解决办法
查看>>
while((ch = getchar()) != '\n')
查看>>
好程序员web前端分享JS检查浏览器类型和版本
查看>>
Linux 安装oracle内核参数
查看>>
Oracle DG 逻辑Standby数据同步性能优化
查看>>
exchange 2010 队列删除
查看>>
android实用测试方法之Monkey与MonkeyRunner
查看>>
「翻译」逐步替换Sass
查看>>
H5实现全屏与F11全屏
查看>>
处理excel表的列
查看>>
Excuse me?这个前端面试在搞事!
查看>>