-->
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.  [ 11 posts ] 
Author Message
 Post subject: Dynamic models and 1-N mapping
PostPosted: Mon Mar 15, 2010 1:28 pm 
Newbie

Joined: Mon Mar 15, 2010 1:16 pm
Posts: 9
Hi,

I implemented a dynamic model as the following CUSTOMER(1)--(N)ACCOUNTS. I wrote the XML mapping as the following:

<hibernate-mapping>
<class entity-name="Customer" table="customer">
<id name="cId" column="c_id" type="long">
<generator class="native"/>
</id>
<property name="cName" column="c_name" type="string"/>

<set name="accounts" inverse="true" table="account">
<key column="c_id" not-null="true"/>
<one-to-many entity-name="Account"/>
</set>
</class>

<class entity-name="Account" table="account">
<id name="aId" column="a_id" type="long">
<generator class="native"/>
</id>
<property name="aName" column="a_name" type="string"/>

<many-to-one name="cId" column="c_id" entity-name="Customer" foreign-key="cId"/>
</class>
</hibernate-mapping>

Note that I only know the database schema at runtime so I can't have any compiled entity classes. My problem is that this works OK with mappings from N to 1, but doesn't work with mappings from 1 to N, i.e. collections. I tried googling about this but I can't find any example of such mapping using entity-names. The error that I am having is the following:

SEVERE: illegal access to loading collection
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)

The queries that are being generated are the following:

Hibernate: select customer0_.c_id as c1_5_, customer0_.c_name as c2_5_ from customer customer0_
Hibernate: select accounts0_.c_id as c3_1_, accounts0_.a_id as a1_1_, accounts0_.a_id as a1_6_0_, accounts0_.a_name as a2_6_0_, accounts0_.c_id as c3_6_0_ from account accounts0_ where accounts0_.c_id=?

Does anyone have any clue about what is going on? A working example of what I am trying to do would be great too.

Thanks in advance,

Rui Vilão


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Wed Mar 17, 2010 9:33 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
This exception in my opinion is not because of the mapping (which seems ok).
This exception in my opinion comes when working with more threads contemporanously on the same persistent objects (= with the same session / persistent context):
While one thread is initializing the collection (initializing == true) the other tries to access the same collection.

Code:
protected final void initialize(boolean writing) {
      if (!initialized) {
         if (initializing) {
            throw new LazyInitializationException("illegal access to loading collection");
         }
         throwLazyInitializationExceptionIfNotConnected();
         session.initializeCollection(this, writing);
      }
   }


Can it be that you are sharing a session between different threads ? (that should be avoided).


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Wed Mar 17, 2010 9:48 am 
Newbie

Joined: Mon Mar 15, 2010 1:16 pm
Posts: 9
Hi,

Thanks for replying. I'm using unit tests to get it to work. I added a @Before to clean the session factory but I am still getting the same error. What I am doing is the following:

Code:
    @Before
    public void init() {

        System.setProperty("derby.system.home", System.getProperty("user.dir") + "/db");

        out.println(System.getProperty("derby.system.home"));

        Configuration cfg =  new Configuration().configure(new File("test/test/hibernate/hibernate.xml"));

        cfg.addFile(new File("test/test/hibernate/mapping.xml"));

        sf = cfg.buildSessionFactory();
    }

    @After
    public void destroy() {
        out.println("SF CLOSED");
        sf.close();
    }

    @Test
    public void testInsert() {

        Session s = sf.openSession();

        Map miau = new HashMap();
        Map account = new HashMap();

        miau.put("cName", "Miau2");
        account.put("aName", "Miau2's Account");
        account.put("cId", miau);

        Transaction tx = s.beginTransaction();

        s.save("Customer", miau);
        s.save("Account", account);

        tx.commit();
        s.close();

        out.println("Out of insert!");
    }

    @Test
    public void testSelect() {
        Session s = sf.openSession().getSession(EntityMode.MAP);
       
        //List<Map> res = s.createQuery("SELECT a FROM Account a").list();
        List<Map> res = s.createQuery("SELECT c FROM Customer c").list();
       

        /*for(Map o : res) {
            out.println(o.get("aName"));
            out.println(o.get("aId"));
            MapProxy mp;
            mp = (MapProxy) o.get("cId");
            for(Object c : mp.keySet()){
                out.println("   " + mp.get(c));
            }
            out.println("#####################");
        }*/

        out.println(res);

        s.close();
    }


Thanks agin,

Rui Vilao


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Wed Mar 17, 2010 10:22 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Sorry, I'm not familiar with annotations in testcases (is this JUnit ?).
Could it be that your test-suite is configured for executing tests in concurrent mode (multiple threads) ?

Is the exception also raising if you use EntityMode.POJO instead to EntityMode.MAP ?


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Wed Mar 17, 2010 10:28 am 
Newbie

Joined: Mon Mar 15, 2010 1:16 pm
Posts: 9
Hi,

Yes this is JUnit.

I believe not because I tested using only one test (inserting and then selecting right after), moreover the output of the test comes in sequence.

With POJO throws the same exception.

Best regards,

Rui Vilao


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Wed Mar 17, 2010 10:56 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Can you please post the complete stacktrace of your org.hibernate.LazyInitializationException
and tell which hibernate version you are using.


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Wed Mar 17, 2010 10:59 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Do you eventually define a postload callback where you invoke the collection?
(invoking a lazy collection from the postload callback is not allowed)


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Wed Mar 17, 2010 11:07 am 
Newbie

Joined: Mon Mar 15, 2010 1:16 pm
Posts: 9
Hi,

The stack trace is:

SEVERE: illegal access to loading collection
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
SF CLOSED
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:411)
at java.util.HashMap$Entry.hashCode(HashMap.java:720)

So the error is first thrown from the HashMap class... strange.

I just try to print the List.

--
Rui Vilao


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Wed Mar 17, 2010 11:46 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
This stacktrace is still not complete. please provide the complete stacktrace (it must go until a code-line from your application)


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Wed Mar 17, 2010 11:53 am 
Newbie

Joined: Mon Mar 15, 2010 1:16 pm
Posts: 9
EDIT

I was looking at the wrong console...

Code:
SEVERE: illegal access to loading collection
org.hibernate.LazyInitializationException: illegal access to loading collection
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
        at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:411)
        at java.util.HashMap$Entry.hashCode(HashMap.java:720)
        at java.util.AbstractMap.hashCode(AbstractMap.java:461)
        at java.util.HashMap$Entry.hashCode(HashMap.java:720)
        at java.util.AbstractMap.hashCode(AbstractMap.java:461)
        at java.util.HashMap.put(HashMap.java:372)
        at java.util.HashSet.add(HashSet.java:200)
        at java.util.AbstractCollection.addAll(AbstractCollection.java:305)
        at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:329)
        at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:237)
        at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:222)
        at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:195)
        at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:877)
        at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:865)
        at org.hibernate.loader.Loader.doQuery(Loader.java:729)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
        at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
        at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
        at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
        at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
        at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
        at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:309)
        at java.lang.String.valueOf(String.java:2826)
        at java.lang.StringBuilder.append(StringBuilder.java:115)
        at java.util.AbstractMap.toString(AbstractMap.java:490)
        at java.lang.String.valueOf(String.java:2826)
        at java.lang.StringBuilder.append(StringBuilder.java:115)
        at java.util.AbstractCollection.toString(AbstractCollection.java:422)
        at java.lang.String.valueOf(String.java:2826)
        at java.lang.StringBuilder.append(StringBuilder.java:115)
        at test.hibernate.HibernateTest.testSelect(HibernateTest.java:88)
        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:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
        at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
        at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:515)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1031)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:888)
org.hibernate.LazyInitializationException: illegal access to loading collection
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
        at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:411)
        at java.util.HashMap$Entry.hashCode(HashMap.java:720)
        at java.util.AbstractMap.hashCode(AbstractMap.java:461)
        at java.util.HashMap$Entry.hashCode(HashMap.java:720)
        at java.util.AbstractMap.hashCode(AbstractMap.java:461)
        at java.util.HashMap.put(HashMap.java:372)
        at java.util.HashSet.add(HashSet.java:200)
        at java.util.AbstractCollection.addAll(AbstractCollection.java:305)
        at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:329)
        at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:237)
        at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:222)
        at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:195)
        at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:877)
        at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:865)
        at org.hibernate.loader.Loader.doQuery(Loader.java:729)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
        at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
        at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
        at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
        at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
        at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
        at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:309)
        at java.lang.String.valueOf(String.java:2826)
        at java.lang.StringBuilder.append(StringBuilder.java:115)
        at java.util.AbstractMap.toString(AbstractMap.java:490)
        at java.lang.String.valueOf(String.java:2826)
        at java.lang.StringBuilder.append(StringBuilder.java:115)
        at java.util.AbstractCollection.toString(AbstractCollection.java:422)
        at java.lang.String.valueOf(String.java:2826)
        at java.lang.StringBuilder.append(StringBuilder.java:115)
        at test.hibernate.HibernateTest.testSelect(HibernateTest.java:88)
        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:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
        at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
        at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:515)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1031)
        at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:888)


Hi,

Well the thing is that's impossible I believe... because this does not throw an exception to my part of the code. This exception I believe is being caught and handled at the hibernate level...

--
Rui


Top
 Profile  
 
 Post subject: Re: Dynamic models and 1-N mapping
PostPosted: Mon Apr 12, 2010 1:02 pm 
Newbie

Joined: Wed Jul 26, 2006 4:18 pm
Posts: 8
Location: Brussels, Belgium
I have the same or a similar problem : viewtopic.php?f=1&t=1003833&start=0
and I wonder whether this is linked to this, or not ?

The solution from Roberto looks OK to me but I have not managed to implement it thoroughly in my code :(

\T,


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