2

Motivation

My app makes requests to some web service based on contents of table foo. I would like to log those requests (in a data base) as well as value of row of foo, which was used to make them. Still, as data in table foo can change over time, I need to log exact contents of row (rather then its id). I am using Hibernate so I thought, I could embed row of foo in entity representing request. To have clean class hierarchy I would like to have something like this:

 class Foo
 {
   /*
    * Here goes some properties of Foo, which specify columns names etc.
    */
 }

 @Embeddable
 class EmbeddableFoo extends Foo
 {
   //EmbeddableFoo should have the same properties as Foo
 }

 @Entity
 class EntityFoo extends Foo
 {
   @Id
   Long getId() { //EntityFoo  has some id }
   //EntityFoo should have the same properties as Foo, except that
   //it has id
 }

Problem

This will not work, because EmbeddableFoo doesn't see properties of Foo. I can add @Inheritance annotation over Foo, so that EntityFoo *sees* properties of Foo, but it will not solve my problems with EmbeddableFoo

Question

Is there an easy (i.e. not requiring to write a lot of redundant code) way to have @Embeddable and @Entity* inherit from the same class?

1 Answer 1

2

have you checked the annotation @MappedSuperclass ? that might be what you are looking for.

http://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html

Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.

A class designated with the MappedSuperclass annotation can be mapped in the same way as an entity except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself. When applied to the subclasses the inherited mappings will apply in the context of the subclass tables. Mapping information may be overridden in such subclasses by using the AttributeOverride and AssociationOverride annotations or corresponding XML elements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.