一、结构

二、代码

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. 29.DDR2问题1仿真模型文件

    在使用modelsim仿真DDR2时,一般我们会用美光网站上下载的DDR2仿真模型.仿真模型文件一般有ddr2_module.v,ddr2.v,ddr2_mcp.v,ddr2_parameters.v ...

  2. 使用itunes同步ios时丢失照片恢复

    因没有备份,在使用同步功能后,发现照片被清空了,找到恢复方法,分享之! from:http://modmyi.com/forums/native-iphone-ipod-touch-app-discu ...

  3. 团队项目(NABC分析)

    我们团队开发的是<校园导航>软件 (1)N(Need需求) 我们的团队主要考虑到我们学校没有自己的校园导航,有时会给同学及参观人员带来不便,又看到好多学校都有自己的导航,所以就从这个需求方 ...

  4. UITableView的常用属性和cell的内存优化

    UITableView的常用属性: 分割线颜色设置: 1> 设置separatorStyle: 分割线的颜色 方法:tableView.separatorStyle = UITableViewC ...

  5. mysql字符集基础知识梳理

    接着上一篇继续来一篇关于mysql字符设置等问题学习笔记,这篇就不说什么废话了,直接进入正题,不过还是感谢十八哥的无私分享! 我们首先看看mysql整个数据存储和读取一个流程: 连接器(connect ...

  6. ffmpeg 按时间戳读取文件 -re

    ffmpeg读取文件有两种方式:一种是直接读取,文件被迅速读完;一种是按时间戳读取.一般都是按时间戳读取文件, 命令行加入-re,表示按时间戳读取文件,在ffmpeg_opt.c 中可以看到re对应的 ...

  7. JavaScript 异常

    转载自:http://www.cnblogs.com/aqbyygyyga/archive/2011/10/29/2228824.html(排版格式修改了一下) 一.错误处理的重要性 以前,javas ...

  8. 博文&零散信息阅读

    关于培养方案: 全国一线高校.网易云课堂和我院培养计划的区别主要体现在: 基础课上,我院删去了物理课程的必修要求. 专业课上,删去了汇编语言.编译原理.信息安全技术等学科的必修要求. 专业选修课上,我 ...

  9. HTTP1.1缓存策略

    以下是一幅虽然信息包含量有限.但足够以最简洁的方式说明了“什么是HTTP1.1缓存策略”的图  缓存和缓存策略 web缓存(web cache)或代理缓存(proxy cache)是一种特殊的HTTP ...

  10. PS4 Razor GPU

    这东西,从出来就感觉没用,各种请教也都没有帮助.虽然搞明白了 rt啊tex啊buffer啊但是就是感觉对于抓bug没有用处.所以从来都是像巫师一样靠直觉,再用科学的方法来测试,其实就是让ps retu ...