You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Christian Eugster <ch...@gmx.net> on 2007/09/04 19:26:26 UTC

Id from superclass with different columnnames in subclasses

Hello,

I try to build a database on some hierarchical java-classes:

I have a superclass, say AbstractObject, with an identity field named id.
Subclasses inherit that field, but in the database tables I use for every subclass another name for the id-column. I cannot see how I can do that, because the annotation for the id field is done on the superclass. Is there a way to map the one and only id field in the superclass to the id columns in the database tables with different names?

I read the manual, but have not seen a solution for it. Do you know any?

Thanks!

Christian
-- 
****************************
Christian Eugster
Grissian Widum 14
I-39010 Tisens
--------------------------------------
Handy Schweiz: 0041 79 594 85 45
Handy Italia: 0039 333 888 77 64
Email: christian.eugster@gmx.net


Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger

Re: Id from superclass with different columnnames in subclasses

Posted by Christian Eugster <ch...@gmx.net>.
Thanks a lot!

Christian

(Sorry!)
-- 
****************************
Christian Eugster
Grissian Widum 14
I-39010 Tisens
--------------------------------------
Handy Schweiz: 0041 79 594 85 45
Handy Italia: 0039 333 888 77 64
Email: christian.eugster@gmx.net


GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail

Re: Id from superclass with different columnnames in subclasses

Posted by Patrick Linskey <pl...@gmail.com>.
Hi,

Specify the new column names using @AttributeOverride /
@AttributeOverrides annotations (or their corresponding XML):

> @Entity
> @Table(name="events_course")
> public class Course extends AbstractObject {
> // the inherited attribute 'id' from AbstractObject is named 'course_id' in
> // the table 'events_course'. How can I do that using annotations?
> }

@Entity
@Table(name="events_course")
@AttributeOverride(name="id", column="course_id")
public class Course extends AbstractObject {
    ...
}

> @MappingSuperclass
> public abstract class AbstractObject {

FTR, it's @MappedSuperclass, not @MappingSuperclass.

-Patrick

On 9/4/07, Christian Eugster <ch...@gmx.net> wrote:
> Hi Patrick,
>
> My Subclasses have only the 'id' and the 'version' field of the superclass in common, each has it own table and the tables primary key columns differ from each other. So the subclasses inherit all the id attribute from the superclass, but their corresponding column names differ.
>
> @MappingSuperclass
> public abstract class AbstractObject {
>   @Id
>   @GeneratedValue(strategy=GenerationType.IDENTITY)
>   private Long id;
> }
>
> @Entity
> @Table(name="events_course")
> public class Course extends AbstractObject {
> // the inherited attribute 'id' from AbstractObject is named 'course_id' in
> // the table 'events_course'. How can I do that using annotations?
> }
>
> @Entity
> @Table(name="events_customer")
> public class Customer extends AbstractObject {
> // the inherited attribute 'id' from AbstractObject is named 'customer_id'
> // in the table 'events_customer'. How can I do that using annotations?
> }
>
> @Entity
> @Table(name="events_booking")
> public class Booking extends AbstractObject {
> // the inherited attribute 'id' from AbstractObject is named 'booking_id'
> // in the table 'events_booking'. How can I do that using annotations?
> }
>
> Thank you in advance!
>
> Christian
>
>
> --
> ****************************
> Christian Eugster
> Grissian Widum 14
> I-39010 Tisens
> --------------------------------------
> Handy Schweiz: 0041 79 594 85 45
> Handy Italia: 0039 333 888 77 64
> Email: christian.eugster@gmx.net
>
> GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
> Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail
>


-- 
Patrick Linskey
202 669 5907

Re: Id from superclass with different columnnames in subclasses

Posted by Christian Eugster <ch...@gmx.net>.
Hi Patrick,

My Subclasses have only the 'id' and the 'version' field of the superclass in common, each has it own table and the tables primary key columns differ from each other. So the subclasses inherit all the id attribute from the superclass, but their corresponding column names differ.

@MappingSuperclass
public abstract class AbstractObject {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Long id;
}

@Entity
@Table(name="events_course")
public class Course extends AbstractObject {
// the inherited attribute 'id' from AbstractObject is named 'course_id' in 
// the table 'events_course'. How can I do that using annotations?
}

@Entity
@Table(name="events_customer")
public class Customer extends AbstractObject {
// the inherited attribute 'id' from AbstractObject is named 'customer_id'
// in the table 'events_customer'. How can I do that using annotations?
}

@Entity
@Table(name="events_booking")
public class Booking extends AbstractObject {
// the inherited attribute 'id' from AbstractObject is named 'booking_id'
// in the table 'events_booking'. How can I do that using annotations?
}

Thank you in advance!

Christian


-- 
****************************
Christian Eugster
Grissian Widum 14
I-39010 Tisens
--------------------------------------
Handy Schweiz: 0041 79 594 85 45
Handy Italia: 0039 333 888 77 64
Email: christian.eugster@gmx.net

GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/go/freemail

Re: Id from superclass with different columnnames in subclasses

Posted by Patrick Linskey <pl...@gmail.com>.
Hi,

>From a JPA specification standpoint, the right way to handle this is
to make the AbstractObject class be a @MappedSuperclass, instead of an
@Entity. That basically means that it's a persistent type, but not one
that manages its own identity. The limitation is that (per the spec)
you can't have relationships to @MappedSuperclass types, and you can't
query for them.

There are other OpenJPA-specific options that you could use, too, but
if it's appropriate for you, the @MappedSuperclass approach is
probably the best, from a portability standpoint.

-Patrick

On 9/4/07, Christian Eugster <ch...@gmx.net> wrote:
> Hello,
>
> I try to build a database on some hierarchical java-classes:
>
> I have a superclass, say AbstractObject, with an identity field named id.
> Subclasses inherit that field, but in the database tables I use for every subclass another name for the id-column. I cannot see how I can do that, because the annotation for the id field is done on the superclass. Is there a way to map the one and only id field in the superclass to the id columns in the database tables with different names?
>
> I read the manual, but have not seen a solution for it. Do you know any?
>
> Thanks!
>
> Christian
> --
> ****************************
> Christian Eugster
> Grissian Widum 14
> I-39010 Tisens
> --------------------------------------
> Handy Schweiz: 0041 79 594 85 45
> Handy Italia: 0039 333 888 77 64
> Email: christian.eugster@gmx.net
>
>
> Psssst! Schon vom neuen GMX MultiMessenger gehört?
> Der kanns mit allen: http://www.gmx.net/de/go/multimessenger
>


-- 
Patrick Linskey
202 669 5907