-->
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.  [ 5 posts ] 
Author Message
 Post subject: Why are some annotated properties being ignored?
PostPosted: Tue Jul 11, 2006 10:47 am 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Lately I've been finding instances where the annotation configuration is ignoring annotated properties on my classes. For example:

Code:
public class SomeClass {

  @Id
  @Column(...)
  private Long firstEntityId;

  @Id
  @Column(...)
  private Long secondEntityId;

  @ManyToOne
  @PrimaryKeyJoinColumn(...)
  private FirstEntity firstEntity;

  @ManyToOne
  @PrimaryKeyJoinColumn(...)
  private SecondEntity secondEntity;

  @Id
  @Basic(...)
  private Integer sequence;

}


1. Annotation configuration completely ignores the "sequence" property. If I try this,

Code:
public class FirstEntity {

  @OneToMany
  @OrderBy( "sequence" )
  private List<SomeClass> blah = new LinkedList<SomeClass>();

}


then I get "property from @OrderBy clause not found: SomeClass.sequence".

2. It seems redundant to have two properties for a @ManyToOne mapping; one for the primary key part, the other for the actual entity. Is this necessary by design, or is there another way to map them? I've tried,

Code:
  @Id @ManyToOne private FirstEntity firstEntity;


but again, it completely ignores this property and I get errors later on! This approach honestly seems more intuitive to me, but assuming I'm wrong, having annotations ignore the property and reporting general "property not found" errors later on is frustrating. Am I simply doing all this wrong, or, if I could find the time (!! :), would it be helpful for me to submit patches for additional error checking/validation to the hibernate-annotations code?


Top
 Profile  
 
 Post subject: SOLN: Surrogate key
PostPosted: Fri Jul 21, 2006 1:42 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
It seems to be ignoring ID properties when the developer (me) doesn't specify them properly. I decided just to add a surrogate key to the table and be done with it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 3:21 pm 
Beginner
Beginner

Joined: Wed Apr 26, 2006 2:41 pm
Posts: 30
The @Id annotation represents the primary key of the class... not every column.

You also dont need to store the id and the object in SomeClass... just the object will do.

Read more here: http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#mapping

Code:
public class SomeClass {

  @Id
  @Column(...)
  private Long id; // this is the primary key of SomeClass

  @ManyToOne
  @JoinColumn(...) // this is the foreign key (in the some_class table) to FirstEntity
  private FirstEntity firstEntity;

  @ManyToOne
  @JoinColumn(...)// this is the foreign key to SecondEntity
  private SecondEntity secondEntity;

  @Column(...) // this is just a column in the some_class table
  private Integer sequence;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 3:24 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Right, but the original design was a composite primary key, made up of two foreign keys. So there was no surrogate primary key, and the two @ManyToOne mappings made up the id. I didn't know how to properly map this without duplicating the foreign keys in the entity.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 21, 2006 3:35 pm 
Beginner
Beginner

Joined: Wed Apr 26, 2006 2:41 pm
Posts: 30
Ah.... I see now. Well the multiple @Id route is still wrong. I think the way you're supposed to do this is with an @Embedded primary key Java object and @IdClass to represent the dual foreign keys.


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