【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..."); } }