简体   繁体   English

ID的JPA实体问题

[英]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. 我正在使用JPA构建一个应用程序,我想要一个具有属性ID的父实体“ BaseEntity”,然后其余实体扩展该实体,然后拥有自己的属性。 The field id in the parent class is protected. 父类中的字段ID受保护。 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 原因:org.hibernate.AnnotationException:未为实体指定标识符: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. 当然,如果我将id字段放在类User中,则可以正常工作,但这不是我想要的。 The weird thing is if I use xml files for the hibernate mappings instead of JPA, it works fine, but not with JPA. 奇怪的是,如果我将xml文件用于休眠映射而不是JPA,则可以正常工作,但不适用于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. 您不能以这种方式这样做:BaseEntity不是@Entity,因此甚至不应处理@Id。

If Hibernate does process it while using xml, that's probably a non-portable specificity. 如果Hibernate确实在使用xml时进行了处理,那可能是不可移植的特性。

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 您可以在此处找到有关实体继承的更多信息: 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. 在这种情况下,只需使用@Embeddable注释User类(不是@Entity),并在using类上使用@EmbeddedId注释字段。

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). 仍然,我不会这样做:看起来更干了,但是它没有更多的好处,那就是用别的东西替换String到处都只是为了不重复自己(无论如何你都会这样做)。

I would just have an @Id Long id; 我只有一个@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中实现继承,则不要像在Java中那样做。 JPA got its own implementation strategies. JPA有自己的实施策略。 Have a look here and choose the one that best suits your need 在这里看看然后选择最适合您的需求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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