-->
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: Problems using EntityManager.find for JOINED subclasses
PostPosted: Wed Jul 26, 2006 7:53 am 
Newbie

Joined: Wed Jul 26, 2006 7:17 am
Posts: 7
Hello,

I have a strange problem to retrieve an Entity by ID from the EntityManager using its find() method:


Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Owner {
    Long id;
    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    ...
}

@Entity
public class DmsUser extends Owner {
     ...
}


There is one DmsUser persisted with id 3, all tables are generated automatically, no caches enabled, MySql, Hibernate 3.2.0.cr3

If i use
Code:
1.: DmsUser user = EntityManager.find(DmsUser.class, new Long(3))
returns NULL !
but
Code:
2.: DmsUser user = (DmsUser) EntityManager.find(Owner.class, new Long(3))
works.

The two executed SQL Queries differ a lot. If I change InheritanceType.JOINED to InheritanceType.SINGLE_TABLE the first example works again.

Is this the expected behavior of InheritanceType.JOINED and EntityManager.find that I have to pass the superclass there ?

Thank you, frobb

---- sql trace from example 1 ----
Code:
DEBUG [SQL]: select dmsuser0_.id as id0_0_, dmsuser0_1_.name as name0_0_, dmsuser0_.password as password1_0_, dmsuser0_.groupName as groupName1_0_, dmsuser0_.description as descript4_1_0_ from DmsUser dmsuser0_ inner join Owner dmsuser0_1_ on dmsuser0_.id=dmsuser0_1_.id where dmsuser0_.id=?


---- sql trace from example 2 ----
Code:
DEBUG [SQL]: select owner0_.id as id0_0_, owner0_.name as name0_0_, owner0_1_.password as password1_0_, owner0_1_.groupName as groupName1_0_, owner0_1_.description as descript4_1_0_, case when owner0_1_.id is not null then 1 when owner0_2_.id is not null then 2 when owner0_.id is not null then 0 end as clazz_0_ from Owner owner0_ left outer join DmsUser owner0_1_ on owner0_.id=owner0_1_.id left outer join USER_GROUP owner0_2_ on owner0_.id=owner0_2_.id where owner0_.id=?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 3:14 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
did you save the object using hibernate? your DB data are probably screwed up

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 4:21 am 
Newbie

Joined: Wed Jul 26, 2006 7:17 am
Posts: 7
Hello, i used Hibernate to persist the object. I boiled it down to the following test lines:

Test model classes:
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AbstractSuperclass{
    Long id;
    @Id @GeneratedValue
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
}

@Entity
public class Subclass extends AbstractSuperclass {}


Test Code:
Code:
   
        EntityManager firstSession = EntityManagerHelper.createSession();
        Subclass u = new Subclass();
        firstSession.getTransaction().begin();
        firstSession.persist(u);
        firstSession.getTransaction().commit();
        Long newId = u.getId();
        System.out.println("new ID is:" + newId);
        firstSession.close();

        EntityManager secondSession = EntityManagerHelper.createSession();
        secondSession.getTransaction().begin();

        // 1.
        Subclass result1 = secondSession.find(Subclass.class, newId);
        System.out.println("result is:" + result1);

        // 2.
        Subclass result2 = (Subclass) secondSession.find(AbstractSuperclass.class, newId);
        System.out.println("result is:" + result2);

        secondSession.getTransaction().commit();
        secondSession.close();

        EntityManagerHelper.shutdown();


Result is:

Code:
new ID is:1
result is:null
result is:com.riacom.dms.model.Subclass@3afb99


But it works (first result is != null) with SINGLE_TABLE. I think it may be a bug, should it be reported? Is there more information i should provide?

Frobb


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 2:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
attach a runnable version of your unit test case (please remove the helper class), and post it to JIRA. Also be sure that Subclass is actually part of the EMF creation

Thanks

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 31, 2006 8:02 am 
Newbie

Joined: Wed Jul 26, 2006 7:17 am
Posts: 7
Something interesting I found out:

The code works for JOINED subclasses for Hsqldb and Postgres, but fails for MySql. So changing my database to postgres solves the problem for me.
I added a bug report containing the test case ejb-208.

Frobb


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.