-->
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 associated One To Many to inherited entity
PostPosted: Tue Apr 25, 2006 11:23 am 
Newbie

Joined: Tue Apr 25, 2006 10:32 am
Posts: 2
Hibernate version:3.1 Beta
Name and version of the database you are using:oracle 9i

I have an entity called "SCHOOL", which is associated (ONE TO MANY relation) to an entity called "STUDENT" that inherited from the abstract entity called "PERSON".
The code of the entities is like this:

The Person entity:
@Entity
@Table(name="PERSON")
@Inheritance(discriminatorValue="1", strategy=InheritanceType.SINGLE_TABLE, discrimunatorType=DiscriminatorType.INTEGER)
@DiscriminatorColumn(name="TYPE")
public abstract class Person imlements Serializable {
...
}

The Student entity:
@Entity
@Inheritance(discriminatorValue="2")
public class Student extends Person imlements Serializable {
...
}

The School entity:
@Entity
@Table(name="SCHOOL")
public class School imlements Serializable {
...
@OneToMany(targetEntity=Student.class, mappedBy="school")
public List<Student> getStudents() {
return students;
}
}

When I used merge to the master entity, I get all the kinds of Persons to the students list and not just the Students as I would expect.
I'm trying a lot, but couldn't undersatnd why it merges like this.
By the way, when I merge the Student without association it works great.

Thanks, Roni[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 1:03 pm 
Newbie

Joined: Wed Apr 19, 2006 2:51 pm
Posts: 16
The Person entity:
@Entity
@Table(name="PERSON")


@Inheritance(discriminatorValue="1", strategy=InheritanceType.SINGLE_TABLE, discrimunatorType=DiscriminatorType.INTEGER)
@DiscriminatorColumn(name="TYPE")


==> Try this : here you should NOT specify any descriminator for the parent abstract class since it has not to be instantiated ... only concrete subclasses need to be"descriminated" ...

Please keep me informed of the result !



public abstract class Person imlements Serializable {
...
}

The Student entity:
@Entity
@Inheritance(discriminatorValue="2")
public class Student extends Person imlements Serializable {
...
}

The School entity:
@Entity
@Table(name="SCHOOL")
public class School imlements Serializable {
...
@OneToMany(targetEntity=Student.class, mappedBy="school")
public List<Student> getStudents() {
return students;
}
}

_________________
Cheers,
Laurent

Thanks for rating in case it helped !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 10:37 am 
Newbie

Joined: Tue Apr 25, 2006 10:32 am
Posts: 2
Thank for your reply.

I have tried to create the abstract entity without the discriminator value to the Person entity as you offered, but it didn't pass compilation without this.
This is the reason I added this also to the abstract entity.
I'll happy to get other ideas..

Roni


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 5:24 pm 
Newbie

Joined: Wed Apr 19, 2006 2:51 pm
Posts: 16
Please provide your XML mapping files instead of xDoclet tags. I feel more confortable with that.

Anyway, try this : since the Person class is abstract you do not really need to map it to the table person.

Just map Student to PERSON table in the usual way (but keep the inheritance in your java code) ; in case you have an additional concrete subclass, map it also in the usual way independently from the Student class.

In the mapping files use the attribute where="descriminator=1" to descriminate Student from the other concrete subclass in Table Person.

This is a very correct work around ... (I recently faced a similar issue I could solved this way).

_________________
Cheers,
Laurent

Thanks for rating in case it helped !


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.