简体   繁体   中英

JPA Entity issue with ID

I'm building one application using JPA, I want to have a parent entity called "BaseEntity" with the attribute ID and then the rest of the entities extending this entity and then having their own attributes. The field id in the parent class is protected. However when I start the server I'm getting the following error:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.fgonzalez.domainmodel.User

Of course if I place the id field in the class User, it is working fine but this is not what I want. The weird thing is if I use xml files for the hibernate mappings instead of JPA, it works fine, but not with JPA. Any idea where can be the problem? Attached the code:

Base Entity:

public class BaseEntity implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;


@Id
@GeneratedValue
@Column(name="id")
protected Long id;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}


}

And User entity:

@Entity
@Table(name="users")
public class User extends BaseEntity{


/**
 * 
 */
private static final long serialVersionUID = 1L;

/**
 * 
 */

@Column(name="EMAIL",nullable=false,length=50,insertable=true,updatable=true)
private String email;

@Column(name="PASSWORD",nullable=false,length=50,insertable=true,updatable=true)
private String password;



public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email=email;
}


public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

}

Thank you in advance!!

You can't do that this way: BaseEntity isn't an @Entity, so @Id shouldn't even be processed.

If Hibernate does process it while using xml, that's probably a non-portable specificity.

You could implement some entity hierarchy, but I wouldn't do it in this case. You can only extend once, and this doesn't look like a real hierarchy: only one root, shared by every class?

You can find more information on entity inheritance here: http://docs.oracle.com/javaee/6/tutorial/doc/bnbqn.html

You could use compositions instead of inheritance. In this case, just annotate your User class (which wouldn't be an @Entity) with @Embeddable, and have a field annotated with @EmbeddedId on the using class.

Still, I wouldn't do that: it seems more DRY, but it has no more benefits that replacing String everywhere with something else just to not repeat yourself (which you would then do anyway).

I would just have an @Id Long id; field in every entity, freeing them from hierarchy hell. It looks more boilerplate, but will be much easier in the long term, with no obvious disadvantage.

If you are going implement inheritance in JPA, you are not suppose to do like in java. JPA got its own implementation strategies. Have a look here and choose the one that best suits your need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM