JavaEE(13) - JPA属性映射
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.*; @Embeddable
public 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)
JavaEE(13) - JPA属性映射的更多相关文章
- JavaEE(14) - JPA关联映射
1. 单向N-1关联映射 2. 单向1-1关联映射 3. 单向1-N关联映射 4. 单向N-N关联映射 5. 双向1-1关联映射 6. 双向1-N关联映射 7. 双向N-N关联映射 1. 单向N-1关 ...
- Touch 方法&属性 映射工具
Touch 方法&属性 映射工具(0.5 版本) 标签 : github 线上后门与接口调试: 原先需要测试一个接口(如Dubbo.DAO), 或为线上留后门, 需要写大量的Web层(Api. ...
- 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
原文链接:https://www.entityframeworktutorial.net/code-first/configure-property-mappings-using-fluent-api ...
- 【EF】Entity Framework实现属性映射约定
Entity Framework Code First属性映射约定中“约定”一词,在原文版中为“Convention”,翻译成约定或许有些不好理解,这也是网上比较大多数的翻译,我们就当这是Entity ...
- JavaEE高级-JPA学习笔记
*JPA概述 *JPA是什么? - Java Persistence API :用于对象持久化的API - Java EE 5.0平台标准的ORM规范,使得应用程序以统一的方式访问持久化层 - JPA ...
- Entity Framework实现属性映射约定
Entity Framework Code First属性映射约定中“约定”一词,在原文版中为“Convention”,翻译成约定或许有些不好理解,这也是网上比较大多数的翻译,我们就当这是Entity ...
- Entity Framework Code First属性映射约定
Entity Framework Code First与数据表之间的映射方式有两种实现:Data Annotation和Fluent API.本文中采用创建Product类为例来说明tity Fram ...
- Hibernate持久化类属性映射
Hibernate充当应用程序和数据库之间的中间件,实现二者之间的交互操作,他对JDBC进行了封装,以完全面向对象的方式来操作数据. 适用于有多个数据源的情况下,不必去考虑不同数据源的操作差异. Hi ...
- ReferentialConstraint 中的依赖属性映射到由存储生成的列
ReferentialConstraint 中的依赖属性映射到由存储生成的列 这个问题是由于从表中的外键关系建立错误(可能是由于误改),查看从表的所有外键关系,即可找到问题所在. 问题: 什么是从表? ...
随机推荐
- 《深入了解Android:Wi-Fi、NFC和GPS音量》勘误表
资源下载更新(我不知道为什么被115网盘屏蔽) 深入了解android它wifi-nfc-gps 115网盘礼包码:5lbee5qn4g5ghttp://115.com/lb/5lbee5qn4g5g ...
- 辛格尔顿和Android
辛格尔顿(Singleton) .singleton.h,定义类的基本成员及接口 #ifndef SINGLETON_H_INCLUDE #define SINGLETON_H_INCLUDE cla ...
- C++ string类取字符串的左右子串(以特定子串为分界限)
// Example3.cpp : 定义控制台应用程序的入口点. //以特定单词为分界,求取字符串的左右子串 #include "StdAfx.h" #include <st ...
- CSDN markdown 编辑 三 基本语法
这是一个副本,在准备好了help档,另外补充一些他们自己的解释. 这一次是不说话UML和LaTEX. 准备抽两篇文章专门谈. 1. 标题和字体大小 markdown字号太少了.觉得应该再加些. 在文字 ...
- hdu 2049 别easy列(4)——测试新郎
问题: 使用double定义的数量和long数定义19-20出现分歧,原因不明.求大公社. 这个问题需要用long,否则,只是通过,这应该纠结了很久. 问题是乘以一个交错的思想相结合. 不easy系列 ...
- IE7,8,9兼容性处理
在IE7根据假设高度文本框中设置,则光标将不会被中心的方法如以下: 添加属性,如:style="line-height:32px\9";能够 假设一个页面有多个TAB交换的物品.而 ...
- NOI 评价体系 arbiter 安装方法 常见的问题 移植
#!/bin/bash AppPath="$PWD" 读取当前文件夹 echo "Arbiter is installing..." sudo apt-ge ...
- ODPS 下一个map / reduce 准备
阿里接到一个电话说练习和比赛智能二选一, 真的很伤心, 练习之前积极老龄化的权利. 要总结ODPS下一个 写map / reduce 并进行购买预测过程. 首先这里的hadoop输入输出都是表的形式, ...
- 《图书管理系统——java》
/* (程序头部凝视開始) * 程序的版权和版本号声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名: < ...
- 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...