【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 注意在执行字 ...
随机推荐
- requests模拟登陆的三种方式
###获取登录后的页面三种方式: 一.实例化seesion,使用seesion发送post请求,在使用他获取登陆后的页面 import requests session = requests.sess ...
- 【SSM Spring 线程池 OJ】 使用Spring线程池ThreadPoolTaskExecutor
最近做的Online Judge项目,在本地判题的实现过程中,遇到了一些问题,包括多线程,http通信等等.现在完整记录如下: OJ有一个业务是: 用户在前端敲好代码,按下提交按钮发送一个判题请求给后 ...
- sqlcipher的php扩展运行在fast-cgi:php-fpm下工作不正常
今天发现了这样的问题,php-fpm运行sqlcipher时,有些数据库工作正常,有些却不正常. 不正常的,都在日志上报错,也就是php处理异常了. 这个报错发生在执行sql语句时,通常就是sqlci ...
- opencv 3 core组件进阶(2 ROI区域图像叠加&图像混合;分离颜色通道、多通道图像混合;图像对比度,亮度值调整)
ROI区域图像叠加&图像混合 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp&g ...
- 从零开始入门 | Kubernetes 中的服务发现与负载均衡
作者 | 阿里巴巴技术专家 溪恒 一.需求来源 为什么需要服务发现 在 K8s 集群里面会通过 pod 去部署应用,与传统的应用部署不同,传统应用部署在给定的机器上面去部署,我们知道怎么去调用别的机 ...
- 【笔记】总结Springboot和Vue前后端分离的跨域问题
跨域一直是个很玄学的问题,SSM的时候又得前后端一起配置,sb的时候又不用. 前端 axios普通get请求 submitForm() { var v=this; this.$axios({ meth ...
- Windows Server 2008 服务器重启后卡死在Windows Update 页面问题处理
Windows Update 服务器 服务器是联想RD640 操作系统Windows Server 2008 R2 Enterprise版 补丁版本是SP1 远程windows服务器时,一直处于远程建 ...
- Java方法的可变参数
class Demo { public static int sum(int ... data) { //此处可以传递一个数组,也可以是多个参数 int sum = 0; for (int i : d ...
- 两个对象值相同(x.equals(y)==true),但却可有不同的hashcode这句话对吗?
1.这句话当然不对啦,请参看官方文档给出的解释! hashCode public int hashCode()返回该对象的哈希码值.支持此方法是为了提高哈希表(例如 java.util.Hashtab ...
- MySql计算字段的长度
用户账号有用中文字符,查找所有含有中文字符的账号 SELECT member_name FROM table_member WHERE length(member_name)!=char_length ...