使用hibernate映射不可变属性

时间:2011-01-13 16:12:06

标签: hibernate

我正在尝试使用hibernate映射基础数据库的不可变属性,这里是相同的映射文件快照

<hibernate-mapping default-access="field">    
<!-- Immutable property -->
            <property name="creationDate" column="CREATIONDATE" type="timestamp"
                update="false" not-null="true" />

我没有在我的类中声明任何名为creationDate的属性,而hibernate抱怨我有以下错误

org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:110)
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:135)
    at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:80)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:323)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:456)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:131)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:267)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
    at com.raisonne.tr.persistence.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:161)
    at com.raisonne.tr.persistence.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:72)
    at com.raisonne.tr.dao.impl.GenericDAOImpl.makePersistance(GenericDAOImpl.java:173)
    at com.raisonne.tr.service.impex.impl.DestinationImportServiceImpl.startDestinationImport(DestinationImportServiceImpl.java:130)
    at com.raisonne.tr.backoffice.actions.impex.DestinationImportAction.destinationImport(DestinationImportAction.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

Caused by: org.hibernate.PropertyNotFoundException: field [creationDate] not found on com.tr.category.Category
    at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:145)
    at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:137)
    at org.hibernate.property.DirectPropertyAccessor.getGetter(DirectPropertyAccessor.java:160)
    at org.hibernate.mapping.Property.getGetter(Property.java:294)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:300)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:141)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:78)

我正在关注Hibernate的caveatemptor应用程序我甚至无法在caveatemptor中尊重类中看到任何名为creationdate的属性(特别是对于creatiodate)。

我正在考虑如何最好地使用hiberntae来映射这些属性。

提前致谢

1 个答案:

答案 0 :(得分:1)

根据问题的评论:我会说你的Java类应该有private Date createdDate = new Date(),没有setter(只有一个getter)。这样,您(和Hibernate)仍然可以访问它,但不允许此实体的使用者更改此值。

但是如果你真的不希望它在你的Java类中,那么你只需要从映射中删除它(并使用你的数据库功能来填充这个字段,比如触发器)。此错误是因为您映射了数据库列,但Java类中没有此类属性。只是从映射中删除它应该可以抑制异常。