我有一个自定义对象:
@Embeddable
public class MyObject () {
private String field1;
private String field2;
//getters and setters...
}
我有一个表示具有两列的表的持久性实体,我将它们称为colA和colB。在持久性实体中,我想将field1映射到colA,将field2映射到colB。我试过了:
@Embedded
@AttributeOverrides({
@AttributeOverride(name="field1", column = @Column(name="colA", nullable=true)),
@AttributeOverride(name="field2", column = @Column(name="colB", nullable=true))
})
private MyObject myObject;
但是,如果其中一个字段没有值,则无法持久保存到数据库。我知道JPA 2.1中的AttributeConverter允许我们将对象中的多个字段保存到一列,但有没有办法将对象中的两个字段保存到两个不同的列?
我在运行时遇到的错误:
javax.persistence.PersistenceException:无法构建EntityManagerFactory 在org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915) 引起:org.hibernate.HibernateException:无法实例化默认tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer] 在org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:101) 在org.hibernate.tuple.component.ComponentTuplizerFactory.constructDefaultTuplizer(ComponentTuplizerFactory.java:122) 在org.hibernate.tuple.component.ComponentEntityModeToTuplizerMapping。(ComponentEntityModeToTuplizerMapping.java:75) 在org.hibernate.tuple.component.ComponentMetamodel。(ComponentMetamodel.java:73) 在org.hibernate.mapping.Component.buildType(Component.java:184) 在org.hibernate.mapping.Component.getType(Component.java:177) 在org.hibernate.tuple.PropertyFactory.buildStandardProperty(PropertyFactory.java:143) 在org.hibernate.tuple.component.ComponentMetamodel。(ComponentMetamodel.java:68) 在org.hibernate.mapping.Component.buildType(Component.java:184) 在org.hibernate.mapping.Component.getType(Component.java:177) 在org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:290) 在org.hibernate.mapping.Property.isValid(Property.java:217) 在org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:464) 在org.hibernate.mapping.RootClass.validate(RootClass.java:235) 在org.hibernate.cfg.Configuration.validate(Configuration.java:1362) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865) 在org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906) ......还有4个 引起:java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 在org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:98) ......还有20个 引起:org.hibernate.PropertyNotFoundException:在com.mypackage.persistenceentities.MyEntity上找不到字段[field1] 在org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:182) 在org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:174) 在org.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropertyAccessor.java:197) 在org.hibernate.mapping.Property.getGetter(Property.java:304) 在org.hibernate.tuple.component.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:156) 在org.hibernate.tuple.component.AbstractComponentTuplizer。(AbstractComponentTuplizer.java:65) 在org.hibernate.tuple.component.PojoComponentTuplizer。(PojoComponentTuplizer.java:60) ......还有25个
谢谢, 亚历