-->
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.  [ 10 posts ] 
Author Message
 Post subject: require help with annotations
PostPosted: Fri Jan 26, 2007 4:11 pm 
Newbie

Joined: Fri Jan 26, 2007 3:51 pm
Posts: 16
Hi there,

I'm trying to create a database schema using hbm2ddl tool on my first project.
However, I am getting the following exception:

[hibernatetool] javax.persistence.PersistenceException: org.hibernate.Annotation
Exception: mappedBy reference an unknown target entity property: scholastic.models.
User.administrator in scholastic.models.Administrator.instructors

I don't understand what it is I need to change in my annotations in order to make
this work. In my Administrator class I declare the following:

Code:
@Entity
@Table(name = "ADMINISTRATORS")
@PrimaryKeyJoinColumn(name = "ADMINISTRATOR_ID")
public class Administrator extends User implements Serializable
{
  ...
  @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "administrator")
  @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  @org.hibernate.annotations.CollectionOfElements
  @JoinTable(name = "INSTRUCTOR", joinColumns = @JoinColumn(name = "INSTRUCTOR_ID"))
  @org.hibernate.annotations.CollectionId(columns = @Column(name="ADMIN_INSTRUCTOR_ID"), type =    @org.hibernate.annotations.Type(type = "long"), generator = "sequence")
  private Collection<User> instructors = new ArrayList<User>();

  @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "administrator")
  @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  @org.hibernate.annotations.CollectionOfElements
  @JoinTable(name = "STUDENTS", joinColumns = @JoinColumn(name = "STUDENT_ID"))
  @org.hibernate.annotations.CollectionId(columns = @Column(name="ADMIN_STUDENT_ID"), type = @org.hibernate.annotations.Type(type = "long"), generator = "sequence")
  private Collection<User> students = new ArrayList<User>();
  ...
}


I'm not sure I have all the strings in quotes right.
Please understand the following hierarchy:

abstract User
|
|___ Administator extends User
|___ Instructor extends User
|___ Student extends User

As you can see, I want Administrator to have a collection of elements of type Instructor and Student (both of which are User type since both extend User just as Administrator does). I'm trying to make the hbm2ddl tool create seperate tables for each of the classes in the hierarchy including the abstract class User.

The Instructor class looks like so:
Code:
@Entity
@Table(name = "INSTRUCTORS")
@PrimaryKeyJoinColumn(name = "INSTRUCTOR_ID")
public class Instructor extends User implements Serializable
{
   @ManyToOne(targetEntity = scholastic.models.Administrator.class)
   @JoinColumn(name = "ADMINISTRATOR_ID", nullable = false)
   private Administrator administrator;
   ...
   public Administrator getAdministrator()
   {  return administrator;
   }

   public void setAdministrator(Administrator administrator)
   {
     this.administrator = administrator;
   }
   ...
}


The Student class is made in the same way.

Please advise,

Alan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 3:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Several problems:
you cannot let several associations points (mappedBy) to the same target property, it does not make sense
it does not make sense to define @Join* associations on the side marked as mappedby since the physycal representation will be determined by the other side

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Obscure Usability for Designers using Hybernate Annotations
PostPosted: Tue Feb 20, 2007 9:35 pm 
Newbie

Joined: Tue Feb 20, 2007 9:00 pm
Posts: 4
Hello Alan,

Quote:
I'm not sure I have all the strings in quotes right.
Please understand the following hierarchy:

abstract User
|
|___ Administator extends User
|___ Instructor extends User
|___ Student extends User

As you can see, I want Administrator to have a collection of elements of type Instructor and Student (both of which are User type since both extend User just as Administrator does). I'm trying to make the hbm2ddl tool create seperate tables for each of the classes in the hierarchy including the abstract class User.


I was having the same problem trying to create a @ManyToMany bidirectional relationship between two classes. I solved the problem by pointing to the class that contains the key attribute or the derived class. This was in the most base class in the hierachy instead of trying to reference the key attribute of the actual class you're trying to associate. It looks like that's what you're trying to do also in your code.

I think this is a bug in the design of annotations for hibernate as most good OO designers whould consider a base class key attribute an attribute of the derived class as it is in scope unless defined as private (public and protected are in the scope of derived classes).

I would appreciate it if one of the hibernate team members comment on this issue as I think this can be corrected without affect on existing developments but it will improve the usability of this excellent tool.

Regards,

Paul Cahill


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 1:38 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
nope this is not a bug, I had lengthy discussions on the forum and in JIRA about it, AFAIR

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 5:36 am 
Newbie

Joined: Tue Feb 20, 2007 9:00 pm
Posts: 4
[quote="emmanuel"]nope this is not a bug, I had lengthy discussions on the forum and in JIRA about it, AFAIR[/quote]
Hi Emmanuel,

As an experienced OO designer, I use UML which is the natural way to design large and small systems. Hibernate claims to take a POJO model and create a persistence tier transparently. My model is a real UML model. This is my starting point. When I draw an association link between two classes that will be related in a many to many fashion, I don't draw a line between one classes base class and my associated class when I want the derived class to be associated. This is effectively what is happening when you require designers to name the base class that contains the key attribute to be related class. This is very un-natural and cumbersome.

I think this needs more discussion as the JIRA group may not have this experience. I'm looking for the most natural way to work with Hibernate as I believe is the intent of the designers and I and many others will find this is a repeating problem in the future hence my claim that its a design bug even if it has made it into the specification.

Can you direct me to some of the discussions or could you give me some of your reasons for your point of view please? I'm not totally familiar with acronyms so would you please use plain English (AFAIR ??).

Regards,

Paul Cahill


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 6:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The short answer is...
In OO languages, there is no such notion of bidirectional relationship. It's always a bi- unidirectional relationship maintained by the user manually. When you start thinking about bidirectional relationship, the model you have chosen appears to be wrong.

PS http://www.acronymfinder.com is a fantastic site, it translates all weirdo English acronyms out there :-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 8:41 pm 
Newbie

Joined: Tue Feb 20, 2007 9:00 pm
Posts: 4
Hello Emmanuel,

Some things need a long answer....

What you point out here is true for OO languages however we're talking about OO design, modelling and transformation. The bi-directional relationship is implied and implemented in the translation from the model to the target OO language.

Your argument is a bit irrelevant with regard to the discussion I'm having here. I've been talking about any kind of relationship between two derived classes (several levels down an inheritance tree, in discussions in this forum called a parent [base class] child [derived class] relationship). From what I've been able to determine, if two derived classes have some kind of use relationship (aggregation or composition, etc) the hibernate syntax requires that you name the class and key attribute at the top of the inheritance tree as the mapped attribute. As I pointed out, this is absolutely clunky and is an artificial overhead to anyone doing real OO design.

Sure we need to perform some translation from a UML model to class annotations but it shouldn't be necessary to do the Hibernate's work in the first place. We should be able to name the same attribute in any of the derived classes and have Hibernate work it out as there will only be one attribute in scope with that name so Hibernate can work it out precisely without our help. Hibernate uses reflection to walk the class hierarchy so this is a simple thing to implement that will save a lot of confusion in the future and unnecessary training in the nuances of Hibernate.

Hibernate claims to be an OR tool that transparently maps a pure OO class hierarchy to persist in a relational world and is designed to remove the impedance mismatch between these two similar but disparate mediums. This issue introduces what amounts to an assembly language call out that is required to make it work and is a coders dream but a designer’s nightmare.

Please take another look.

Regards,

Paul Cahill


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 10:13 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Quote:
if two derived classes have some kind of use relationship (aggregation or composition, etc) the hibernate syntax requires that you name the class and key attribute at the top of the inheritance tree as the mapped attribute.


Hum, I read your comment too fast. Can you elaborate with a simple example. Definitely you can do an association between 2 derived classes provided that the property pointed in mappedBy is effectively part of the associated derived class (otherwise we fall to what I described before).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 28, 2007 3:51 am 
Newbie

Joined: Tue Feb 20, 2007 9:00 pm
Posts: 4
Image

This is an example of the kind of relationship that should be accepted. To make this work with Hibernate annotations, I need to name BASEB as the class owning the key attribute 'id'. I should only need to name DerivedB2 as the related class and name the key attribute of it which is still 'id'. I should not need to name detailed lineage of each class to create the mappings.

Regards,

Paul Cahill


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 01, 2007 2:46 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This kind of UML design is working. What is the Object model with annotation that fails?
I don't understand "I need to name BASEB as the class owning the key attribute 'id'.", but the code will clarify I think

_________________
Emmanuel


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