-->
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.  [ 5 posts ] 
Author Message
 Post subject: Trying to map an entity as a java.lang.Object
PostPosted: Fri Feb 17, 2006 8:18 pm 
Newbie

Joined: Wed Sep 28, 2005 12:46 pm
Posts: 3
I am trying to map an entity as a java.lang.Object. I need to do this because I want the "value" to contain different types of objects.

Using hibernate 3.0

I tried this mapping
Mapping documents:
<property name="Value"
column="VALUE"
type="java.lang.Object"
unique="false"
/>

But I was not able to get that to work. If anyone thinks this should work please let me know. I saw a post about something similar suggesting type="byte[]". I used that and I can get the mapping loaded. However, when I try to create and save (session.save(x)) an instance of the class to the database I get the following exception. The actual type of the object in the value field is a java.lang.String so I'm assuming hibernate doesn't like my mapping of byte[].

b]6:03:10,093 ERROR [main] (ValueFactory.java:53) - Error creating value for com.convera.adminApi.dataModel.properties.StringProperty with value of test string value Exception:java.lang.String
java.lang.ClassCastException: java.lang.String
at org.hibernate.type.BinaryType.deepCopyNotNull(BinaryType.java:115)
at org.hibernate.type.MutableType.deepCopy(MutableType.java:25)
at org.hibernate.type.TypeFactory.deepCopy(TypeFactory.java:290)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:227)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:95)
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.convera.adminApi.dataModel.values.ValueFactory.createValue(ValueFactory.java:84)
at com.convera.adminApi.dataModel.values.ValueFactory.createValue(ValueFactory.java:43)
at com.convera.adminApi.dataModel.values.ValuesTest.testCreateStringValue(ValuesTest.java:102)
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 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)
[/b]

Name and version of the database you are using:
MySql 3.1.10

I'd appreciate any help on this. So if anyone knows how to map an entity to just a plain java.lang.Object, please let me know.
Thanks
Doal Miller
[/b]

_________________
Doal Miller
Sr. Software Engineer
Convera


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 19, 2006 5:44 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
Are all your values Serializable? If so, have you tried using:-

<property name="Value"
column="VALUE"
type="serializable"
unique="false"
/>


Top
 Profile  
 
 Post subject: Trying to map an entity as a java.lang.Object
PostPosted: Tue Feb 21, 2006 3:09 pm 
Newbie

Joined: Wed Sep 28, 2005 12:46 pm
Posts: 3
Thanks for your suggestion. All the objects that go into the "value" field are Serializable. They are mostly wrapper objects (Integer, Boolean, Double, etc...) and String.

Using type="serializable" I got to the point where I can create the containing objects in the database, and everything looks good there. The problem I am having now is I get an exception when I try to retrieve the containing objects from the database. Exception is below. AbstractValue is the containing object (note that its a concrete class now). "value" is the field which contains the entity that is being mapped as serializable.

Any help or suggestions will be appreciated. Thanks

Mapping documents:
<property name="Value"
column="VALUE"
type="serializable"
unique="false"
/>
org.hibernate.PropertyAccessException: Exception occurred inside setter of com.convera.adminApi.dataModel.values.AbstractValue.Value
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:51)
at org.hibernate.tuple.AbstractTuplizer.setPropertyValues(AbstractTuplizer.java:207)
at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:176)
at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2919)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:530)
at org.hibernate.loader.Loader.doQuery(Loader.java:436)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)
at org.hibernate.loader.Loader.list(Loader.java:1577)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:395)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:844)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at com.convera.adminApi.dataModel.values.ValueFactory.getValueById(ValueFactory.java:229)
at com.convera.adminApi.dataModel.values.ValueFactory.deleteValue(ValueFactory.java:176)
at com.convera.adminApi.dataModel.values.ValueFactory.deleteValue(ValueFactory.java:156)
at com.convera.adminApi.dataModel.values.ValuesTest.testCreateStringValue(ValuesTest.java:116)
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 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: java.lang.reflect.InvocationTargetException
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 org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:40)
... 30 more
Caused by: java.lang.NullPointerException
at com.convera.adminApi.dataModel.values.AbstractValue.setValue(AbstractValue.java:80)
... 35 more

_________________
Doal Miller
Sr. Software Engineer
Convera


Top
 Profile  
 
 Post subject: Trying to map an entity as a java.lang.Object
PostPosted: Tue Feb 21, 2006 3:31 pm 
Newbie

Joined: Wed Sep 28, 2005 12:46 pm
Posts: 3
Again thanks for you help. It all seems to be working now. My last problem was caused by some code of mine that wasn't protecting against null values.

So type="serializable" looks like the way to map entitys to generic "object" type.

Thanks

_________________
Doal Miller
Sr. Software Engineer
Convera


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 1:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
word of warning...

be very careful doinf this. The approach is fine for primitive wrappers and such as you stated, but is problematic for classes which might change their class def and/or serial version between storing and subsequent loading


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