自定义类型

在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. 关于Python2字符编码的体会

    对于Python的字符编码问题也懵了很久,最近做爬虫多次遇到网页转码的问题,干脆彻底解决掉!Just Do it! 1.两种类型str与unicode str和unicode都是basestring的 ...

  2. git在webstorm中的使用

    打开webstorm新建项目,这里新建的项目名称我起为lianxi 打开设置选项里的插件栏 搜索gitignore,并安装,我这里已安装,所以显示X,没有安装的会显示一个绿色的下载箭头.安装完后需要重 ...

  3. spring定时任务的几种实现方式

    Spring定时任务的几种实现 近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将 ...

  4. label 不同颜色

    label  不同颜色 UILabel* noteLabel = [[UILabel alloc] init]; noteLabel.frame = CGRectMake(60, 100, 200, ...

  5. LINUX利用Speedtest测速

    那么远程服务器呢?要知道大多数远程服务器是没有浏览器可以打开web页面的.用浏览器打开网页测速的瓶颈就在此,你不能按计划的对服务器进行定期的常规测试.这时需要到一个名为Speedtest-cli的软件 ...

  6. Quartz的cron表达式

    一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素. 按顺序依次为 秒(0~59) 分钟(0~59) 小时(0~23) 天(月)(0~31,但是你需要考虑你月的天数) 月(0~11) 天( ...

  7. assembly打包实例

    1.先在pom.xml文件中添加assembly打包插件 <build> <plugins> <plugin> <groupId>org.apache. ...

  8. Windows Server 2008 如何在IIS中添加MIME类型

    用户可以通过使用MIME以设置服务器传送多媒体文件,如声音和视频等.MIME是一种技术规范,现在可以用于浏览器上,传送可以供浏览器识别的信息 如果我们的网站提供下载服务,有时传上去的文件比如 xxx. ...

  9. python join()阻塞的用法

    join()阻塞的用法,用来检测线程有没有完全执行完毕 #!/usr/bin/env python#-*- coding:utf-8 -*-import threadingimport time de ...

  10. VS2013中使用QT插件后每次重新编译问题

    环境 系统:win7 64位旗舰版 软件:VS2013.QT5.5.1-32位.Qt5 Visual Studio Add-in1.2.4 概述 使用QT Visual Studio插件打开pro项目 ...