Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Extending an entity

If you subclass one Entity from another you have Entity inheritance which can
only be of type SINGLE_TABLE, JOINED or TABLE_PER_CLASS.  It looks like you
are getting JOINED inheritance defined, in that you new User will share the
superclass FOOUSER table and its own MYAPPUSER table, so only the new fields
are stored in the subclass table.

You may instead want to user TABLE_PER_CLASS, or better, create an
AbstractUser as a MappedSuperclass that defines the common fields and
behavior and subclass both foo User and myapp User from it. 

See,
http://en.wikibooks.org/wiki/Java_Persistence/Inheritance


Kim L wrote:
> 
> I have class named AbstractEntity, which is annotated with
> @MappedSuperclass. Then I have a class named User (@Entity) which extends
> AbstractEntity. Both of these exist in a package named foo.bar.framework.
> When I use these two classes, everything works just fine. But now I've
> imported a jar containing these files to another project. I'd like to
> reuse the User class and expand it with a few additional fields. I thought
> that @Entity public class User extends foo.bar.framework.User would do the
> trick, but I found out that this implementation of the User only inherits
> the fields from AbstractEntity, but nothing from foo.bar.framework.User.
> The question is, how can I get my second User class to inherit all the
> fields from the first User entity class?
> 
> Both User class implementation have different table names defined with
> @Table(name = "name").
> 
> My classes look like this
> 
> ================
> package foo.bar.framework;
> 
> @MappedSuperclass
> abstract public class AbstractEntity {
> 
>    @Id
>    @GeneratedValue(strategy = GenerationType.AUTO)
>     protected Long id;
> 
>     @Column(nullable = false)
>     @Version
>     protected Long consistencyVersion;
> 
>     ...
> }
> ================
> package foo.bar.framework;
> 
> @Entity
> @Table(name = "foouser")
> public class User extends AbstractEntity {
> 
>     protected String username;
> 
>     protected String password;
> 
>     ....
> }
> 
> ================
> package some.application;
> 
> @Entity
> @Table(name = "myappuser")
> public class User extends foo.bar.framework.User {
> 
>     protected String firstname;
> 
>     protected String lastname;
> 
>     protected String email;
> 
>     ....
> }
> ================
> 
> With the code above, EclipseLink will create a table named "myappuser"
> containing the fields "id", "consistencyVersion", "firstname", "lastname"
> and "email". The fields "username" and "password" are not created to the
> table - and that is the problem I'm having.
> 


-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://old.nabble.com/Extending-an-entity-tp27996492p28001220.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top