-->
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.  [ 8 posts ] 
Author Message
 Post subject: Problems trying to map a MAP collection into a component
PostPosted: Tue Apr 05, 2005 11:17 am 
Newbie

Joined: Tue Jan 25, 2005 12:25 pm
Posts: 9
Hi there! I have two simple classes (i didnt copied here the id properties and their setters/getters):

public class PermissionsTable {
// Permission is an enum type
private Hashtable<Permission,Object> permissions ;

public Map getPermissions() {
return permissions;
}

public void setPermissions(Map permissions) {
// Here i fill the hashTable with Map
}
}

public class Role {
private PermissionsTable permissionsTable ;

public PermissionsTable getPermissionsTable() {
return permissionsTable;
}

public void setPermissionsTable(PermissionsTable permissions) {
this.permissionsTable = permissions;
}
}

I have a ONE-2-ONE relation between a ROLE and a PERMISSION_TABLE, so i mapped directly PermissionsTable as MAP collection into a Component of ROLE.

The problem is that i have a strange error... it seems that Hibernate dont create a collection holder for that collection. Please check it and let me know what i am doing wrong!

Hibernate version:
3.0

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="it.airventsam.markab.server.core.security" default-lazy="false">
<typedef name="Permission" class="it.airventsam.markab.server.core.hibernate.EnumUserType">
<param name="enumClassName">it.airventsam.markab.server.core.security.Permission</param>
</typedef>

<class name="Role" table="security_roles">
<id name="id">
<generator class="native"/>
</id>

<version name="version"/>

<component name="permissionsTable">
<map name="permissionsValues" lazy="false" table="security_permissionstables">
<key column="id"/>
<map-key column="permission" type="Permission"/>
<element column="value" type="blob"/>
</map>
</component>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(obj);
session.flush();
tx.commit();
} catch (RuntimeException e) {
if (tx != null)
tx.rollback();
throw e ;
} finally {
session.close();
}


Full stack trace of any exception that occurs:

java.lang.ClassCastException: java.util.Hashtable
at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:34)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:104)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:64)
at org.hibernate.event.def.AbstractVisitor.processValues(AbstractVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.processComponent(AbstractVisitor.java:85)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:110)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:64)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:58)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:197)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:675)
at it.airventsam.markab.server.core.hibernate.HibernateTableAccessor.save(HibernateTableAccessor.java:47)
at it.airventsam.markab.server.core.security.SecurityService.addRole(SecurityService.java:520)

Name and version of the database you are using:

HSqlDB 1.7.3.3

The generated SQL (show_sql=true):

None.

Debug level Hibernate log excerpt:

DEBUG [main] opened session at timestamp: 4557675321036800
DEBUG [main] begin
DEBUG [main] opening JDBC connection
DEBUG [main] total checked-out connections: 0
DEBUG [main] using pooled JDBC connection, pool size: 0
DEBUG [main] current autocommit status: false
DEBUG [main] saving transient instance
DEBUG [main] generated identifier: , using strategy: org.hibernate.id.IdentityGenerator
DEBUG [main] saving [it.airventsam.markab.server.core.security.Role#<null>]
DEBUG [main] executing insertions
DEBUG [main] processing cascade ACTION_SAVE_UPDATE for: it.airventsam.markab.server.core.security.Role
DEBUG [main] done processing cascade ACTION_SAVE_UPDATE for: it.airventsam.markab.server.core.security.Role
DEBUG [main] using initial version: 0
DEBUG [main] Wrapped collection in role: it.airventsam.markab.server.core.security.Role.it.airventsam.markab.server.core.security.Role.permissionsTable.permissionsValues
DEBUG [main] Wrapped collection in role: it.airventsam.markab.server.core.security.Role.accounts
DEBUG [main] Inserting entity: it.airventsam.markab.server.core.security.Role (native id)
DEBUG [main] Version: 0
DEBUG [main] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [main] insert into security_roles (version, name, description, level, id) values (?, ?, ?, ?, null)
DEBUG [main] preparing statement
DEBUG [main] Dehydrating entity: [it.airventsam.markab.server.core.security.Role#<null>]
DEBUG [main] binding '0' to parameter: 1
DEBUG [main] binding 'Director' to parameter: 2
DEBUG [main] binding 'Director of the hotel' to parameter: 3
DEBUG [main] binding '255' to parameter: 4
DEBUG [main] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [main] closing statement
DEBUG [main] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [main] call identity()
DEBUG [main] preparing statement
DEBUG [main] Natively generated identity: 1
DEBUG [main] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [main] closing statement
DEBUG [main] processing cascade ACTION_SAVE_UPDATE for: it.airventsam.markab.server.core.security.Role
DEBUG [main] done processing cascade ACTION_SAVE_UPDATE for: it.airventsam.markab.server.core.security.Role
DEBUG [main] flushing session
DEBUG [main] processing flush-time cascades
DEBUG [main] processing cascade ACTION_SAVE_UPDATE for: it.airventsam.markab.server.core.security.Role
DEBUG [main] done processing cascade ACTION_SAVE_UPDATE for: it.airventsam.markab.server.core.security.Role
DEBUG [main] dirty checking collections
DEBUG [main] Flushing entities and processing referenced collections
DEBUG [main] Wrapped collection in role: it.airventsam.markab.server.core.security.Role.it.airventsam.markab.server.core.security.Role.permissionsTable.permissionsValues
DEBUG [main] it.airventsam.markab.server.core.security.Role.permissionsTable is dirty
DEBUG [main] Updating entity: [it.airventsam.markab.server.core.security.Role#1]
DEBUG [main] Incrementing: 0 to 1
DEBUG [main] rollback
DEBUG [main] before transaction completion
DEBUG [main] before transaction completion
DEBUG [main] rolled back JDBC Connection
DEBUG [main] after transaction completion
DEBUG [main] after transaction completion
DEBUG [main] closing session
DEBUG [main] closing JDBC connection (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)
DEBUG [main] returning connection to pool, pool size: 1
DEBUG [main] after transaction completion
DEBUG [main] after transaction completion


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 05, 2005 1:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Do not use a private Hashtable<Permission,Object> permissions ;
, use a Map (ie the interface)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 07, 2005 4:56 am 
Newbie

Joined: Tue Jan 25, 2005 12:25 pm
Posts: 9
emmanuel wrote:
Do not use a private Hashtable<Permission,Object> permissions ;
, use a Map (ie the interface)


I tried that by using:

private Map<Permission,Object> permissionsValues = new Hashtable<Permission,Object>() ;

but the problem still happens:

DEBUG [main] before transaction completion
java.lang.ClassCastException: java.util.Hashtable
at org.hibernate.event.def.FlushVisitor.processCollection(FlushVisitor.java:34)

As you see it seems Hibernate tries to do a strange cast:

coll = (PersistentCollection) collection;

and that is impossibile even if i change the type you said. It seems to me that Hibernate dont create an holder for my collection included inside a component. Do you confirm that?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 07, 2005 10:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
pscaffardi wrote:
It seems to me that Hibernate dont create an holder for my collection included inside a component. Do you confirm that?

I don't think so. But if you write a runnable small unit test to reproduce the problem, post it to JIRA

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 07, 2005 1:55 pm 
Newbie

Joined: Wed Dec 03, 2003 9:41 am
Posts: 7
Location: Stockholm, Sweden
I think your problem is in the setPermissions(Map permissions) method. When Hibernate calls the method the supplied map parameter is an instance of PersistentCollection, but you are probably copying the map entiries to your HashMap. But when Hibernate later calls getPermissions() it acts as if the returned map is the same map as the one supplied to setPermissions.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 08, 2005 2:29 am 
Newbie

Joined: Tue Jan 25, 2005 12:25 pm
Posts: 9
odd wrote:
I think your problem is in the setPermissions(Map permissions) method. When Hibernate calls the method the supplied map parameter is an instance of PersistentCollection, but you are probably copying the map entiries to your HashMap. But when Hibernate later calls getPermissions() it acts as if the returned map is the same map as the one supplied to setPermissions.


If so, how can initialize that property by myself without assigning it a subclass of Map?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 09, 2005 2:20 pm 
Newbie

Joined: Wed Dec 03, 2003 9:41 am
Posts: 7
Location: Stockholm, Sweden
First I must stress that I don't know if the analysis in my previous reply is correct, but you if it is, you should still be able to set the map field in your field declaration as long as the setter method simply reassigns the map field to the supplied map (which should be an instance of PersistentCollection when supplied by Hibernate), instead of copying the contents to the initial value of the field instance (which is not a PersistentCollection, but in your case a Hashtable).
Again, I don't know if my assumptions above are correct, so please check with the documentation and source.

Greetings
///Odd


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 12, 2005 6:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
if you don't provide your pojo code we can't help you efficiently

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.