自定义类型

在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();
}
}

源码地址

https://github.com/je-ge/jpa

如果觉得我的文章对您有帮助,请打赏支持。您的支持将鼓励我继续创作!谢谢!



JPA 系列教程14-自定义类型-@Embedded+@Embeddable的更多相关文章

  1. JPA 系列教程13-复合主键-@EmbeddedId+@Embeddable

    复合主键 指多个主键联合形成一个主键组合 需求产生 比如航线一般是由出发地及目的地确定,如果要确定唯一的航线就可以用出发地和目的地一起来表示 ddl语句 同复合主键-2个@Id和复合主键-2个@Id+ ...

  2. React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发

    React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发   2016/09/23 |  React Native技术文章 |  Sky丶清|  4 条评论 |  1 ...

  3. Json.Net系列教程 2.Net类型与JSON的映射关系

    原文 Json.Net系列教程 2.Net类型与JSON的映射关系 首先谢谢大家的支持和关注.本章主要介绍.Net类型与JSON是如何映射的.我们知道JSON中类型基本上有三种:值类型,数组和对象.而 ...

  4. JPA 系列教程5-双向一对多

    双向一对多的ddl语句 同单向多对一,单向一对多表的ddl语句一致 Product package com.jege.jpa.one2many; import javax.persistence.En ...

  5. JPA 系列教程4-单向一对多

    JPA中的@OneToMany @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface OneToMany { Class tar ...

  6. JPA 系列教程3-单向多对一

    JPA中的@ManyToOne 主要属性 - name(必需): 设定"many"方所包含的"one"方所对应的持久化类的属性名 - column(可选): 设 ...

  7. Spring 系列教程之自定义标签的解析

    Spring 系列教程之自定义标签的解析 在之前的章节中,我们提到了在 Spring 中存在默认标签与自定义标签两种,而在上一章节中我们分析了 Spring 中对默认标签的解析过程,相信大家一定已经有 ...

  8. JPA 系列教程21-JPA2.0-@MapKeyColumn

    @MapKeyColumn 用@JoinColumn注解和@MapKeyColumn处理一对多关系 ddl语句 CREATE TABLE `t_employee` ( `id` bigint(20) ...

  9. JPA 系列教程17-继承-独立表-TABLE_PER_CLASS

    PerTable策略 每个具体的类一个表的策略 举例 这种映射策略每个类都会映射成一个单独的表,类的所有属性,包括继承的属性都会映射成表的列. 这种映射策略的缺点是:对多态关系的支持有限,当查询涉及到 ...

随机推荐

  1. java web开发环境搭建:jdk1.8+eclipse+tomcat8.0

    一.安装JDK 1.下载jdk-8-windows-i586.exe 2.双击安装(可以将安装路径选择到自己喜欢的地方) 3.配置环境变量 1.我的电脑->2.右键单击-> 然后将%JAV ...

  2. 安卓工程中定义的app_name等报错解决办法 工程上有叹号

    类似于"app_name" is not translated in af, am, ar, be, bg, ca, cs, da, de, el, en-rGB, es, es- ...

  3. php笔记(七)PHP类于对象之多态

    <?php interface ICanEat{ public function eat($food);} class Human implements ICaneat{ public func ...

  4. HDU 5073 Galaxy (2014 Anshan D简单数学)

    HDU 5073 Galaxy (2014 Anshan D简单数学) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5073 Description G ...

  5. Array对象 识记

    1.Array 创建 new Array(); new Array(size); new Array(element0, element1, ..., elementn); 2.Array 对象属性 ...

  6. libev中timer时间事件监控器

    1.数据结构 #define ev_at(w) ((WT)(w))->at#define ev_active(w) ((W)(w))->active typedef ev_watcher_ ...

  7. Python基础篇-day11 - 协程

    本节主要内容: 1.Gevent协程2.Select\Poll\Epoll异步IO与事件驱动3.RabbitMQ队列 1.Gevent协程 1.1协程的好处 无需线程上下文切换的开销无需原子操作锁定及 ...

  8. Golang--计算文件的MD5值

    参考: http://blog.csdn.net/u014029783/article/details/53762363 用法: $ go run 01.go -f 1.txt b9d228f114d ...

  9. Hdu 3363 Ice-sugar Gourd(对称,圆)

    Ice-sugar Gourd Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  10. HDU 5855 Less Time, More profit

    二分t+最大权闭合图. 很显然二分那个t作为limit.每一个limit下,有一些边不能用了,然后要知道这种情况下怎么选点获得的价值最大. 这么想:一个shop想获得收益,就必须选择某一些plant, ...