-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Classes implementing Map cannot be persisted.
PostPosted: Thu Mar 15, 2007 11:16 am 
Newbie

Joined: Thu Mar 15, 2007 10:57 am
Posts: 2
Hi List,

I'm using hibernate with the hibernate entity manager and annotation. And 'am getting the exception below.The problem seems to be in that my VersionedPersistentObject class inherits from a Map implementation. Hibernate seems to think that a class which implements Map is a dynamicaly mapped entity and because it cannot find a special key, it just
refuses to persist the object.

I have the feeling that this is not conformant with the EJB3 spec (Entities may extend non entity classes). What do you think about it? Should I report this as a bug?

Here are the details:

Hibernate version: 3.2.2

Entity Classes:
@MappedSuperclass
public abstract class VersionedPersistentObject extends HashMap<Object, Object> implements Serializable {
private Long id;
private Long version;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}

protected void setId(Long id) {
this.id = id;
}

@Version
public Long getVersion() {
return version;
}

protected void setVersion(Long version) {
this.version = version;
}

@Override
public String toString() {
return toStringBuffer().toString();
}

/**
* @return Returns the string buffer representation of self. This method is used
* in {@link #toString()}}
*/
protected StringBuffer toStringBuffer() {
return new StringBuffer()
.append(getClass().getName())
.append("[")
.append("id: ")
.append(getId())
.append(", ")
.append("version: ")
.append(getVersion())
.append("]");
}
}

@Entity
@Table(name="events")
public class Event extends VersionedPersistentObject implements IEvent {
private String title;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Override
protected StringBuffer toStringBuffer() {
return super.toStringBuffer()
.append("[")
.append("title: ")
.append(getTitle())
.append("]");
}

public void copyTo(Event event) {
}
}

Code between sessionFactory.openSession() and session.close():
em.getTransaction().begin();
Event e = new Event();
e.setTitle("Title");
e.copyTo(new Event());
em.persist(e);
em.getTransaction().commit();

Full stack trace of any exception that occurs:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.HibernateException: could not determine type of dynamic entity
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:641)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
at com.ls.hibernate.EntityManagerTest.main(EntityManagerTest.java:22)
Caused by: org.hibernate.HibernateException: could not determine type of dynamic entity
at org.hibernate.impl.SessionImpl.guessEntityName(SessionImpl.java:1764)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:487)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:70)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:618)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:592)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:596)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:212)
... 1 more


Top
 Profile  
 
 Post subject: Hibernate method code which caused the error
PostPosted: Thu Mar 15, 2007 11:29 am 
Newbie

Joined: Thu Mar 15, 2007 10:57 am
Posts: 2
public String guessEntityName(Object object) throws HibernateException {
errorIfClosed();
String entity = interceptor.getEntityName( object );
if ( entity == null ) {
if ( object instanceof Map ) {
entity = (String) ( (Map) object ).get( DynamicMapInstantiator.KEY );
if ( entity == null ) {
throw new HibernateException( "could not determine type of dynamic entity" );
}
}
else if ( object instanceof Element ) {
// TODO : really need to keep a map of nodeName -> entityName, but that would mean nodeName being distinct
entity = ( (Element) object ).getName();
}
else {
entity = object.getClass().getName();
}
}
return entity;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 15, 2007 11:33 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yes, please open a JIRA issue with a minimal test case.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.