开发者

mapping immutable properties using hibernate

开发者 https://www.devze.com 2023-02-04 18:49 出处:网络
i am trying to map an immutable property with underlying DB using hibernate and here is the mapping file snapshot for the same

i am trying to map an immutable property with underlying DB using hibernate and here is the mapping file snapshot for the same

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

i have not declared any property with the name creationDate in my class and hibernate is complaining me with the following error

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.S开发者_C百科ingleTableEntityPersister.<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)

i was following Hibernate's caveatemptor application i even unable to see any property with the name creationdate in the respected Class in caveatemptor (Especially for creatiodate).

i am just thinking how best we can map such properties using hiberntae.

Thanks in advance


Based on the comments from the question: I'd say that you should have a private Date createdDate = new Date() in your Java class, without a setter (only a getter). This way, you (and Hibernate) still have access to it, but consumers of this entity are not allowed to change this value.

But if you really don't want it in your Java class, then you just need to remove it from your mapping (and use your database features to populate this field, like triggers). This error is because you mapped the database column, but there's no such property in the Java class. Just removing it from your mapping should suppress the exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消