-->
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: Problem using UserCollectionType to wrap Properties object
PostPosted: Thu Jul 28, 2005 5:10 pm 
Newbie

Joined: Thu Jul 28, 2005 4:14 pm
Posts: 2
Hi:

I am trying to persist a java.util.Properties object as a map. Apparently the <map> element is not appropriate, because hibernate implements the map as an instance of HashMap, causing a ClassCastException when it attempts to assign that object to the Properties field in the persisted class.

With that in mind, I tried to create an implementation of UserCollectionType to persist the Properties object as a map. A PropertyAccessException is being thrown however -- it looks as if hibernate is trying to assign the PersistentMap to the Properties field of the persisted POJO. I think that I'm misunderstanding some portion of the API for that class, but I'm not sure where. Any help resolving this problem (or suggesting an alternative approach) would be greatly appreciated. I am unable, however, change the Properties object to a map.

TIA.


Hibernate version:
Version 3.0.5

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

<hibernate-mapping default-lazy="false">

<!-- PersistenceTarget -->
<class name="com.foo.pojo.PersistenceTarget" table="PERSISTENCE_TARGET">
<id name="id" column="PERSISTENCE_ID" type="string">
<generator class="uuid.hex"/>
</id>
<property name="description" column="DESCRIPTION" type="string"/>

<map name="properties" lazy="false" collection-type="com.foo.hibernate.PropertiesType" table="PROPERTIES">
<key column="ID"/>
<index column="NAME" type="string"/>
<element column="VALUE" type="string" not-null="true"/>
</map>
</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
tx = session.beginTransaction();

String description = "A simple class containing a String object and a Properties object";
Properties props = new Properties();
props.setProperty("color", "blue");
props.setProperty("size", "medium");
PersistenceTarget target = new PersistenceTarget();
target.setDescription(description);
target.setProperties(props);

session.save(target);

tx.commit();


Full stack trace of any exception that occurs:
org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.foo.pojo.PersistenceTarget.setProperties
org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.foo.pojo.PersistenceTarget.setProperties
at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:203)
at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:173)
at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2919)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:225)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at com.foo.test.PropertiesTest.testProperties(PropertiesTest.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: net.sf.cglib.beans.BulkBeanException: org.hibernate.collection.PersistentMap
at com.foo.pojo.PersistenceTarget$$BulkBeanByCGLIB$$de231fd6.setPropertyValues(<generated>)
at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:200)
... 28 more
Caused by: java.lang.ClassCastException: org.hibernate.collection.PersistentMap
... 30 more


Name and version of the database you are using:
MySQL, version 4.0.21


The generated SQL (show_sql=true):
n/a

Debug level Hibernate log excerpt:
16:44:09,176 DEBUG PropertiesTest:37 - PropertiesTest
16:44:09,226 DEBUG PropertiesTest:42 - In setUp().....
16:44:09,226 DEBUG PropertiesTest:51 - in testProperties().....
16:44:09,336 INFO Environment:464 - Hibernate 3.0.5
16:44:09,336 INFO Environment:477 - hibernate.properties not found
16:44:09,336 INFO Environment:510 - using CGLIB reflection optimizer
16:44:09,406 INFO Environment:540 - using JDK 1.4 java.sql.Timestamp handling
16:44:09,656 INFO Configuration:1110 - configuring from resource: /hibernate.cfg.xml
16:44:09,656 INFO Configuration:1081 - Configuration resource: /hibernate.cfg.xml
16:44:10,988 DEBUG Configuration:1067 - show_sql=true
16:44:10,988 DEBUG Configuration:1067 - dialect=org.hibernate.dialect.MySQLDialect
16:44:10,998 DEBUG Configuration:1067 - hibernate.cglib.use_reflection_optimizer=false
16:44:10,998 DEBUG Configuration:1067 - hibernate.connection.driver_class=com.mysql.jdbc.Driver
16:44:10,998 DEBUG Configuration:1067 - hibernate.connection.url=jdbc:mysql://localhost/propDB
16:44:10,998 DEBUG Configuration:1067 - hibernate.connection.username=root
16:44:11,008 DEBUG Configuration:1067 - hibernate.dbcp.maxActive=25
16:44:11,008 DEBUG Configuration:1067 - hibernate.dbcp.whenExhaustedAction=1
16:44:11,008 DEBUG Configuration:1067 - hibernate.dbcp.maxWait=120000
16:44:11,008 DEBUG Configuration:1067 - hibernate.dbcp.maxIdle=10
16:44:11,008 DEBUG Configuration:1067 - hibernate.dbcp.maxActive=100
16:44:11,018 DEBUG Configuration:1067 - hibernate.dbcp.ps.whenExhaustedAction=1
16:44:11,018 DEBUG Configuration:1067 - hibernate.dbcp.ps.maxWait=120000
16:44:11,018 DEBUG Configuration:1067 - hibernate.dbcp.ps.maxIdle=100
16:44:11,028 DEBUG Configuration:1262 - null<-org.dom4j.tree.DefaultAttribute@16dadf9 [Attribute: name resource value "com/foo/pojo/foo-hibernate.xml"]
16:44:11,028 INFO Configuration:444 - Mapping resource: com/foo/pojo/foo-hibernate.xml
16:44:11,309 INFO HbmBinder:260 - Mapping class: com.foo.pojo.PersistenceTarget -> PERSISTENCE_TARGET
16:44:11,329 DEBUG HbmBinder:1099 - Mapped property: id -> PERSISTENCE_ID
16:44:11,359 DEBUG HbmBinder:1099 - Mapped property: description -> DESCRIPTION
16:44:11,369 INFO HbmBinder:1218 - Mapping collection: com.foo.pojo.PersistenceTarget.properties -> PROPERTIES
16:44:11,379 DEBUG HbmBinder:1099 - Mapped property: properties
16:44:11,379 INFO Configuration:1222 - Configured SessionFactory: null
16:44:11,379 DEBUG Configuration:1223 - properties: {java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition, sun.boot.library.path=C:\java\jdk1.5.0_01\jre\bin, java.vm.version=1.5.0_01-b08, hibernate.connection.username=root, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.country=US, sun.os.patch.level=Service Pack 4, java.vm.specification.name=Java Virtual Machine Specification, user.dir=C:\workspace\rfid2, java.runtime.version=1.5.0_01-b08, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:\java\jdk1.5.0_01\jre\lib\endorsed, os.arch=x86, java.io.tmpdir=C:\DOCUME~1\SWILLI~1\LOCALS~1\Temp\, line.separator=
, java.vm.specification.vendor=Sun Microsystems Inc., user.variant=, os.name=Windows 2000, hibernate.dbcp.whenExhaustedAction=1, sun.jnu.encoding=Cp1252, java.library.path=C:\java\jdk1.5.0_01\bin;.;C:\WINNT\system32;C:\WINNT;c:/Maven/bin;c:/java/jdk1.5.0_01/bin;C:\WINNT/system32;C:\WINNT;C:\WINNT/System32/Wbem;C:/Program Files/ATI Technologies/ATI Control Panel;C:/java/jdk1.5.0_01/bin;c:/ruby/bin;C:/mysql/bin;C:/php;C:/php/PEAR;C:/ant/bin, java.specification.name=Java Platform API Specification, java.class.version=49.0, sun.management.compiler=HotSpot Client Compiler, os.version=5.0, user.home=C:\Documents and Settings\swilliams, user.timezone=America/New_York, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=1.5, hibernate.dbcp.ps.whenExhaustedAction=1, hibernate.connection.driver_class=com.mysql.jdbc.Driver, show_sql=true, hibernate.dbcp.maxWait=120000, hibernate.show_sql=true, java.vm.specification.version=1.0, java.home=C:\java\jdk1.5.0_01\jre, sun.arch.data.model=32, hibernate.connection.url=jdbc:mysql://localhost/propDB, hibernate.dialect=org.hibernate.dialect.MySQLDialect, user.language=en, java.specification.vendor=Sun Microsystems Inc., hibernate.dbcp.maxIdle=10, awt.toolkit=sun.awt.windows.WToolkit, hibernate.cglib.use_reflection_optimizer=false, java.vm.info=mixed mode, java.version=1.5.0_01, java.ext.dirs=C:\java\jdk1.5.0_01\jre\lib\ext, sun.boot.class.path=C:\java\jdk1.5.0_01\jre\lib\rt.jar;C:\java\jdk1.5.0_01\jre\lib\i18n.jar;
C:\java\jdk1.5.0_01\jre\lib\sunrsasign.jar;C:\java\jdk1.5.0_01\jre\lib\jsse.jar;
C:\java\jdk1.5.0_01\jre\lib\jce.jar;C:\java\jdk1.5.0_01\jre\lib\charsets.jar;
C:\java\jdk1.5.0_01\jre\classes, java.vendor=Sun Microsystems Inc., hibernate.dbcp.maxActive=100, file.separator=\, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, hibernate.dbcp.ps.maxWait=120000, sun.desktop=windows, hibernate.dbcp.ps.maxIdle=100, dialect=org.hibernate.dialect.MySQLDialect, sun.cpu.isalist=}
16:44:11,389 DEBUG Configuration:998 - Preparing to build session factory with filters : {}
16:44:11,389 INFO Configuration:875 - processing extends queue
16:44:11,399 INFO Configuration:879 - processing collection mappings
16:44:11,399 DEBUG HbmBinder:2466 - Second pass for collection: com.foo.pojo.PersistenceTarget.properties
16:44:11,409 DEBUG HbmBinder:2482 - Mapped collection key: ID, index: NAME, element: VALUE
16:44:11,409 INFO Configuration:888 - processing association property references
16:44:11,409 INFO Configuration:917 - processing foreign key constraints
16:44:11,419 DEBUG Configuration:964 - resolving reference to class: com.foo.pojo.PersistenceTarget
16:44:11,980 INFO SettingsFactory:77 - RDBMS: MySQL, version: 4.0.21-debug
16:44:11,980 INFO SettingsFactory:78 - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.0.14-production ( $Date: 2004/04/24 15:49:43 $, $Revision: 1.27.2.39 $ )
16:44:12,070 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled
16:44:12,070 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled
16:44:12,080 INFO SettingsFactory:136 - JDBC batch size: 15
16:44:12,100 INFO SettingsFactory:139 - JDBC batch updates for versioned data: disabled
16:44:12,100 INFO SettingsFactory:144 - Scrollable result sets: enabled
16:44:12,110 DEBUG SettingsFactory:148 - Wrap result sets: disabled
16:44:12,110 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): enabled
16:44:12,110 INFO SettingsFactory:160 - Connection release mode: null
16:44:12,110 INFO SettingsFactory:184 - Maximum outer join fetch depth: 2
16:44:12,110 INFO SettingsFactory:187 - Default batch fetch size: 1
16:44:12,120 INFO SettingsFactory:191 - Generate SQL with comments: disabled
16:44:12,120 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled
16:44:12,120 INFO SettingsFactory:334 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
16:44:12,150 INFO SettingsFactory:203 - Query language substitutions: {}
16:44:12,150 INFO SettingsFactory:209 - Second-level cache: enabled
16:44:12,150 INFO SettingsFactory:213 - Query cache: disabled
16:44:12,150 INFO SettingsFactory:321 - Cache provider: org.hibernate.cache.EhCacheProvider
16:44:12,160 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled
16:44:12,160 INFO SettingsFactory:237 - Structured second-level cache entries: disabled
16:44:12,180 INFO SettingsFactory:257 - Echoing all SQL to stdout
16:44:12,180 INFO SettingsFactory:261 - Statistics: disabled
16:44:12,190 INFO SettingsFactory:265 - Deleted entity synthetic identifier rollback: disabled
16:44:12,190 INFO SettingsFactory:279 - Default entity-mode: pojo
16:44:12,530 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/Documents%20and%20Settings/swilliams/.maven/repository/ehcache/jars/ehcache-1.1.jar!/ehcache-failsafe.xml
org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.foo.pojo.PersistenceTarget.setProperties
org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.foo.pojo.PersistenceTarget.setProperties
at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:203)
at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:173)
at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2919)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:225)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at com.foo.test.PropertiesTest.testProperties(PropertiesTest.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: net.sf.cglib.beans.BulkBeanException: org.hibernate.collection.PersistentMap
at com.foo.pojo.PersistenceTarget$$BulkBeanByCGLIB$$de231fd6.setPropertyValues(<generated>)
at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:200)
... 28 more
Caused by: java.lang.ClassCastException: org.hibernate.collection.PersistentMap
... 30 more
16:44:13,332 DEBUG PropertiesTest:47 - In tearDown().....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 6:46 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
You are required to use collections framework interfaces for your POJO instance variables. This is because Hibernate assigns its own implementations of those interfaces to those variables. If you don't do this, you will get ClassCastExceptions, as you have seen.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 9:30 am 
Newbie

Joined: Thu Jul 28, 2005 4:14 pm
Posts: 2
Thanks, Nebob.


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.