-->
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.  [ 12 posts ] 
Author Message
 Post subject: Discriminator & ManyToOne
PostPosted: Tue Aug 01, 2006 4:43 am 
Newbie

Joined: Tue Aug 01, 2006 4:02 am
Posts: 8
Hi,

Code:
@Entity
@Table(catalog="lookup",schema="dbo", name = "lookup_table")
@DiscriminatorColumn(
    name="tablename",
    discriminatorType=DiscriminatorType.STRING
)
public class LookupTable implements ILookup {   
    /**
     * Primary key.
     */   
   @Id
    @Column(name = "codeID")
   private Integer id;
...


Code:
@Entity
@DiscriminatorValue("persoon")
public class TypePersoonCode extends LookupTable implements Serializable{   

}


With these entity, if i query the database, the discriminator are perfectly working. I get a sql like this :
select ... from lookup.dbo.lookup_table typepersoo0_ where typepersoo0_.tablename='persoon' and typepersoo0_.codeID=?

It's returning 1 row, ok.

Now in an other entity, i try to add a ManyToOne column to this lookup.

Code:
@Entity
@Table(catalog="params", schema="dbo", name = "paramspersoonbis")
public class ParamsPersoonBis implements Serializable {
   
   @Id
   @Column(name="persoonID")
   private Integer persoonId;

   @ManyToOne
    @JoinColumn(name = "typepersooncodeID")
   private TypePersoonCode typePersoonCodeId;
...


But when i query the db (with a simple .find(class,id)), the generated sql don't use the discriminatorValue:

Code:
select ... from params.dbo.paramspersoonbis paramspers0_, lookup.dbo.lookup_table typepersoo1_ where paramspers0_.typepersooncodeID *= typepersoo1_.codeID and paramspers0_.persoonID=?


And I receive multiple row exception for the typepersoon table/class,
( if i add manually "and typepersoo0_.tablename='persoon' " it's working. )

Did i forget something, or discriminator don't work with relational class?
(with special record i also try wih onetoon relation)


Top
 Profile  
 
 Post subject: Re: Discriminator & ManyToOne
PostPosted: Tue Aug 01, 2006 4:12 pm 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Icy wrote:
And I receive multiple row exception for the typepersoon table/class,
( if i add manually "and typepersoo0_.tablename='persoon' " it's working. )


How can you get multiple rows?
Isn't there just one ParamsPersoonBis with certain id?
What can typepersoo0_.tablename='persoon' change?

What's that '*' in the query?

Rgds, Ales


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 3:09 am 
Newbie

Joined: Tue Aug 01, 2006 4:02 am
Posts: 8
thanks for your interest to my pbm...

Yes, ParamsPersoonBis is unique by id. Problems comes when I add the ManyToOne join.
Without this relationship, i get 1 row for a "simple" .find(ParamsPersoonBis.class,3).
When adding the ManyToOne relationship on the TypePersoonCode entity (which has a discriminator), hibernate is creating this *= select, but doesn't add the discriminator stuff , and then the lookup table return more than one row (what is normal, there are multiple "tablename" column with a certain ID, it's why I use the discriminator mechanism).

I don't know what exactly *= is. result look the same with or without the *, so it's not important. Main problem is that Hibernate doesn't add the and typepersoo0_.tablename='persoon' , has it should normally(i think) do it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 3:33 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
You are missing @Inheritance(strategy = InheritanceType.SINGLE_TABLE).
But I don't know if this is neccessary when you already have @DiscriminatorColumn.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 3:49 am 
Newbie

Joined: Tue Aug 01, 2006 4:02 am
Posts: 8
No, it doesn't change anything :/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 5:15 am 
Newbie

Joined: Tue Aug 01, 2006 4:02 am
Posts: 8
it's finally working.
I specify the fetch type :
Code:
   @ManyToOne(fetch = FetchType.LAZY)


FetchType.Lazy is normaly the default value (see in doc)... don't know why i have to specify it...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 5:42 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Do you get 3 rows in sql with that query or 3 objects in Java?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 7:49 am 
Newbie

Joined: Tue Aug 01, 2006 4:02 am
Posts: 8
in entitymanager.find(ParamsPersoonBis.class,3), 3 is an exemple of id(primary key), not the number of rows ;)

I received more than 500 rows


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 7:53 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Icy wrote:
3 is an exemple of id(primary key), not the number of rows ;)


Knew that, just had some #3 in my head about your multiple rows.

Then 500+ in sql rows or java objects?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 02, 2006 8:36 am 
Newbie

Joined: Tue Aug 01, 2006 4:02 am
Posts: 8
alesj wrote:
Icy wrote:
3 is an exemple of id(primary key), not the number of rows ;)


Knew that, just had some #3 in my head about your multiple rows.

Then 500+ in sql rows or java objects?


500+ rows(even if execute in a db manager), due to missing discriminator option in the sql.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 03, 2006 6:03 am 
Newbie

Joined: Thu Aug 03, 2006 5:47 am
Posts: 1
I meet the same issue concerning the use of @Discriminator and @ManyToOne annotations.
OK, it works with fetch type LAZY!

But in my case I have to specify the fetch type to EAGER.
@ManyToOne(fetch = FetchType.EAGER)
And It does not work!

Is there a solution?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
EAGER is the default for @ManyToOne as per the spec
There is no need to add the discriminator in the restriction clause because your id column should already be unique

_________________
Emmanuel


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