JPA 系列教程14-自定义类型-@Embedded+@Embeddable
自定义类型
在hibernate中实现自定义类型,需要去实现UserType接口即可或者以Component的形式提供。
JPA的@Embedded注解有点类似,通过此注解可以在Entity模型中使用一般的java对象,不过此对象还需要用@Embeddable注解标注。
需求产生
Employee类有一个address属性,
address应该有city,street两个属性,
一般的写法直接在Employee类中写两个属性:
private String city;
private String street;
现在可以用一个Address类来代替此类写法,Address类包含了city和street,如此一来,我们在Employee类只要这样写:
private Address address;
ddl语句
CREATE TABLE `t_employee` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`city` varchar(255) DEFAULT NULL,
`street` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Address
package com.jege.jpa.embeddable;
import javax.persistence.Embeddable;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:实现自定义类型,在hibernate里面需要实现UserType接口
*/
@Embeddable
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
Employee
package com.jege.jpa.embeddable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:@Embedded注解
*/
@Entity
@Table(name = "t_employee")
public class Employee {
@Id
@GeneratedValue
private Long id;
private String name;
@Embedded
private Address address;
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
MainTest
package com.jege.jpa.embeddable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author JE哥
* @email 1272434821@qq.com
* @description:自定义类型-@Embedded+@Embeddable测试
*/
public class MainTest {
private static EntityManagerFactory entityManagerFactory = null;
private EntityManager entityManager = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
entityManagerFactory = Persistence.createEntityManagerFactory("com.jege.jpa");
}
@Before
public void setUp() throws Exception {
entityManager = entityManagerFactory.createEntityManager();
}
@Test
public void persist() {
Address address = new Address();
address.setCity("beijing");
address.setStreet("王府井大街");
Employee employee = new Employee();
employee.setAddress(address);
employee.setName("jege");
entityManager.getTransaction().begin();
entityManager.persist(employee);
entityManager.getTransaction().commit();
}
@After
public void tearDown() throws Exception {
if (entityManager != null && entityManager.isOpen())
entityManager.close();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
if (entityManagerFactory != null && entityManagerFactory.isOpen())
entityManagerFactory.close();
}
}
源码地址
如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!
JPA 系列教程14-自定义类型-@Embedded+@Embeddable的更多相关文章
- JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable
复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...
- React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发
React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发 2016/09/23 | React Native技术文章 | Sky丶清| 4 条评论 | 1 ...
- Json.Net系列教程 2.Net类型与JSON的映射关系
原文 Json.Net系列教程 2.Net类型与JSON的映射关系 首先谢谢大家的支持和关注.本章主要介绍.Net类型与JSON是如何映射的.我们知道JSON中类型基本上有三种:值类型,数组和对象.而 ...
- JPA 系列教程5-双向一对多
双向一对多的ddl语句 同单向多对一,单向一对多表的ddl语句一致 Product package com.jege.jpa.one2many; import javax.persistence.En ...
- JPA 系列教程4-单向一对多
JPA中的@OneToMany @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OneToMany { Class tar ...
- JPA 系列教程3-单向多对一
JPA中的@ManyToOne 主要属性 - name(必需): 设定"many"方所包含的"one"方所对应的持久化类的属性名 - column(可选): 设 ...
- Spring 系列教程之自定义标签的解析
Spring 系列教程之自定义标签的解析 在之前的章节中,我们提到了在 Spring 中存在默认标签与自定义标签两种,而在上一章节中我们分析了 Spring 中对默认标签的解析过程,相信大家一定已经有 ...
- JPA 系列教程21-JPA2.0-@MapKeyColumn
@MapKeyColumn 用@JoinColumn注解和@MapKeyColumn处理一对多关系 ddl语句 CREATE TABLE `t_employee` ( `id` bigint(20) ...
- JPA 系列教程17-继承-独立表-TABLE_PER_CLASS
PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...
随机推荐
- c#如何使两个方法并行运行
static void Main(string[] args) { Parallel.Invoke(Foo, Bar); } static void ...
- NEUQ1038: 谭浩强C语言(第三版)习题4.8
之前没做对的一道题,今天集中清理一下. //------------------- 很水的题,主要是 %.2lf 不能四舍五入,需要仅保留两位小数,用了丑陋的强制类型转换... //--------- ...
- WWW 资源下载与表单提交
在注册与验证用户信息,以及非即时通信的游戏中,我们可以使用WWW类使用短链接来完成客户端与服务器数据的通信,今天我们将使用用POST方法来完成的用户注册与登录,在最后介绍下其它资源的加载. 首先使用P ...
- 关于ios原声嵌入web页面的问题
当在一个界面中既有原生又有web的时候,如果想让上下整体滑动的话,我们怎么确定web的高度呢,下面分享一下我的心得 首先在webView的代理方法中我们可以获取到加载完整个web页面的高度 - (vo ...
- WinForm ListView
今天,我学习了公共控件中的ListView的内容. 首先,在利用ListView布置界面时,有以下三个方面: 1.视图: 在其右上方小箭头点击将视图改为Details:或者右键属 ...
- MySQL千万级数据JDBC插入
案例语句: String sql = "LOAD DATA LOCAL INFILE '" + dataFilepath + "' into table " + ...
- 拦截QWebView弹出窗口
环境 系统:win7 64位旗舰版 软件:VS2013.QT5.5.1-32位 概述 当网页打开一个新的窗口时,我们有时候需要根据URL地址来判断弹出窗口是否合理,如果合理则弹出新窗口,否则不弹出.本 ...
- NIC Bonding: 2 nic port as 1 interface
The following is concluded from here. Consider we have 2 interfaces: eth0 & eth1 bond the two in ...
- 利用poi向excle写入数据
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import org ...
- Android中使用OKHttp上传图片,从相机和相册中获取图片并剪切
效果:注意:1:网络权限<;;;); intent.putExtra(); ); intent.putExtra(); intent.putExtra(, byteArrayOutputStre ...