-->
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: Strange hibernate behaviour
PostPosted: Thu May 31, 2007 7:13 am 
Newbie

Joined: Wed May 23, 2007 10:06 am
Posts: 5
Hello, I am trying to make project for studies and I am doing everything as tutorial says but I am getting strange exception:

org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [szzs.domain.registration.TaskTerm#203]
at org.hibernate.impl.SessionFactoryImpl$1.handleEntityNotFound(SessionFactoryImpl.java:377)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:79)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:68)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:140)
at szzs.domain.registration.TaskTerm$$EnhancerByCGLIB$$3122036f.getSubject(<generated>)
at szzs.tests.HBtest.testRegisterProject(HBtest.java:105)
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:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
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)



In situation like this:
Code:
Long idp, idt, idc, idr1, idr2;

        Session session1 = HibernateUtil.openSession();
    session1.beginTransaction();
        ProjectGroup p1=new ProjectGroup();
        TaskTerm t1=new TaskTerm();
        ConsultationTerm c1=new ConsultationTerm();
        Registry r1=new Registry();
        Registry r2=new Registry();

        t1.setSubject("TestSubject");
        t1.getMembers().put(p1, r1);

        c1.setSubject("TestSubjectofCON");
        c1.getMembers().put(p1,r2);

        idp=(Long) session1.save(p1);
        idt=(Long) session1.save(t1);
        idc=(Long) session1.save(c1);
        idr1=(Long) session1.save(r1);
        idr2=(Long) session1.save(r2);
        session1.getTransaction().commit();
    session1.close();

    Session session2 = HibernateUtil.openSession();
    session2.beginTransaction();

       ProjectGroup p2=(ProjectGroup)session2.load(ProjectGroup.class, idp);
        TaskTerm t2=(TaskTerm) session2.load(TaskTerm.class, idt);
        ConsultationTerm c2=(ConsultationTerm) session2.load(ConsultationTerm.class, idc);

         for (Engagement t : p2.getTaskTerms().keySet()){
                 System.out.println(t.getSubject());   <<<<<<<<<< Here the exception is thrown, moreover p2.getTaskTerms().keySet()) has 2 elements whereas it should have just one
         }


My classes are (plus getters/setter and unimportant fields):

Code:
public abstract class Engagement {
        private Long id;
        private Map <ProjectGroup, Registry> members =new HashMap <ProjectGroup, Registry> ();
}

public class ConsultationTerm extends Engagement {
}

public class TaskTerm extends Engagement{
}

public class ProjectGroup  extends RegistrationGroup{
        private Map  <TaskTerm, Registry > TaskTerms= new HashMap <TaskTerm, Registry>();//inverse
        private Map  <ConsultationTerm, Registry > consultationTerms= new HashMap <ConsultationTerm, Registry>();//inverse
}

public class RegistrationGroup {
        protected Long id;
}

public class Registry {
private Long id;
}


with mapping:

Code:
<class name="szzs.domain.registration.Engagement" abstract="true">
                <id name="id" column="ENGAGEMENT_ID">
                <generator class=...></id>

        <map name="members">
              <key column="ENGAGEMENT_ID" not-null="true"/>
              <map-key-many-to-many column="PROJECTGROUP_ID" class="szzs.domain.registration.ProjectGroup"/>
              <one-to-many class="szzs.domain.registration.Registry"/>
        </map>

        <union-subclass name="szzs.domain.registration.TaskTerm" table="TASKTERMS">
        </union-subclass>

        <union-subclass name="szzs.domain.registration.ConsultationTerm" table="CONSULTATIONTERMS">
        </union-subclass>
</class>

<class name="szzs.domain.registration.RegistrationGroup" table="REGISTRATIONGROUPS">
        <id name="id" column="REGISTRATIONGROUP_ID">
                <generator class=...></id>

        <joined-subclass name="szzs.domain.registration.ProjectGroup" table="PROJECTGROUPS">
                <key column="PROJECTGROUP_ID"/>

                <map name="taskTerms" inverse="true">
                     <key column="PROJECTGROUP_ID" not-null="true"/>
                     <map-key-many-to-many column="ENGAGEMENT_ID" class="szzs.domain.registration.TaskTerm"/>
                     <one-to-many class="szzs.domain.registration.Registry"/>
                </map>

                <map name="consultationTerms" inverse="true" >
                      <key column="PROJECTGROUP_ID" not-null="true"/>
                      <map-key-many-to-many column="ENGAGEMENT_ID" class="szzs.domain.registration.ConsultationTerm"/>
                      <one-to-many class="szzs.domain.registration.Registry"/>
                </map>
        </joined-subclass>
</class>

<class name="szzs.domain.registration.Registry" table="REGISTRIES">
                <id name="id" column="REGISTRY_ID">
                <generator class=...></id>
</class>



I really ask for help how should i map my classes to make them work. For me it doesnt matter if i have to keep each of ProjectGroup - ConsultationTerm and ProjectGroup -TaskTerm associations information in different table, or to have one bonus column to remember to which class is the association (like in many-to-any). Still i would like to preserve explicite polymorphism, but not for all cost ofc.


Top
 Profile  
 
 Post subject: Strange hibernate behaviour
PostPosted: Thu May 31, 2007 4:29 pm 
Newbie

Joined: Wed May 16, 2007 4:13 pm
Posts: 5
I've only been using hibernate a few months, but it looks like hibernate can't figure out how to set foreign keys. What generator are you using? Your example just has "...". I've been using <generator class="identity" /> with much success.

Also, you probably should change your relationship

<one-to-many class="szzs.domain.registration.Registry"/>

to be more like

<set name="whatever-you-call-it-in-parent" cascade="all" lazy="false">
<key column="id" />
<one-to-many
class="szzs.domain.registration.Registry" />
</set>

I found the association mapping section in the documentation to be very helpful, see

http://www.hibernate.org/hib_docs/v3/re ... erence.pdf


Hopefully this will get you going a little further with hibernate!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 01, 2007 3:36 pm 
Newbie

Joined: Wed May 23, 2007 10:06 am
Posts: 5
I am using hilo id generator, like that:

Code:
   <id name="id" column="ENGAGEMENT_ID">
       <generator class="hilo">
         <param name="table">hi_value</param>
         <param name="column">next_value</param>
         <param name="max_lo">100</param>
   </id>


When I changed it to identity i got:

Initial SessionFactory creation failed.org.hibernate.MappingException: Cannot use identity column key generation with <union-subclass> mapping for: szzs.domain.registration.Engagement
Exception in thread "main" java.lang.ExceptionInInitializerError
at szzs.common.utils.HibernateUtil.<clinit>(HibernateUtil.java:17)
at szzs.tests.bla.main(bla.java:18)
Caused by: org.hibernate.MappingException: Cannot use identity column key generation with <union-subclass> mapping for: szzs.domain.registration.Engagement
at org.hibernate.persister.entity.UnionSubclassEntityPersister.<init>(UnionSubclassEntityPersister.java:67)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:61)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at szzs.common.utils.HibernateUtil.<clinit>(HibernateUtil.java:13)
... 1 more

so most probably one can't use generotar identity witch union-classes;

I already read everything in the manual about assoiations and unfortunately found no information considering my problem.

Unfortunately in map tag one cannot use set tag, so it can't help there.

Therefore still i have not clue on what to do.... Pls help


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.