【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】自定义转换器的更多相关文章

  1. spring mvc 自定义转换器

    <!-- 注册转化器 --> <mvc:annotation-driven conversion-service="conversionService" /> ...

  2. Hibernate自定义主键

    Hibernate自定义主键,通过此方法可以解决一此特殊的主键ID,在了解自定义主键时,先了解下Hibernate有自带的10种生成主键方法. 1) assigned主键由外部程序负责生成,无需Hib ...

  3. Struts2 请求数据的自动封装 及 自定义转换器类

    请求数据自动封装: 实现原理:使用了参数拦截器.struts-default.xml中 <interceptor name="params" class="com. ...

  4. Flask自定义转换器,实现路由匹配正则表达式参数

    Flask框架动态路由实现参数传递和Django框架有类似之处,但是相比于Django框架,Flask实现复杂的参数就需要自己自定义转换器来实现了,而不能向Django那样直接使用正则表达式 # 路由 ...

  5. struts2自定义转换器

    Struts2自定义类型转换器分为局部类型转换器和全局类型转换器 (1)局部类型转换器 如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用d ...

  6. Spring -- 自定义转换器

    Spring 定义了 3 种类型的转换器接口,实现任意一个转换器接口都可以作为自定义转换器注册到 ConversionServiceFactoryBean 中: Converter<S,T> ...

  7. flask自定义转换器

    根据具体的需求,有些时候是需要用到正则来灵活匹配URL,但是Flask的路由匹配机制是不能直接在路由里直接写正则的,这时候就需要使用转换器! Flask的默认转换器: DEFAULT_CONVERTE ...

  8. Retrofit 2.0基于OKHttp更高效更快的网络框架 以及自定义转换器

    时间关系,本文就 Retrofit 2.0的简单使用 做讲解  至于原理以后有空再去分析 项目全面.简单.易懂  地址: 关于Retrofit 2.0的简单使用如下:  https://gitee.c ...

  9. 一、数据库表中字段的增删改查,二、路由基础.三、有名无名分组.四、多app共存的路由分配.五、多app共存时模板冲突问题.六、创建app流程.七、路由分发.八、路由别名,九、名称空间.十、反向解析.十一、2.x新特性.十二、自定义转换器

    一.数据库表中字段的增删改查 ''' 直接在modules中对字段进行增删改查 然后在tools下点击Run manage.py Task执行makemigrations和migrate 注意在执行字 ...

随机推荐

  1. 微调(Fine-tune)原理

    在自己的数据集上训练一个新的深度学习模型时,一般采取在预训练好的模型上进行微调的方法.什么是微调?这里已VGG16为例进行讲解,下面贴出VGGNet结构示意图. 上面圈出来的是VGG16示意图,也可以 ...

  2. ubuntu 16.04上源码编译dlib教程 | compile dlib on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/c6ead512/,欢迎阅读! compile dlib on ubuntu 16.04 Series Part 1: compil ...

  3. proxy protocol

    Proxy protocol 是haproxy 作者开发和设计的一个inernet 协议, 用于获取客户端的IP地址. 在使用7层代理是可以向http协议添加X-Forword-For来实现,而4层协 ...

  4. node后台初始配置(2)

    一.node-app结构 创建成功node-app项目后,会自动生成一些文件一般初始的结构如下图 在bin文件夹里面只有一个文件www      var port = normalizePort(pr ...

  5. /etc/security/limits.conf 详解与配置

    目录 一. /etc/security/limits.conf 详解 /etc/security/limits.conf 配置解析 /etc/security/limits.d/ 目录 二. ulim ...

  6. Altium Designer 18 画keepout层与将keepout层转换成Mechanical1层的方法

    画keepout的方法 先选中Keepout层:然后 右键->Place->Keepout->然后选择要画圆还是线 Keepout层一般只用来辅助Layout,不能作为PCB的外形结 ...

  7. [ch04-03] 用神经网络解决线性回归问题

    系列博客,原文在笔者所维护的github上:https://aka.ms/beginnerAI, 点击star加星不要吝啬,星越多笔者越努力. 4.3 神经网络法 在梯度下降法中,我们简单讲述了一下神 ...

  8. linux下创建mysql用户和数据库,并绑定

    Linux下输入命令: mysql -uroot -proot123 进入mysql后输入: 查看目前有哪些数据库存在:mysql> SHOW DATABASES; 创建数据库:create s ...

  9. 如何在导航条的button点击变换时,切换对应的控制器

    1.导航条内的button被点击 切换对应的控制器 让控制器作为调航条的代理 1.定义代理 2.遵循代理协议 3.实现代理 4.在合适的地方调用代理    当按钮被点击的时候切换控制器

  10. cesium添加多个geojson文件并分别控制显示和隐藏

    /*获取geojson数据*/ function get_geojson(name,h,n){ let x=document.getElementById(n); if(x.className === ...