-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping a hierarchy as "disjoint" entities
PostPosted: Wed Jun 20, 2007 6:47 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 1:44 pm
Posts: 27
Hi all,

I have a hierarchy where class B extends class A. Both are entities:

Code:
@Entity
class A {
}

@Entity
class B extends A{
}


with the above annotations hibernate will try to map A and B as a SINGLE_TABLE hierarchy.Also, with TABLE_PER_CLASS it will forbid AUTO ids. But as I don't care to treat A and B polymorphically it would be fine for me to just map them as different classes, disregarding their inheritance relationship. Something like that could be achieved, although a bit laboriously, as:

Code:
@MappedSuperclass
abstract class  X {
}

@Entity
class A extends X{
}

@Entity
class B extends X {
}


Is there any way to do the same thing avoiding this somewhat synthetic abstract class expedient.

Thank you in advance
Cheers,
Carlos


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 8:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I have hard time to believe that A and B are stored in the same table (SINGLE_TABLE). You might have make a mistake somewhere.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 8:49 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 1:44 pm
Posts: 27
emmanuel wrote:
I have hard time to believe that A and B are stored in the same table
(SINGLE_TABLE). You might have make a mistake somewhere.


Believe it or not here is the schema autogenerated for a couple of simple classes in the inheritance relationship described in my previous post. Note that fields of both classes are mapped to the same table. Also, the other inheritance mappings are not an option, I don't want inheritance from hibernate point of view at all. I got that with the abstract @MappedSuperclass workaround, but I think there should be an easier way.

Code:
@Entity
public class A {
   
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private Integer fieldOfA;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }   
}
@Entity
public class B extends A {

    private Integer fieldOfB;
}

Code:
CREATE MEMORY TABLE A(

DTYPE VARCHAR(31) NOT NULL,

ID INTEGER GENERATED BY DEFAULT  AS IDENTITY(START WITH 1) NOT NULL PRIMARY KEY,

FIELDOFA INTEGER,

FIELDOFB INTEGE R)


Cheers,
Carlos


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 10:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
That's different than what you described. In you last example B extends A

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 11:57 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 1:44 pm
Posts: 27
emmanuel wrote:
That's different than what you described. In you last example B extends A


You're not getting my point I guess. Originally I had:

Code:
@Entity
class A {
  // class A stuff
}

@Entity
class B {
  // class B stuff
}


But this mapped everything (both class A stuff and class B stuff) to the same table. I could use joined tables or table per class instead, but anyway I don't care about mapping the hierarchy, just about reusing A stuff inside B at java level. And not without reasons, suppose I map the hierarchy as:

1) single table per hierarchy: I don't want a table mixing A stuff and B stuff
2) table per class: that's fine, but I can't autogenerate ids anymore.
3) joined tables: don't want the additional join

So, not finding another way, I refactor the above to:

Code:
@MappedSuperclass
abstract class X {
  // class A stuff
}

@Entity class A extends X {
}

@Entity class B extends X {
  // class B stuff
}

Now I reutilize class A stuff and both A and B are mapped to different classes without any id generation restriction.

Cheers,
Carlos


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 21, 2007 3:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
ah right i read too fast :)

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.