一、结构

二、代码

1.

 package org.jpwh.model.advanced;

 import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Currency; /*
This value-typed class should be <code>java.io.Serializable</code>: When Hibernate stores entity
instance data in the shared second-level cache (see <a href="#Caching"/>), it <em>disassembles</em>
the entity's state. If an entity has a <code>MonetaryAmount</code> property, the serialized
representation of the property value will be stored in the second-level cache region. When entity
data is retrieved from the cache region, the property value will be deserialized and reassembled.
*/
public class MonetaryAmount implements Serializable { /*
The class does not need a special constructor, you can make it immutable, even with
<code>final</code> fields, as your code will be the only place an instance is created.
*/
protected final BigDecimal value;
protected final Currency currency; public MonetaryAmount(BigDecimal value, Currency currency) {
this.value = value;
this.currency = currency;
} public BigDecimal getValue() {
return value;
} public Currency getCurrency() {
return currency;
} /*
You should implement the <code>equals()</code> and <code>hashCode()</code>
methods, and compare monetary amounts "by value".
*/
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MonetaryAmount)) return false; final MonetaryAmount monetaryAmount = (MonetaryAmount) o; if (!value.equals(monetaryAmount.value)) return false;
if (!currency.equals(monetaryAmount.currency)) return false; return true;
} public int hashCode() {
int result;
result = value.hashCode();
result = 29 * result + currency.hashCode();
return result;
} /*
You will need a <code>String</code> representation of a monetary
amount. Implement the <code>toString()</code> method and a static method to
create an instance from a <code>String</code>.
*/
public String toString() {
return getValue() + " " + getCurrency();
} public static MonetaryAmount fromString(String s) {
String[] split = s.split(" ");
return new MonetaryAmount(
new BigDecimal(split[0]),
Currency.getInstance(split[1])
);
}
}

2.

 package org.jpwh.converter;

 import org.jpwh.model.advanced.MonetaryAmount;

 import javax.persistence.AttributeConverter;
import javax.persistence.Converter; @Converter(autoApply = true) // Default for MonetaryAmount properties
public class MonetaryAmountConverter
implements AttributeConverter<MonetaryAmount, String> { @Override
public String convertToDatabaseColumn(MonetaryAmount monetaryAmount) {
return monetaryAmount.toString();
} @Override
public MonetaryAmount convertToEntityAttribute(String s) {
return MonetaryAmount.fromString(s);
}
}

3.

 package org.jpwh.model.advanced.converter;

 import org.jpwh.converter.MonetaryAmountConverter;
import org.jpwh.model.advanced.MonetaryAmount; import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import java.util.Date; @Entity
public class Item { @Id
@GeneratedValue(generator = "ID_GENERATOR")
protected Long id; @NotNull
protected String name; @NotNull
@Convert( // Optional, autoApply is enabled
converter = MonetaryAmountConverter.class,
disableConversion = false)
@Column(name = "PRICE", length = 63)
protected MonetaryAmount buyNowPrice; @NotNull
protected Date createdOn = new Date(); public Long getId() {
return id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public MonetaryAmount getBuyNowPrice() {
return buyNowPrice;
} public void setBuyNowPrice(MonetaryAmount buyNowPrice) {
this.buyNowPrice = buyNowPrice;
} // ...
}

4.

 package org.jpwh.model.advanced.converter;

 abstract public class Zipcode {

     protected String value;

     public Zipcode(String value) {
this.value = value;
} public String getValue() {
return value;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Zipcode zipcode = (Zipcode) o;
return value.equals(zipcode.value);
} @Override
public int hashCode() {
return value.hashCode();
}
}

5.

 package org.jpwh.converter;

 import org.jpwh.model.advanced.converter.GermanZipcode;
import org.jpwh.model.advanced.converter.SwissZipcode;
import org.jpwh.model.advanced.converter.Zipcode; import javax.persistence.AttributeConverter;
import javax.persistence.Converter; @Converter
public class ZipcodeConverter
implements AttributeConverter<Zipcode, String> { @Override
public String convertToDatabaseColumn(Zipcode attribute) {
return attribute.getValue();
} @Override
public Zipcode convertToEntityAttribute(String s) {
if (s.length() == 5)
return new GermanZipcode(s);
else if (s.length() == 4)
return new SwissZipcode(s); // If you get to this point, you should consider
// cleaning up your database... or you can create
// an InvalidZipCode subclass and return it here. throw new IllegalArgumentException(
"Unsupported zipcode in database: " + s
);
}
}

6.

 package org.jpwh.model.advanced.converter;

 public class GermanZipcode extends Zipcode {

     public GermanZipcode(String value) {
super(value);
}
}

7.

 package org.jpwh.model.advanced.converter;

 public class SwissZipcode extends Zipcode {

     public SwissZipcode(String value) {
super(value);
}
}

8.

 package org.jpwh.model.advanced.converter;

 import org.jpwh.converter.ZipcodeConverter;
import org.jpwh.model.Constants; import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.io.Serializable; @Entity
@Table(name = "USERS")
public class User implements Serializable { @Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id; @NotNull
protected String username; // Group multiple attribute conversions with @Converts
@Convert(
converter = ZipcodeConverter.class,
attributeName = "zipcode" // Or "city.zipcode" for nested embeddables
)
protected Address homeAddress; public Long getId() {
return id;
}
public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Address getHomeAddress() {
return homeAddress;
} public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
} // ...
}

9.

 package org.jpwh.model.advanced.converter;

 import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull; @Embeddable
public class Address { @NotNull
@Column(nullable = false)
protected String street; @NotNull
@Column(nullable = false, length = 5)
protected Zipcode zipcode; @NotNull
@Column(nullable = false)
protected String city; protected Address() {
} public Address(String street, Zipcode zipcode, String 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 String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
}
}

也可以转换集合,If several @Convert annotations are required on a single embedded property, to convert several attributes of the Address , for example, you can group them within an @Converts annotation. You can also apply converters to values of collections and maps, if their values and/or keys are of basic or embeddable type. For example, you can add the @Convert annotation on a persistent Set<Zipcode> . We’ll show you how

to map persistent collections later, with @ElementCollection , in chapter 7.
For persistent maps, the attributeName option of the @Convert annotation has some special syntax:

 On a persistent Map<Address, String> , you can apply a converter for the zipcode property of each map key with the attribute name key.zipcode .
 On a persistent Map<String, Address> , you can apply a converter for the zipcode property of each map value with the attribute name value.zipcode .
 On a persistent Map<Zipcode, String> , you can apply a converter for the key of each map entry with the attribute name key .
 On a persistent Map<String, Zipcode> , you can apply a converter for the value of each map entry by not setting any attributeName .
As before, the attribute name can be a dot-separated path if your embeddable classes are nested; you can write key.city.zipcode to reference the zipcode property of the City class, in a composition with the Address class.
Some limitations of the JPA converters are as follows:
 You can’t apply them to identifier or version properties of an entity.
 You shouldn’t apply a converter on a property mapped with @Enumerated or @Temporal , because these annotations already declare what kind of conversion has to occur. If you want to apply a custom converter for enums or date/time properties, don’t annotate them with @Enumerated or @Temporal .
 You can apply a converter to a property mapping in an hbm.xml file, but you have to prefix the name: type="converter:qualified.ConverterName" .

JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-006类型转换器( @Converter(autoApply = true) 、type="converter:qualified.ConverterName" )的更多相关文章

  1. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-001Mapping basic properties(@Basic、@Access、access="noop"、@Formula、@ColumnTransformer、@Generated、 @ColumnDefaul、@Temporal、@Enumerated)

    一.简介 在JPA中,默认所有属性都会persist,属性要属于以下3种情况,Hibernate在启动时会报错 1.java基本类型或包装类 2.有注解 @Embedded 3.有实现java.io. ...

  2. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)

    一.结构 二.Hibernate支持的UserTypes接口  UserType —You can transform values by interacting with the plain JD ...

  3. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-005控制类型映射(Nationalized、@LOB、@org.hibernate.annotations.Type)

    一.简介 1. 2. 3. 4. to override this default mapping. The JPA specification has a convenient shortcut a ...

  4. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-004嵌套组件的注解AttributeOverrides

    一.数据库 二.代码 1. package org.jpwh.model.advanced; import javax.persistence.AttributeOverride; import ja ...

  5. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-003使用@AttributeOverrides

    Each @AttributeOverride for a component property is “complete”: any JPA or Hibernate annotation on t ...

  6. JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-002使用@Embeddable

    一.数据库 二.代码 1. package org.jpwh.model.simple; import javax.persistence.Column; import javax.persisten ...

  7. JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-003映射实体时的可选操作(<delimited-identifiers/>、PhysicalNamingStrategy、PhysicalNamingStrategyStandardImpl、、、)

    一.自定义映射的表名 1. @Entity @Table(name = "USERS") public class User implements Serializable { / ...

  8. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-003Table per concrete class with unions(@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)、<union-subclass>)

    一.代码 1. package org.jpwh.model.inheritance.tableperclass; import org.jpwh.model.Constants; import ja ...

  9. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-002Table per concrete class with implicit polymorphism(@MappedSuperclass、@AttributeOverride)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.mappedsuperclass; import javax.persistence.MappedSup ...

随机推荐

  1. 如何安装altium designer 10

    http://jingyan.baidu.com/article/4dc4084881e2bdc8d946f1f3.html

  2. Android实现简单音乐播放器(MediaPlayer)

    Android实现简单音乐播放器(MediaPlayer) 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个简单的音乐播放器,要求功能 ...

  3. QT 按钮类继承处理带定时器

    01.class KeyButton : public QPushButton  02.{  03.    Q_OBJECT  04.public:  05.    explicit KeyButto ...

  4. 浅谈ERP系统实施后如何完善企业内部控制制度建设

    ERP与企业内部控制制度,前者提升企业的管理水平,后者为企业发展保驾护航,两项工作都是企业各项工作的重中之重. ERP是企业资源规划Enterprise Resource Planning的缩写.企业 ...

  5. Java学习之IO流总结

    ---恢复内容开始--- 流是用来读写数据的,java有一个类叫File,它封装的是文件的文件名,只是内存里面的一个对象,真正的文件是在硬盘上的一块区间,在这个文件里面存放着各种各样的数据,我们想读文 ...

  6. openfire插件开发之完美开发

    http://redhacker.iteye.com/blog/1919329 一.说在前面 在继上篇Openfire3.8.2在eclipse中Debug方式启动最简单的方式后,我研究了openfi ...

  7. VC6.0打开或者添加工程文件崩溃的解决方法

    在Win7操作系统下使用Visual C++ 6.0编程时,如果点击菜单中的[打开]或者[添加],或者按快捷键,都会弹出下图的对话框,出现程序崩溃并退出的情况. 出现这种问题的原因是VC6.0和其他软 ...

  8. 避免JS全局变量冲突

    一.原则1.1 用匿名函数将脚本包起来1.2 使用命名空间(多级) 二.改进过程 2.1 原始数据(a.js和b.js都有全局变量window.a,导致冲突,全局变量属于window) //a.js& ...

  9. Applicationpoolidentity 好有趣哦

    前言: 在服务器上搭建一个网站 Windows Server2008 R2 +IIS7. 应用程序池默认选择ApplicationPoolIdentity.为了适应分布式需求,所以将SqlServer ...

  10. ZOJ 1111 Poker Hands

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1111 A poker hand consists of 5 ca ...