-->
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.  [ 11 posts ] 
Author Message
 Post subject: Annotations and inheritance
PostPosted: Mon Apr 25, 2005 9:20 am 
Newbie

Joined: Fri Feb 06, 2004 10:16 am
Posts: 7
Hibernate version:3.01

Hi,

I have a quetsion about annotations and inheritance. If I have an annotated base class like this (without the @Entity):

Code:
public class HibernateEntity {

   private long id;

   @Id(generate=GeneratorType.AUTO)
   @Column(name="id_")
   public long getId() {return id;}
   public void setId(long id) {this.id=id;}

   public int hashCode() {...}
   public boolean equals() {...}
}


Is it possible to annotate this class with the @Id, as in the example above, and let all my other classes extend this one like this:


Code:
@Entity(access=AccessType.FIELD)
@Table(name="myTable")
public class MyEntity extends HibernateEntity {

   @Column(name="myprop_")
   private String myProperty;

   public long getMyProperty() {return myProperty;}
   public void setId(String myProperty) {this.myProperty=myProperty;}
}



Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 7:49 am 
Newbie

Joined: Wed Mar 30, 2005 5:41 am
Posts: 6
Yes, it should be possible.

But you should use setMyProperty instead of setId in your MyEntity class.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 7:16 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
Did you ever solve this problem? I am in a similar situation where the AnnotationException complains because there is no identifier specified for the subclass . The docs only demonstrate how to inherit, and override, non-identifiers .


Top
 Profile  
 
 Post subject: Same here
PostPosted: Sun Oct 02, 2005 3:23 pm 
Newbie

Joined: Thu May 06, 2004 8:14 pm
Posts: 18
I'm using annotations 3.0-beta-5 and I'm having the exact same problem. No idea how to work around it...

mike


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 3:24 pm 
Newbie

Joined: Thu May 06, 2004 8:14 pm
Posts: 18
Of course I mean 3.1, not 3.0


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 3:33 pm 
Newbie

Joined: Thu May 06, 2004 8:14 pm
Posts: 18
I looked and didn't find anything in JIRA so I entered a bug for this.

http://opensource2.atlassian.com/projec ... se/ANN-106


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 4:11 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This works perfectly OK. As long as you remember that AccessType.FIELD requires annotations on the field, not on the getter method.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 6:28 pm 
Newbie

Joined: Thu May 06, 2004 8:14 pm
Posts: 18
Christian, note in the JIRA issue that I am using getter access. Anything else I should look out for?

christian wrote:
This works perfectly OK. As long as you remember that AccessType.FIELD requires annotations on the field, not on the getter method.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 8:44 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
All I can say is that it works perfectly fine with my CaveatEmptor source code and I believe that exactly this is in the annotation unit tests as well.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 1:12 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
mperham, try this.

Code:
import javax.persistence.EmbeddableSuperclass;
import javax.persistence.GeneratorType;
import javax.persistence.Id;

@EmbeddableSuperclass
public class DomainImpl implements Domain {

   protected String id;

   private String text;

   @Id(generate = GeneratorType.NONE)
   public String getId() {
      return id;
   }
   
   public void setId(String id) {
      this.id = id;
   }

   public String getText() {
      return text;
   }

   public void setText(String text) {
      this.text = text;
   }

}


Code:
import gov.blm.ak.model.DomainImpl;
import java.util.Set;
import javax.persistence.*;

@Entity
@Table(name = "assess_meth")
@AttributeOverrides( {
        @AttributeOverride(name="id", column = @Column(name = "assess_meth_cd", unique = false, nullable = false, insertable = true, updatable = true, length = 1, precision = 19, scale = 2)),
        @AttributeOverride(name="text", column = @Column(name = "assess_meth_txt", unique = false, nullable = false, insertable = true, updatable = true, length = 20, precision = 19, scale = 2) )
} )

public class AssessMeth extends DomainImpl implements gov.blm.ak.model.Domain,
      java.io.Serializable {

   private Set<gov.blm.ak.arims.generated.Study> studies;

   public AssessMeth() {}

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn(name = "assess_meth_cd")
   public Set<gov.blm.ak.arims.generated.Study> getStudies() {
      return this.studies;
   }

   public void setStudies(Set<gov.blm.ak.arims.generated.Study> studies) {
      this.studies = studies;
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 03, 2005 3:17 pm 
Newbie

Joined: Thu May 06, 2004 8:14 pm
Posts: 18
Dennis, you're a rock star. @EmbeddableSuperclass was what I was missing. Thank you!


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