-->
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: Mapping a column to several tables!?
PostPosted: Mon Sep 02, 2013 1:33 pm 
Newbie

Joined: Mon Sep 02, 2013 12:11 pm
Posts: 10
Hallo community,

I´m a newbie according Hibernate.

I have following table:

ID | Parent | Type | RefID
...
2 | 1 | Section | 2
3 | 1 | Part | 1
4 | 2 | Section | 1
4 | 3 | Lang | 5

Let me explain shortly.
In this table there is a child-parent-relationship (ID and Parent). Column "Type" refers a row to several tables (e.g. table "Section", "Part" and "Lang"). Column "RefId" is the uniqe identifier in the according table.

There is a "one-to-one" relation between "Type"-"RefId" and "RefId" on a row.

Is there a possibility to map these issue with Hibernate? Can one map a column to several tables with Hibernate?
I don´t find an answer unti now.

I´m looking forward.

Thanks in advance.

PS:I´m using Hibernate 3.6.7 and Java 7.


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Tue Sep 03, 2013 7:15 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
I think the @Any annotation might be what you are looking for:
http://stackoverflow.com/questions/217831/how-to-use-hibernate-any-related-annotations
The article also has a link to this:
http://www.jroller.com/eyallupu/entry/hibernate_the_any_annotation

I think it will become something like this:
Code:
public class Parent {
  @Any( metaColumn = @Column( name = "Type" ), fetch=FetchType.EAGER )
  @AnyMetaDef(idType = "string", metaType = "string",
      metaValues = {
        @MetaValue(targetEntity = Section.class, value = "Section"),
        @MetaValue(targetEntity = Part.class, value = "Part")
             @MetaValue(targetEntity = Lang.class, value = "Lang")
   })
   @JoinColumn( name = "RefID" )
   Child child;
}


Assuming the Section, Part and Lang are entities that extend a common superclass named Child.


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Tue Sep 03, 2013 11:41 am 
Newbie

Joined: Mon Sep 02, 2013 12:11 pm
Posts: 10
Hi davided80,

thx a lot for your answer.
This is definitly I´m looking for!!!! @Any is the solution.

I tried to use @Any but a problem appeard.
Annoation @Any is not recognized. There is a failure with a message "Any cannot be resolved to a type".

I import following packages to my project:

Code:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.*;
import org.hibernate.*;


What´s wrong with that? I looked at some examples I founded at google. The examples shall work with these packages.

Can you give a hint to solve my problem!?

THX


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Tue Sep 03, 2013 2:57 pm 
Newbie

Joined: Mon Sep 02, 2013 12:11 pm
Posts: 10
I solved it...I updated Hibernate.

Can you explain "Assuming the Section, Part and Lang are entities that extend a common superclass named Child.".
Of course i have to use a common class. In the examples were used datype object. But it doesn´t work.

Please can you explain how to use.

THX


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Wed Sep 04, 2013 5:42 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
In the Parent class I used as example, the child attribute is of type Child. Child should be the superclass of Part, Section and Lang.
I was just trying to explain what Child is.
It is not a requirement to have a superclass for Part, Section and Lang. It should also work if you declare the attribute child as Object.

What kind of error do you have? Could you gimme more details, for example you could show me the entities class you are using.

By the way in my previous snippet I used
Code:
@AnyMetaDef(idType = "string", metaType = "string",


but if RefID is of type long, for example, you should change it to
Code:
@AnyMetaDef(idType = "long", metaType = "string",


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Wed Sep 04, 2013 7:33 am 
Newbie

Joined: Mon Sep 02, 2013 12:11 pm
Posts: 10
Great to hear you, now it works!
I declared child as datytype object.

After all I can read data out of my table. For this I use:

Code:
public class Parent{
  @Any( metaColumn = @Column( name = "Type" ), fetch=FetchType.EAGER )
  @AnyMetaDef(idType = "long", metaType = "string",
      metaValues = {
        @MetaValue(targetEntity = Section.class, value = "Section"),
        @MetaValue(targetEntity = Part.class, value = "Part")
             @MetaValue(targetEntity = Lang.class, value = "Lang")
   })
   @JoinColumn( name = "RefID" )
   private Object obj_Object;

   ...
}

So I can get table "Parent" with columns and "obj_Object".
Code:
Query query = session.createQuery("select l from Parent l");
ArrayList objects = (ArrayList)query.list();

The "obj_Object" shows me the belonging table (e.g. Section) with columns. But entry of the columns is empty, for every "obj_Object".
Relation between tables with RefId is ok.

I can figured out SectionId with this code. That works! SectionId is reloading out of database.
Code:
Parent test = (Parent ) objects.get(0);   
Section test2= (Section) test.getObj_Object();
test2.getSectionId();

This seems behaviour of lazy-lodaing. First table "Parent" is loading and then (when needed) sub-table with entries is loading. But I use "fetch=FetchType.EAGER". When I try "fetch=FetchType.LAZY" it´s same behaviour.

What do I wrong?
I want to load table "Parent" and all subtables with entities at one time. I want to load eager not lazy!!

How can I do this?

THX in advance.


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Wed Sep 04, 2013 8:31 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
It seems that to make it work you need to add the discriminators value in the query.
This should work:
Code:
List<Parent> results = session
              .createQuery( "SELECT p FROM Parent p WHERE p.child.class IN (:discriminators)" )
              .setParameterList( "discriminators", new String[]{"Section", "Parent", "Lang"} )
              .list();


The parameters used in the discriminators variable are the values of the attribute "value" that you set using the annotation @MetaValue on the "child" attribute:
Code:
...@MetaValue(..., value="Section")...


"class" is a special attribute for element annotated with @Any

Please, let me know if this works.


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Wed Sep 04, 2013 9:01 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
It seems my previous solution is not actually working.

I'm still loking into it


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Wed Sep 04, 2013 9:18 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
It seems this is a hibernate bug: https://hibernate.atlassian.net/browse/HHH-3574


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Wed Sep 04, 2013 9:23 am 
Newbie

Joined: Mon Sep 02, 2013 12:11 pm
Posts: 10
Thanks for answering my question so fast!!!

I tried if it worked.
But it appeared same behaviour

I get a result with 0 rows with this snippet.
Code:
List<Parent> results = session
              .createQuery( "SELECT p FROM Parent p WHERE p.child.class IN (:discriminators)" )
              .setParameterList( "discriminators", new String[]{"Section", "Parent", "Lang"} )
              .list();

I get a correct result of rowcount with no entities in subtable with this snippet.
Code:
List<Parent> results = session
              .createQuery( "SELECT p FROM Parent p WHERE p.child.class IN ("Section", "Parent", "Lang")" )
              .list();


So it´s same result I described above.
Can you write me a link that explain this or have you another idea to solve!? I didn´t find similiar.
Is loading eager possible in combination with @Any?

THX for your response.


Top
 Profile  
 
 Post subject: Re: Mapping a column to several tables!?
PostPosted: Wed Sep 04, 2013 9:29 am 
Newbie

Joined: Mon Sep 02, 2013 12:11 pm
Posts: 10
I read your link. It´s definitly a bug.

To solve the problem one should do a second select.

Thanks very much for your support!!!


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.