-->
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.  [ 2 posts ] 
Author Message
 Post subject: Can I put @id into abstract class ? Or @overide?
PostPosted: Sun Dec 19, 2010 6:30 am 
Beginner
Beginner

Joined: Wed Oct 08, 2008 10:59 am
Posts: 37
I have created an abstract class and multiple classes which derive from the parent class.

The actual

long id;

field is in the abstract parent class.

Where should I put now the

@id
and
@Generatevalue

annotations? In the abstract class or in the child class(es) with additional @Override annotation?

Peter


Top
 Profile  
 
 Post subject: Re: Can I put @id into abstract class ? Or @overide?
PostPosted: Wed Dec 22, 2010 4:06 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
The JPA2 spec has this as an example in section 2.11.1 Abstract Entity Classes:
Code:
@Entity
@Table(name="EMP")
@Inheritance(strategy=JOINED)
public abstract class Employee {
  @Id protected Integer empId;
  @Version protected Integer version;
  @ManyToOne protected Address address;
  ...
}

@Entity
@Table(name="FT_EMP")
@DiscriminatorValue("FT")
@PrimaryKeyJoinColumn(name="FT_EMPID")
public class FullTimeEmployee extends Employee {
  // Inherit empId, but mapped in this class to FT_EMP.FT_EMPID
  // Inherit version mapped to EMP.VERSION
  // Inherit address mapped to EMP.ADDRESS fk
  // Defaults to FT_EMP.SALARY
  protected Integer salary;
  ...
}

@Entity
@Table(name="PT_EMP")
@DiscriminatorValue("PT")
// PK column is PT_EMP.EMPID due to PrimaryKeyJoinColumn default
public class PartTimeEmployee extends Employee {
  protected Float hourlyWage;
  ...
}

If you're not using @DiscriminatorValue and are simply refactoring common entity patterns out into an abstract base class, you'll want to have a look at @AttributeOverrides and @AttributeOverride for differing column names (e.g. your @Id property in your abstract base class maps to two different column names depending on the table). So you'd stick those @AttributeOverrides in your subclasses in case you need to tweak the mappings a bit. Other than that, you need to specify that you want to include mappings in your abstract base class by marking it with @MappedSuperclass (actually the next section 2.11.2 in the spec). That whole chapter is actually pretty good and should cover your specific use case better than I could.


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