Java 不可变值对象与JPA
有没有办法使用JPA映射像电子邮件地址这样的不可变值对象Java 不可变值对象与JPA,java,hibernate,jpa,domain-driven-design,final,Java,Hibernate,Jpa,Domain Driven Design,Final,有没有办法使用JPA映射像电子邮件地址这样的不可变值对象 @Immutable @Embeddable public final class EmailAddress { private final String value; public EmailAddress(String value) { this.value = value; } public String getValue() { return value;
@Immutable
@Embeddable
public final class EmailAddress {
private final String value;
public EmailAddress(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;
EmailAddress that = (EmailAddress) o;
return value.equals(that.value);
}
@Override
public int hashCode() {
return value.hashCode();
}
}
现在我在实体保存时出现异常
org.hibernate.InstantiationException: No default constructor for entity: com.domain.EmailAddress
org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:107)
org.hibernate.tuple.component.AbstractComponentTuplizer.instantiate(AbstractComponentTuplizer.java:102)
org.hibernate.type.ComponentType.instantiate(ComponentType.java:515)
org.hibernate.type.ComponentType.deepCopy(ComponentType.java:434)
org.hibernate.type.TypeHelper.deepCopy(TypeHelper.java:68)
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:302)
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:672)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
$Proxy25.persist(Unknown Source)
org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:360)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:368)
org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:349)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy26.save(Unknown Source)
com.controller.UserController.create(UserController.java:64)
我希望使用final字段和hibernate作为JPA实现。为了让JPA能够通过反射创建对象,您必须有一个默认构造函数,但它不必是公共的。我也喜欢将字段保留为最终字段,但这对于反射来说可能太严格了--您必须尝试一下 我建议删除最后一个字段修饰符,并添加一个带有简短注释的私有默认构造函数(这样您仍然知道下周没有op构造函数的原因):
使用标准JPA注释和可嵌入对象无法做到这一点,因为必须使用默认构造函数和通过反射设置的值来创建对象
但是,您可以使用Hibernate自定义类型。阅读,其中有一个示例
Money
类型,它是使用带参数的构造函数实例化的,因此可能是不可变的。可能最简单的解决方案是实现org.Hibernate.usertype.usertype,它适用于稍旧的版本,如Hibernate的3.5。
其中有很多方法,但对于不可变类型,您可以将其中大部分提取到公共超类:
package com.acme;
import java.io.Serializable;
import org.hibernate.usertype.UserType;
public abstract class AbstractImmutableType
implements UserType {
public AbstractImmutableType() {
super();
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object value) {
return (Serializable) value;
}
public Object assemble(Serializable cached, Object owner) {
return cached;
}
public Object deepCopy(Object value) {
return value;
}
public Object replace(Object original, Object target,
Object owner) {
return original;
}
public boolean equals(Object x, Object y) {
if (x != null && y != null) {
return x.equals(y);
}
// Two nulls are equal as well
return x == null && y == null;
}
public int hashCode(Object x) {
if (x != null) {
return x.hashCode();
}
return 0;
}
}
您可以这样使用它:
package com.acme;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
public class CurrencyType extends AbstractImmutableType {
public static final String TYPE = "com.acme.CurrencyType";
private static final int[] SQL_TYPES = {
Types.VARCHAR
};
public CurrencyType() {
super();
}
public Object nullSafeGet(ResultSet rs, String[] names,
Object owner) throws SQLException {
String value = rs.getString(names[0]);
if (rs.wasNull()) {
return null;
}
return Currency.valueOf(value);
}
public void nullSafeSet(PreparedStatement st, Object value,
int index) throws SQLException {
if (value != null) {
st.setString(index, ((Currency)value).getCode());
} else {
st.setNull(index, SQL_TYPES[0]);
}
}
public Class<?> returnedClass() {
return Currency.class;
}
public int[] sqlTypes() {
return SQL_TYPES;
}
}
package com.acme;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Types;
公共类CurrencyType扩展了AbstractImmutableType{
公共静态最终字符串TYPE=“com.acme.CurrencyType”;
私有静态最终int[]SQL\u类型={
类型.VARCHAR
};
公共货币类型(){
超级();
}
公共对象nullSafeGet(结果集rs,字符串[]名称,
对象所有者)抛出SQLException{
字符串值=rs.getString(名称[0]);
如果(rs.wasNull()){
返回null;
}
返回货币。valueOf(值);
}
public void nullSafeSet(PreparedStatement st,对象值,
int索引)引发SQLException{
if(值!=null){
st.setString(索引,((货币)值).getCode());
}否则{
st.setNull(索引,SQL_类型[0]);
}
}
公共类returnedClass(){
返回货币.class;
}
公共int[]sqlTypes(){
返回SQL_类型;
}
}
有关此代码的详细说明,您可以找到谢谢。但我希望有更好的方法)我做了一个快速测试,让我通过反射设置私有final字段——这样你就可以得到
私有final字符串值
和默认构造函数中的dothis.value=null
——但Hibernate文档建议保留最终修饰符(也适用于类)以启用代理/延迟加载:我认为这是一个很好的快速解决方案。但我更喜欢使用显式绑定器进行EE开发。
package com.acme;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
public class CurrencyType extends AbstractImmutableType {
public static final String TYPE = "com.acme.CurrencyType";
private static final int[] SQL_TYPES = {
Types.VARCHAR
};
public CurrencyType() {
super();
}
public Object nullSafeGet(ResultSet rs, String[] names,
Object owner) throws SQLException {
String value = rs.getString(names[0]);
if (rs.wasNull()) {
return null;
}
return Currency.valueOf(value);
}
public void nullSafeSet(PreparedStatement st, Object value,
int index) throws SQLException {
if (value != null) {
st.setString(index, ((Currency)value).getCode());
} else {
st.setNull(index, SQL_TYPES[0]);
}
}
public Class<?> returnedClass() {
return Currency.class;
}
public int[] sqlTypes() {
return SQL_TYPES;
}
}