-->
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.  [ 4 posts ] 
Author Message
 Post subject: problem with inheritance and entities
PostPosted: Fri May 16, 2008 12:23 am 
Newbie

Joined: Thu May 15, 2008 11:58 pm
Posts: 3
Location: La Paz, Bolivia
I am using Hibernate Annotations 3.2.1 and Hibernate 3.2.4 from Jboss Seam 2.0.0.

I have four entity classes, lets call them A, AA, B and BB with the following relationships:

- AA extends A
- BB extends B
- A has a one to many relationship with B.

Now I want to add a restriction: If the entity class is A, it should have one to many relationship only with entities of class B; conversely, If the entity class is AA, it should have one to many relationship only with entities of class BB.

My first approximation was to create two generic mapped superclasses A0 and B0 and use them as a base this way:

Code:
@MapperSuperclass
class A0<X> {
...
   @OneToMany
   Set<X> getChildren() {
...
}

@MappedSuperclass
class B0<X> {
...
   @ManyToOne
   X getParent() {
...
}

@Entity
class A extends A0<B> {
...
}

@Entity
Class B extends B0<A> {
...
}

@Entity
class AA extends A0<BB> {
...
}

@Entity
Class BB extends B0<AA> {
...
}


but I am not happy with result because I see this as too convoluted and I lose the inheritance between (A and AA) and (B and BB).

How can I implement this structure?

_________________
Daniel Murguía


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 6:46 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
Having to override a relationship is not very clean IMHO.
if you think about the physical schema you are going to generate you see there is much unnecessary complexity.

I would advise you to NOT have AA extend A, if they have all other code in common they could both extend a same non-entity class. The same for B classes.
So you don't have to share the same table for parent and childs, this sharing would make it impossible to enforce your requirement at a DB level.

A completely different approach is you enforce you requirement at Business code level, not very nice.

regards,

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 12:16 pm 
Newbie

Joined: Thu May 15, 2008 11:58 pm
Posts: 3
Location: La Paz, Bolivia
Hi,

Thank you for your post. I should have said that I already have a database structure like this:

Code:
create table A(
   column id integer,
   column colA varchar(10),
   primary key(id)
);

create table AA(
   column id integer,
   column colAA varchar(10),
   primary key(id),
   foreign key(id) references A
);

create table B(
   column id integer,
   column colB varchar(10),
   column idA integer,
   primary key(id),
   foreign key idA references A
);

create table BB(
   column id integer,
   column colBB varchar(10),
   primary key(id),
   foreign key(id) references B
);


Because of restrictions of my project, you can regard this database schema as unchangeable.

As you can see, I can't enforce my restriction at the DB level unless I write some combination of triggers and stored procedures that I haven't analyzed yet. I still believe I can enforce the restriction at the Entity Bean level, tough.

_________________
Daniel Murguía


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 12:55 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
having seen your schema I would really suggest you to make 4 different entities,
than maybe use interfaces to define common types.

_________________
Sanne
http://in.relation.to/


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