-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: object references an unsaved transient instance
PostPosted: Fri Dec 08, 2006 5:54 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
Please help me with following question, I am looking for several days now for an answer, I scanned a lot of forum-messages, in several languages, even languages I don't understand.

Thanks in advance
Bert

Hibernate version: 3.0.5

Mapping documents: 2 hbm-files:
----------------------
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.xxx.rm.common.archetyped.Archetyped" table="archetyped">
<id name="id" type="long">
<generator class="identity"/>
</id>
<property name="referenceModelVersion" column="reference_model_version" type="string" />
<many-to-one name="archetypeID" column="archetype_id" cascade="all" />
</class>
<query name="find.archetyped.by.archetypeid">
from org.xxx.rm.common.archetyped.Archetyped as a where a.archetypeID.value = ?
</query>
</hibernate-mapping>
-------------------------
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.openehr.rm.support.identification.ObjectID"
abstract="true"
table="object_id">
<id name="id" type="long">
<generator class="identity"/>
</id>
<discriminator/>
<property name="value" column="_value" type="string"/>
<subclass name="org.xxx.rm.support.identification.ArchetypeID"
discriminator-value="ArchetypeID"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
I use the springframework-class: HibernateDaoSupport:
public class ArchetypedDaoImpl extends HibernateDaoSupport
implements ArchetypedDao {

In this class I have a method save:
public void save(Archetyped archetyped) {
getHibernateTemplate().save(archetyped);
getHibernateTemplate().flush();
}
and a method find:
public Archetyped findByArchetypedID(ArchetypeID archetypedid){
List ret = getHibernateTemplate().findByNamedQuery(
"find.archetyped.by.archetypeid", archetypedid);
if(ret.isEmpty()) {
return null;
}
return (Archetyped) ret.get(0);
}



Name and version of the database you are using:

MySQL 4.1.10a

Full stack trace of any exception that occurs:

org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: org.openehr.rm.support.identification.ArchetypeID; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.openehr.rm.support.identification.ArchetypeID
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: org.openehr.rm.support.identification.ArchetypeID
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:99)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:69)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1115)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1177)
at org.hibernate.loader.Loader.doQuery(Loader.java:390)
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 org.springframework.orm.hibernate3.HibernateTemplate$32.doInHibernate(HibernateTemplate.java:893)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:365)
at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:884)
at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQuery(HibernateTemplate.java:880)
at se.acode.openehr.dao.orm.ArchetypedDaoImpl.findByArchetypedID(ArchetypedDaoImpl.java:37)
at se.acode.openehr.dao.orm.ArchetypedDaoTest.testFind(ArchetypedDaoTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



The generated SQL (show_sql=true):
please look at debug-log below

Debug level Hibernate log excerpt:

The DEBUG-log from the save-action
DEBUG [main] (SessionFactoryUtils.java:329) - Opening Hibernate Session
DEBUG [main] (SessionImpl.java:250) - opened session at timestamp: 4774172045312001
DEBUG [main] (DefaultSaveOrUpdateEventListener.java:159) - saving transient instance
DEBUG [Finalizer] (ConnectionManager.java:369) - running Session.finalize()
DEBUG [main] (AbstractSaveEventListener.java:133) - saving [org.xxx.rm.common.archetyped.Archetyped#<null>]
DEBUG [main] (AbstractSaveEventListener.java:195) - executing insertions
DEBUG [main] (Cascades.java:836) - processing cascade ACTION_SAVE_UPDATE for: org.xxx.rm.common.archetyped.Archetyped
DEBUG [main] (Cascades.java:153) - cascading to saveOrUpdate: org.xxx.rm.support.identification.ArchetypeID
DEBUG [main] (AbstractSaveEventListener.java:410) - transient instance of: org.xxx.rm.support.identification.ArchetypeID
DEBUG [main] (DefaultSaveOrUpdateEventListener.java:159) - saving transient instance
DEBUG [main] (AbstractSaveEventListener.java:133) - saving [org.xxx.rm.support.identification.ArchetypeID#<null>]
DEBUG [main] (AbstractSaveEventListener.java:195) - executing insertions
DEBUG [main] (BasicEntityPersister.java:1732) - Inserting entity: org.xxx.rm.support.identification.ArchetypeID (native id)
DEBUG [main] (AbstractBatcher.java:290) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [main] (ConnectionManager.java:296) - opening JDBC connection
DEBUG [main] (AbstractBatcher.java:324) - insert into object_id (_value, class) values (?, 'ArchetypeID')
DEBUG [main] (AbstractBatcher.java:378) - preparing statement
DEBUG [main] (BasicEntityPersister.java:1612) - Dehydrating entity: [org.xxx.rm.support.identification.ArchetypeID#<null>]
DEBUG [main] (NullableType.java:59) - binding 'zorggemak-ehr_rm-entry.biochemistry_result_cholesterol-cholesterol.2' to parameter: 1
DEBUG [main] (IdentifierGeneratorFactory.java:37) - Natively generated identity: 36
DEBUG [main] (AbstractBatcher.java:298) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [main] (AbstractBatcher.java:416) - closing statement
DEBUG [main] (Cascades.java:861) - done processing cascade ACTION_SAVE_UPDATE for: org.xxx.rm.common.archetyped.Archetyped
DEBUG [main] (BasicEntityPersister.java:1732) - Inserting entity: org.xxx.rm.common.archetyped.Archetyped (native id)
DEBUG [main] (AbstractBatcher.java:290) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [main] (AbstractBatcher.java:324) - insert into archetyped (reference_model_version, archetype_id) values (?, ?)
DEBUG [main] (AbstractBatcher.java:378) - preparing statement
DEBUG [main] (BasicEntityPersister.java:1612) - Dehydrating entity: [org.xxx.rm.common.archetyped.Archetyped#<null>]
DEBUG [main] (NullableType.java:59) - binding '1.0' to parameter: 1
DEBUG [main] (NullableType.java:59) - binding '36' to parameter: 2
DEBUG [main] (IdentifierGeneratorFactory.java:37) - Natively generated identity: 36
DEBUG [main] (AbstractBatcher.java:298) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [main] (AbstractBatcher.java:416) - closing statement
DEBUG [main] (Cascades.java:836) - processing cascade ACTION_SAVE_UPDATE for: org.xxx.rm.common.archetyped.Archetyped
DEBUG [main] (Cascades.java:861) - done processing cascade ACTION_SAVE_UPDATE for: org.xxx.rm.common.archetyped.Archetyped
DEBUG [main] (HibernateAccessor.java:393) - Eagerly flushing Hibernate session
DEBUG [main] (AbstractFlushingEventListener.java:52) - flushing session
DEBUG [main] (AbstractFlushingEventListener.java:102) - processing flush-time cascades
DEBUG [main] (Cascades.java:836) - processing cascade ACTION_SAVE_UPDATE for: org.xxx.rm.common.archetyped.Archetyped
DEBUG [main] (Cascades.java:153) - cascading to saveOrUpdate: org.xxx.rm.support.identification.ArchetypeID
DEBUG [main] (AbstractSaveEventListener.java:394) - persistent instance of: org.xxx.rm.support.identification.ArchetypeID
DEBUG [main] (DefaultSaveOrUpdateEventListener.java:103) - ignoring persistent instance
DEBUG [main] (DefaultSaveOrUpdateEventListener.java:140) - object already associated with session: [org.xxx.rm.support.identification.ArchetypeID#36]
DEBUG [main] (Cascades.java:861) - done processing cascade ACTION_SAVE_UPDATE for: org.xxx.rm.common.archetyped.Archetyped
DEBUG [main] (AbstractFlushingEventListener.java:150) - dirty checking collections
DEBUG [main] (AbstractFlushingEventListener.java:167) - Flushing entities and processing referenced collections
DEBUG [main] (AbstractFlushingEventListener.java:203) - Processing unreferenced collections
DEBUG [main] (AbstractFlushingEventListener.java:217) - Scheduling collection removes/(re)creates/updates
DEBUG [main] (AbstractFlushingEventListener.java:79) - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
DEBUG [main] (AbstractFlushingEventListener.java:85) - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
DEBUG [main] (Printer.java:83) - listing entities:
DEBUG [main] (Printer.java:90) - org.xxx.rm.support.identification.ArchetypeID{value=zorggemak-ehr_rm-entry.biochemistry_result_cholesterol-cholesterol.2, id=36}
DEBUG [main] (Printer.java:90) - org.xxx.rm.common.archetyped.Archetyped{referenceModelVersion=1.0, archetypeID=org.xxx.rm.support.identification.ArchetypeID#36, id=36}
DEBUG [main] (AbstractFlushingEventListener.java:267) - executing flush
DEBUG [main] (AbstractFlushingEventListener.java:294) - post flush
DEBUG [main] (SessionFactoryUtils.java:785) - Closing Hibernate Session
DEBUG [main] (SessionImpl.java:269) - closing session
DEBUG [main] (ConnectionManager.java:317) - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG [main] (JDBCContext.java:283) - after transaction completion
DEBUG [main] (SessionImpl.java:403) - after transaction completion
DEBUG [main] (SessionFactoryUtils.java:329) - Opening Hibernate Session
DEBUG [main] (SessionImpl.java:250) - opened session at timestamp: 4774172045950976
DEBUG [Finalizer] (ConnectionManager.java:369) - running Session.finalize()
DEBUG [main] (HibernateAccessor.java:393) - Eagerly flushing Hibernate session
DEBUG [main] (SessionFactoryUtils.java:785) - Closing Hibernate Session
DEBUG [main] (SessionImpl.java:269) - closing session
the DEBUG-log from the find action
DEBUG [main] (SessionFactoryUtils.java:329) - Opening Hibernate Session
DEBUG [main] (SessionImpl.java:250) - opened session at timestamp: 4774172046012416
DEBUG [main] (SessionImpl.java:829) - find:
from org.xxx.rm.common.archetyped.Archetyped as a where a.archetypeID.value = ?

DEBUG [main] (QueryParameters.java:221) - parameters: [org.xxx.rm.support.identification.ArchetypeID#null]
DEBUG [main] (QueryParameters.java:224) - named parameters: {}
DEBUG [Finalizer] (ConnectionManager.java:369) - running Session.finalize()
DEBUG [main] (QueryTranslatorImpl.java:113) - compile() : The query is already compiled, skipping...
DEBUG [main] (AbstractBatcher.java:290) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [main] (ConnectionManager.java:296) - opening JDBC connection
DEBUG [main] (AbstractBatcher.java:324) - select archetyped0_.id as id, archetyped0_.reference_model_version as reference2_1_, archetyped0_.archetype_id as archetype3_1_ from archetyped archetyped0_, object_id archetypei1_ where archetyped0_.archetype_id=archetypei1_.id and archetypei1_._value=?
DEBUG [main] (AbstractBatcher.java:378) - preparing statement
DEBUG [main] (AbstractBatcher.java:298) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [main] (AbstractBatcher.java:416) - closing statement
DEBUG [main] (JDBCContext.java:322) - after autocommit
DEBUG [main] (SessionImpl.java:403) - after transaction completion
DEBUG [main] (SessionFactoryUtils.java:785) - Closing Hibernate Session
DEBUG [main] (SessionImpl.java:269) - closing session
DEBUG [main] (ConnectionManager.java:317) - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG [main] (JDBCContext.java:283) - after transaction completion
DEBUG [main] (SessionImpl.java:403) - after transaction completion



Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 6:16 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Do you you have reference to unsaved ArchetypeID when you save Archetyped class? I f this is the case, you need to explicitly save ArchetypeID transient instance via Session.save() or map this association with cascade="save-update" attribute, bacause Hibernate don't cascading operations by default.

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 6:22 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
lester wrote:
Do you you have reference to unsaved ArchetypeID when you save Archetyped class? I f this is the case, you need to explicitly save ArchetypeID transient instance via Session.save() or map this association with cascade="save-update" attribute, bacause Hibernate don't cascading operations by default.


Thanks lester!!

I have arranged the mapping from Archetyped to ArchetypID like this:
<many-to-one name="archetypeID" column="archetype_id" cascade="all" />

I further want to add:
In Archetyped and ArchetypedID I have a private-property "id", in both cases with public getter/setters


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 6:39 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
verhees wrote:
Thanks lester!!

I have arranged the mapping from Archetyped to ArchetypID like this:
<many-to-one name="archetypeID" column="archetype_id" cascade="all" />

I further want to add:
In Archetyped and ArchetypedID I have a private-property "id", in both cases with public getter/setters


Sorry for inattention. What is the name and type of discriminator column in table object_id ?

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 6:43 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
no problemn, can happen to everyone ;-)

The name of the discriminator-column is "class" and the type is varchar(255)

regards
Bert


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 6:48 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
An example of a row in Archetyped

id, reference_model_version, archetype_id

36, '1.0', 36

and the corresponding in ArchetypeID

id, class, _value, original_version_id

36, 'ArchetypeID', 'zorggemak-ehr_rm-entry.biochemistry_result_cholesterol-cholesterol.2',null


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 7:09 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Sorry, if i don't understan all clear. How i can see in mapping, you trying to execute query:

from org.xxx.rm.common.archetyped.Archetyped as a where a.archetypeID.value = ?

and pass the parameter with type ArchetypeID in find method. But in query you check plain property of the ArchetypeID named value with type string, not the entity type. Is this correct?

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 7:19 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
Lester, it seems that you are right, this error-code was left over when experimenting to find a solution. Sorry for that

I changed it, but now get another error, see stacktrace:
(maybe you have an idea? I put in the following mail the debug-log, but I have a business talk right now)

the code I do:
(function archetypeid("1") returns the expected archetypeid-object

Archetyped ret1 = dao.findByArchetypedID(archetypeid("1"));
//check if null
assertNotNull(ret1);
//following line causes the exception
System.out.println("ret1:"+ret1.getArchetypeID().getValue());


org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at org.openehr.rm.support.identification.ArchetypeID$$EnhancerByCGLIB$$1fc873.getValue(<generated>)
at se.acode.openehr.dao.orm.ArchetypedDaoTest.testSave(ArchetypedDaoTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 7:20 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
Oh, change the find-function like this

public Archetyped findByArchetypedID(ArchetypeID archetypedid){
List ret = getHibernateTemplate().findByNamedQuery(
"find.archetyped.by.archetypeid", archetypedid.getValue());
if (ret.isEmpty()) {
return null;
}
return (Archetyped) ret.get(0);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 7:37 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
It seems, you trying to access property of the lazy association when session already closed, in that case you have only proxy to the association, not the actual object. You must fetch association eagerly, using one of the following approaches:

1. Use fetch join in HQL for example:

from org.xxx.rm.common.archetyped.Archetyped as a fetch join org.openehr.rm.support.identification.ArchetypeID as aId where aId.value = ?

2. Use Hibernate.initialize() inside session:

Hibernate.initialize(archetyped.getArchetypeID())

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 10, 2006 7:42 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
Thanks, lester,

But I have to study your answer a bit more. I get a syntax-error on this query. Maybe you know what it is, else, I will try to find out myself.
Anyway, you have put me on a track.

If you do not know about. this syntax-error, I will report back later, what I ahve found out about is. First I have to do quite some reading about HQL.

The syntax error is:
ERROR [main] (SessionFactoryImpl.java:300) - Error in named query: find.archetyped.by.archetypeid
org.hibernate.hql.ast.QuerySyntaxError: expecting "all", found 'join' near line 4, column 78 [


from org.openehr.rm.common.archetyped.Archetyped as at fetch join org.openehr.rm.support.identification.ArchetypeID as aId where aId.value = ?
]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:215)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)

etc

etc
etc


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 10, 2006 9:35 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
Lester, I tried the second approach, and got following exception with stacktrace.

By the way, I don't know why the session is closed, i just posted two objects In the same procedure, the same dao-object, I try to return one back, and it says the session is closed

org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at org.openehr.rm.support.identification.ArchetypeID$$EnhancerByCGLIB$$76428f54.getValue(<generated>)
at se.acode.openehr.dao.orm.ArchetypedDaoTest.testSave(ArchetypedDaoTest.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 6:13 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
Hi, lester, there is something wrong, both approaches you offered do not work.

In the end, I changed the function like this, maybe this information helps:

public Archetyped findByArchetypedID(ArchetypeID archetypedid){
ret = getHibernateTemplate().find("from org.openehr.rm.common.archetyped.Archetyped at where at.archetypeID.value = ?",archetypedid.getValue());

System.out.println(ret.size());

if (ret.isEmpty()) {
return null;
}
archetyped = (Archetyped) ret.get(0);
return archetyped;
}
-------------
(The blue parameter "archetyped" I made a class-variable, because I thought, it maybe was destroyed because of the local context inside the function (when one is indesparation, one tries a lot))

You see the line in green, it gives the right size of the list to expect back.

But as soon as I ask something about the first object from this list, like

System.out.println(ret.get(0).toString());

I get a an exception like is printed below. What I don't understand is that I do not get this exception at the time of the query, and that the query returns the right size.

I really do not understand this problem, and it causes me a lot of trouble


org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at org.openehr.rm.support.identification.ArchetypeID$$EnhancerByCGLIB$$76428f54.hashCode(<generated>)
at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:392)
at org.openehr.rm.common.archetyped.Archetyped.hashCode(Archetyped.java:106)
at java.lang.Object.toString(Unknown Source)
at se.acode.openehr.dao.orm.ArchetypedDaoImpl.findByArchetypedID(ArchetypedDaoImpl.java:51)
at se.acode.openehr.dao.orm.ArchetypedDaoTest.testSave(ArchetypedDaoTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 6:23 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
About first approach:

Yes, syntax of HQL query was incorrect, sorry. Right syntax is:

from org.openehr.rm.common.archetyped.Archetyped as archt left join fetch archt.archetypeID as archtID where archtID.value = ?

About second approach - how i understand from your code, you trying to access proxy property outside of the session and DAO in the test, that's why you recive LazyInitializationException in this line:

System.out.println("ret1:"+ret1.getArchetypeID().getValue());

After query execution, when you get reference to Archetyped you must execute Hibernate.initialize() on it inside Session!

Did you use Spring Framework for DAO support? If this is the case, you must remember that session remains open only for getHibernateTemplate() method execution, after that method completes, session is closed!

You must use:

(Archetyped) hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Archetyped archtyped = session.find(...) // Find Archtyped
Hibernate.initialize(archtyped);
return archtyped;
}
});

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 10:20 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 9:06 am
Posts: 29
It works, for others to learn, I write down the function as it is now, I hope it is readable.

Thanks very much, lester. Quite some strange code, but I will get used to it.

Thanks again!!!


public Archetyped findByArchetypedID(ArchetypeID archetypedid){
final String id = archetypedid.getValue();
HibernateTemplate template = getHibernateTemplate();
return (Archetyped) template.execute(
new HibernateCallback()
{
public Object doInHibernate(Session session) throws HibernateException, SQLException
{
Query q = session.createQuery("from org.openehr.rm.common.archetyped.Archetyped at where at.archetypeID.value = :archetypeid");
q.setString("archetypeid", id);
List ret = q.list();
System.out.println(ret.get(0).toString());
return (Archetyped) ret.get(0);
}
}
);
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.