hibernate 3.5.1 Envers retrieve revisions produces PropertyNotFoundException
havelh May 19, 2010 10:48 AMHi all,
the function AuditReaderImpl.find produces the following error:
=====================================================================
11:51:34,825 ERROR http-8080-2 HibernateVersionDAOUtil:52 - ::getVersions: error loading: ContentData
org.hibernate.envers.exception.AuditException: org.hibernate.PropertyNotFoundException: Could not find a setter for property contentid in class java.lang.Object
at org.hibernate.envers.entities.mapper.id.EmbeddedIdMapper.mapToEntityFromMap(EmbeddedIdMapper.java:82)
at org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:95)
at org.hibernate.envers.entities.EntityInstantiator.addInstancesFromVersionsEntities(EntityInstantiator.java:103)
at org.hibernate.envers.query.impl.EntitiesAtRevisionQuery.list(EntitiesAtRevisionQuery.java:90)
at org.hibernate.envers.query.impl.AbstractAuditQuery.getSingleResult(AbstractAuditQuery.java:105)
at org.hibernate.envers.reader.AuditReaderImpl.find(AuditReaderImpl.java:106)
..
..
..
at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property contentid in class java.lang.Object
at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:262)
at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:255)
at org.hibernate.envers.tools.reflection.ReflectionTools.getSetter(ReflectionTools.java:85)
at org.hibernate.envers.tools.reflection.ReflectionTools.getSetter(ReflectionTools.java:78)
at org.hibernate.envers.entities.mapper.id.SingleIdMapper.mapToEntityFromMap(SingleIdMapper.java:64)
at org.hibernate.envers.entities.mapper.id.EmbeddedIdMapper.mapToEntityFromMap(EmbeddedIdMapper.java:79)
... 29 more
=====================================================================
primary key fields should be set.
Question: is it not possible to retrieve the revisions for a entity with a composite-id ?
Please suggest how can i proceed.
=== retireve revisions ======================================================
public class HibernateVersionDAOUtil {
Map map = new HashMap();
Transaction tx = null;
try {
tx = session.getTransaction();
if (tx==null || tx.isActive()==false) {
log.debug("starting new transaction...");
tx = session.beginTransaction();
opened = true;
}
List<Number> versions = reader.getRevisions(cls, id);
Object obj = reader.find(cls, id, c);
}
log.debug("::getVersions: autocomitting new transaction...");
tx.commit();
}
} catch (HibernateException e) {
log.error("::getVersions: error loading: "+cls.getName(),e);
if (tx != null && tx.isActive()) {
try {
tx.rollback();
} catch (HibernateException e1) {
log.error("::getVersions: error rolling back after exception at find "+cls.getSimpleName(),e1);
}
}
}
}
}
=====================================================================
public class ContentDataPK {
private Integer contentid;
private String language;
super();
}
this.contentid = contentid;
this.language = language;
}
return language;
}
this.language = language;
}
return contentid;
}
this.contentid = contentid;
}
}
=====================================================================
@Audited
public class ContentData implements Serializable {
private String text;
super();
}
this.id = new ContentDataPK(id, language);
this.text = text;
}
return title;
}
this.title = title;
}
return text;
}
this.text = text;
}
return id;
}
this.id = id;
}
=====================================================================
=== hibernate mapping ================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="ContentData" table="contents">
<key-property name="contentid" column="contentid" type="java.lang.Integer" />
<key-property name="language" column="language" type="java.lang.String"/>
</composite-id>
<property name="title" column="title" type="java.lang.String" />
<property name="text" column="text" type="java.lang.String" />
</hibernate-mapping>
=====================================================================
On retrieve all revisions of a entity with a composite-id
the function AuditReaderImpl.find produces the following error:
=====================================================================
11:51:34,825 ERROR http-8080-2 HibernateVersionDAOUtil:52 - ::getVersions: error loading: ContentData
org.hibernate.envers.exception.AuditException: org.hibernate.PropertyNotFoundException: Could not find a setter for property contentid in class java.lang.Object
at org.hibernate.envers.entities.mapper.id.EmbeddedIdMapper.mapToEntityFromMap(EmbeddedIdMapper.java:82)
at org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:95)
at org.hibernate.envers.entities.EntityInstantiator.addInstancesFromVersionsEntities(EntityInstantiator.java:103)
at org.hibernate.envers.query.impl.EntitiesAtRevisionQuery.list(EntitiesAtRevisionQuery.java:90)
at org.hibernate.envers.query.impl.AbstractAuditQuery.getSingleResult(AbstractAuditQuery.java:105)
at org.hibernate.envers.reader.AuditReaderImpl.find(AuditReaderImpl.java:106)
..
..
..
at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property contentid in class java.lang.Object
at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:262)
at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:255)
at org.hibernate.envers.tools.reflection.ReflectionTools.getSetter(ReflectionTools.java:85)
at org.hibernate.envers.tools.reflection.ReflectionTools.getSetter(ReflectionTools.java:78)
at org.hibernate.envers.entities.mapper.id.SingleIdMapper.mapToEntityFromMap(SingleIdMapper.java:64)
at org.hibernate.envers.entities.mapper.id.EmbeddedIdMapper.mapToEntityFromMap(EmbeddedIdMapper.java:79)
... 29 more
=====================================================================
The reportetd error is correct, becouse in the class ContentData no setter for a field "contentid " exists.
Insted of set the fields directly in the entry, a primary key class ContentDataPK should be instanciated and in that the
primary key fields should be set.
below you see the access function, entity class "ContentData", primary key class "ContentDataPK" and the hibernate configuration.
I use hibernate 3.5.1.
Question: is it not possible to retrieve the revisions for a entity with a composite-id ?
Please suggest how can i proceed.
Thanks in advance.
Hannes
=== retireve revisions ======================================================
public class HibernateVersionDAOUtil {
public Map<Number, ?> getVersions(Session session, Class<?> cls, Serializable id) throws PersistentException {
log.debug("::getVersions: cls:" + cls + " id:" + id + " session:" + session);
Map map = new HashMap();
Transaction tx = null;
boolean opened = false;
try {
tx = session.getTransaction();
if (tx==null || tx.isActive()==false) {
log.debug("starting new transaction...");
tx = session.beginTransaction();
opened = true;
}
AuditReader reader = AuditReaderFactory.get(session);
List<Number> versions = reader.getRevisions(cls, id);
for (Number c : versions ) {
Object obj = reader.find(cls, id, c);
map.put(c, obj);
}
if (opened) {
log.debug("::getVersions: autocomitting new transaction...");
tx.commit();
}
} catch (HibernateException e) {
log.error("::getVersions: error loading: "+cls.getName(),e);
if (tx != null && tx.isActive()) {
try {
tx.rollback();
} catch (HibernateException e1) {
log.error("::getVersions: error rolling back after exception at find "+cls.getSimpleName(),e1);
}
}
throw new PersistentException("persistence", "getVersions", "hibernate error:" + e.getLocalizedMessage(), null, e);
}
log.debug("::getVersions: result:" + map);
return map;
}
}
=====================================================================
==== primary key class ====================================================
public class ContentDataPK {
private Integer contentid;
private String language;
public ContentDataPK() {
super();
}
public ContentDataPK(Integer contentid, String language) {
this.contentid = contentid;
this.language = language;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public Integer getContentid() {
return contentid;
}
public void setContentid(Integer contentid) {
this.contentid = contentid;
}
}
=====================================================================
==== entity class =================================================================
@Audited
public class ContentData implements Serializable {
private ContentDataPK id; // primary key
private String title;
private String text;
public ContentData() {
super();
}
public ContentData(Integer id, String language, String title, String text) {
this.id = new ContentDataPK(id, language);
this.title = title;
this.text = text;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public ContentDataPK getId() {
return id;
}
public void setId(ContentDataPK id) {
this.id = id;
}
}
=====================================================================
=== hibernate mapping ================================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="ContentData" table="contents">
<composite-id name="id">
<key-property name="contentid" column="contentid" type="java.lang.Integer" />
<key-property name="language" column="language" type="java.lang.String"/>
</composite-id>
<property name="title" column="title" type="java.lang.String" />
<property name="text" column="text" type="java.lang.String" />
</class>
</hibernate-mapping>
=====================================================================