Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@MappedSuperclass is not an @Entity in JPA?

So I am using DerbyDB and I am setting up some entities. I have a @MappedSuperclass which is used as a superclass for some entities (@Entity). More specifically, I have a superclass User and 3 subclasses namely admin, regular and guest. Now I have a different entity, let's say file that should reference (as one of its fields) its owner. So I created a field called User owner. The error I get is:

Exception Description: [File] uses a non-entity [User] as target entity in the relationship attribute [field owner].

Is there a workaround?

like image 244
Rakim Avatar asked May 02 '16 20:05

Rakim


People also ask

What does @entity mean in JPA?

Entities in JPA are nothing but POJOs representing data that can be persisted to the database. An entity represents a table stored in a database. Every instance of an entity represents a row in the table.

What is the @entity annotation?

Each entity must have at least two annotations defined: @Entity and @Id . The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping.

What are the JPA entity properties?

Entity PropertiesPersistability - An object is called persistent if it is stored in the database and can be accessed anytime. Persistent Identity - In Java, each entity is unique and represents as an object identity.

Why do we use @entity annotation?

The JPA specification requires the @Entity annotation. It identifies a class as an entity class. You can use the name attribute of the @Entity annotation to define the name of the entity. It has to be unique for the persistence unit, and you use it to reference the entity in your JPQL queries.


2 Answers

@MappedSupperclass is different than the @Inheritance annotation.

@MappedSuperclass tells the JPA provider to include the base class persistent properties as if they were declared by the child class extending the superclass annotated with @MappedSuperclass.

However, the inheritance is only visible in the OOP world, since, from a database perspective, there's no indication of the base class. Only the child class entity will have an associated mapped table.

The @Inheritance annotation is meant to materialize the OOP inheritance model in the database table structure. More, you can query a base class annotated with @Inheritance but you can't do that for a base class annotated with @MappedSuperclass.

like image 110
Vlad Mihalcea Avatar answered Nov 15 '22 21:11

Vlad Mihalcea


I can suggest two solutions:

Change Inheritance

The exception you get clearly describes your problem: User is not an entity. Any class declared as superclass with the interface @MappedSuperclass cannot be an entity (in standard JPA - depends on your JPA-provider)... let me point you to an answer I just gave to quite a similar problem

--> Superclass-Types

So defining your superclass as an abstract entity will give you the desired behaviour, you described.

Extra:

If you choose your inheritance mapping strategy as @Inheritance(strategy = InheritanceType.SINGLE_TABLE) you don't even need multiple database-tables. Here is a good example: JPA Single-Table Inheritance

Change Model (suggested)

Don't split your user entity in several entities just by their roles. Make an Enum with all your desired roles and add it as a field to your User-entity. This is widely more common, unless u need your admin, guests etc to be an own object...

like image 27
XiCoN JFS Avatar answered Nov 15 '22 20:11

XiCoN JFS