【hibernate】自定义转换器
【hibernate】自定义转换器
转载:https://www.cnblogs.com/yangchongxing/p/10398255.html
1、转换基本属性
package cn.ycx.study.hibernate.bean; import java.math.BigDecimal;
import java.util.Currency; public class Money {
public static final String SPLIT_SYMBOL = " ";
protected BigDecimal value;
protected Currency currency;
public Money(BigDecimal value, Currency currency) {
this.value = value;
this.currency = currency;
}
public BigDecimal getValue() {
return value;
}
public void setValue(BigDecimal value) {
this.value = value;
}
public Currency getCurrency() {
return currency;
}
public void setCurrency(Currency currency) {
this.currency = currency;
}
@Override
public String toString() {
return getValue().toString() + SPLIT_SYMBOL + getCurrency();
}
public static Money fromString(String s) {
String[] split = s.split(SPLIT_SYMBOL);
return new Money(new BigDecimal(split[0]), Currency.getInstance(split[1]));
}
}
转换器实现
package cn.ycx.study.hibernate.converter;
import javax.persistence.AttributeConverter;
import cn.ycx.study.hibernate.bean.Money;
public class MoneyConverter implements AttributeConverter<Money, String> {
@Override
public String convertToDatabaseColumn(Money attribute) {
return attribute.toString();
}
@Override
public Money convertToEntityAttribute(String dbData) {
return Money.fromString(dbData);
}
}
实体对象
package cn.ycx.study.hibernate.entity; import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import cn.ycx.study.hibernate.bean.Money;
import cn.ycx.study.hibernate.converter.MoneyConverter;
@Entity
@org.hibernate.annotations.DynamicInsert
@org.hibernate.annotations.DynamicUpdate
public class User {
@Id
@GeneratedValue(generator="id_generator")
protected long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Convert(converter = MoneyConverter.class,disableConversion=false)
protected Money money;
public Money getMoney() {
return money;
}
public void setMoney(Money money) {
this.money = money;
}
}
测试
@Test
public void testInsert() {
User u = new User();
u.setMoney(new Money(new BigDecimal(10), Currency.getInstance(Locale.CHINA)));
this.session.persist(u);
assertTrue( true );
}
2、转换组件的属性
package cn.ycx.study.hibernate.entity; import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; import cn.ycx.study.hibernate.bean.Money;
import cn.ycx.study.hibernate.converter.MoneyConverter;
import cn.ycx.study.hibernate.converter.ZipcodeConverter;
@Entity
@org.hibernate.annotations.DynamicInsert
@org.hibernate.annotations.DynamicUpdate
public class User {
@Id
@GeneratedValue(generator="id_generator")
protected long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
//转换基本属性
@Convert( converter = MoneyConverter.class, disableConversion = false )
protected Money money;
public Money getMoney() {
return money;
}
public void setMoney(Money money) {
this.money = money;
}
//转换组件属性
@Convert( converter = ZipcodeConverter.class, attributeName = "zipcode")
protected Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
attributeName 声明了可嵌入 Address 类的 zipcode 属性。这一设置支持圆点属性路径,如果 zipcode 是 可嵌入 City 的属性,则要使用嵌套路径 city.zipcode 类引用它
组件
package cn.ycx.study.hibernate.entity;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull; import cn.ycx.study.hibernate.bean.Zipcode;
@Embeddable
public class Address {
@NotNull
protected String street;
@NotNull
protected Zipcode zipcode;
@NotNull
protected City city;
public Address() {
}
public Address(String street, Zipcode zipcode, City city) {
this.street = street;
this.zipcode = zipcode;
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public Zipcode getZipcode() {
return zipcode;
}
public void setZipcode(Zipcode zipcode) {
this.zipcode = zipcode;
}
public City getCity() {
return city;
}
public void setCity(City city) {
this.city = city;
}
}
package cn.ycx.study.hibernate.bean;
public abstract class Zipcode {
protected String symbol;
protected String value;
public Zipcode(String symbol, String value) {
this.symbol = symbol;
this.value = value;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package cn.ycx.study.hibernate.bean;
public class ChinaZipcode extends Zipcode {
public ChinaZipcode(String symbol, String value) {
super(symbol, value);
}
}
package cn.ycx.study.hibernate.bean;
public class UsaZipcode extends Zipcode {
public UsaZipcode(String symbol, String value) {
super(symbol, value);
}
}
转换器
package cn.ycx.study.hibernate.converter; import javax.persistence.AttributeConverter; import cn.ycx.study.hibernate.bean.ChinaZipcode;
import cn.ycx.study.hibernate.bean.UsaZipcode;
import cn.ycx.study.hibernate.bean.Zipcode; public class ZipcodeConverter implements AttributeConverter<Zipcode, String> { @Override
public String convertToDatabaseColumn(Zipcode attribute) {
return attribute.getValue();
} @Override
public Zipcode convertToEntityAttribute(String dbData) {
if (dbData.length() == 6) {
return new ChinaZipcode("CN", dbData);
} else if (dbData.length() == 5) {
return new UsaZipcode("US", dbData);
}
throw new IllegalArgumentException("Unsupported zipcode in database...");
} }
【hibernate】自定义转换器的更多相关文章
- spring mvc 自定义转换器
<!-- 注册转化器 --> <mvc:annotation-driven conversion-service="conversionService" /> ...
- Hibernate自定义主键
Hibernate自定义主键,通过此方法可以解决一此特殊的主键ID,在了解自定义主键时,先了解下Hibernate有自带的10种生成主键方法. 1) assigned主键由外部程序负责生成,无需Hib ...
- Struts2 请求数据的自动封装 及 自定义转换器类
请求数据自动封装: 实现原理:使用了参数拦截器.struts-default.xml中 <interceptor name="params" class="com. ...
- Flask自定义转换器,实现路由匹配正则表达式参数
Flask框架动态路由实现参数传递和Django框架有类似之处,但是相比于Django框架,Flask实现复杂的参数就需要自己自定义转换器来实现了,而不能向Django那样直接使用正则表达式 # 路由 ...
- struts2自定义转换器
Struts2自定义类型转换器分为局部类型转换器和全局类型转换器 (1)局部类型转换器 如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用d ...
- Spring -- 自定义转换器
Spring 定义了 3 种类型的转换器接口,实现任意一个转换器接口都可以作为自定义转换器注册到 ConversionServiceFactoryBean 中: Converter<S,T> ...
- flask自定义转换器
根据具体的需求,有些时候是需要用到正则来灵活匹配URL,但是Flask的路由匹配机制是不能直接在路由里直接写正则的,这时候就需要使用转换器! Flask的默认转换器: DEFAULT_CONVERTE ...
- Retrofit 2.0基于OKHttp更高效更快的网络框架 以及自定义转换器
时间关系,本文就 Retrofit 2.0的简单使用 做讲解 至于原理以后有空再去分析 项目全面.简单.易懂 地址: 关于Retrofit 2.0的简单使用如下: https://gitee.c ...
- 一、数据库表中字段的增删改查,二、路由基础.三、有名无名分组.四、多app共存的路由分配.五、多app共存时模板冲突问题.六、创建app流程.七、路由分发.八、路由别名,九、名称空间.十、反向解析.十一、2.x新特性.十二、自定义转换器
一.数据库表中字段的增删改查 ''' 直接在modules中对字段进行增删改查 然后在tools下点击Run manage.py Task执行makemigrations和migrate 注意在执行字 ...
随机推荐
- React组件间的通讯
组件化开发应该是React核心功能之一,组件之间的通讯也是我们做React开发必要掌握的技能.接下来我们将从组件之间的关系来分解组件间如何传递数据. 1.父组件向子组件传递数据 通讯是单向的,数据必须 ...
- 力扣(LeetCode)Excel表列名称 个人题解
给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> ...
- scipy.sparse的csc_matrix、csr_matrix与coo_matrix区别与应用(思维导图)
- 菜鸟系列Fabric源码学习—orderer服务启动
Fabric 1.4 orderer 服务启动流程 1.提要 orderer提供broadcast和deliver两个服务接口.orderer节点与各个peer节点通过grpc连接,orderer将所 ...
- js 根据指定的多个索引,删除相应的数组元素。splice + sort
更新于2018-04-19 var productItems = ["a", "b", "c", "d"]; var i ...
- 2019-10-16:渗透测试,基础学习,burpsuit学习,爆破的四种方式学习
Burp Suite 是用于攻击web 应用程序的集成平台,包含了许多工具.Burp Suite为这些工具设计了许多接口,以加快攻击应用程序的过程.所有工具都共享一个请求,并能处理对应的HTTP 消息 ...
- sublimetext使用教程
图片来自网络,仅供参考 前言 随着我们编写的代码越来越复杂,DevC++以不再能满足我们的需求,所以,我们需要 一个能够进行调试,编译,运行等等功能的现代化ide,sublimetext(以下简称ST ...
- mysql 忘记登录密码(修改root密码)
1.以管理员身份打开cmd,键入net stop mysql,停止mysql 2.切换到mysql的安装目录下(例:S:\mysql\mysql-8.0.18-winx64\mysql-8.0.18- ...
- 带着canvas去流浪系列之三 绘制饼图
[摘要] 用canvas原生API绘制Echarts图表 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 任务说明 使用原生canvasAPI绘制 ...
- 失去循环标签的Python,我这样实现跳出外层循环
不完美的Python 自从各类Python大火,感觉天上地下哪儿都有Python的一席之地,Python功夫好啊-但python有些细节上缺少其他语言的便利.今天我们就来举几个例子. 跳出外层循环 大 ...