带有@IdClass的JPA子类,没有属性

时间:2013-11-05 07:36:51

标签: java hibernate jpa jboss7.x

我有一个没有@Id或@Column属性的子类Entity。

但是我的子类实体的@IdClass如下

@Entity
@Table(name = "Employee")
@IdClass(EmployeeEntityPK.class)
public class EmployeeEntity extends AbstractEntity {

    @Override
    public void setName(String name) {
            super.setName(name);
    }
    @Override
    public void setLocation(String location) {
        super.setLocation(location);
    }
    @Override
    public void setEmpId(Integer empId) {
        super.setEmpId(empId);
    }
}

当我尝试部署我的项目时。我从hibernate

获得异常
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547) [rt.jar:1.6.0_17]
at java.util.ArrayList.get(ArrayList.java:322) [rt.jar:1.6.0_17]
at org.hibernate.cfg.AnnotationBinder.getUniqueIdPropertyFromBaseClass(AnnotationBinder.java:2576)
at org.hibernate.cfg.AnnotationBinder.isIdClassPkOfTheAssociatedEntity(AnnotationBinder.java:925)
at org.hibernate.cfg.AnnotationBinder.mapAsIdClass(AnnotationBinder.java:824)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:671)

完整例外位于http://pastebin.com/SnhQ1ZVQ

Hibernate试图从我的实体中找到@Id类,而不是那里。

如何解决此问题。

我的超级课程如下

@MappedSuperclass
public class AbstractEntity implements Serializable {
    @Id
    @Column(name = "empId")
    private Integer empId;

    @Column(name = "Name")
    private String name;

    @Column(name = "LOCATION")
    private String location;
    public Integer getEmpId() {
            return empId;
    }
    //along with other getter setters
   }

1 个答案:

答案 0 :(得分:0)

如果我有一个包含多个列的主键,我会在我想要用作Pk的类上使用普通的@Id属性。 Id-Class使用@Embeddable ...

进行注释

示例:

实体:

@Entity
public class Foo extends AbstractEntity implements Serializable {
    private static final long serialVersionUID = 1;

    @EmbeddedId
    private FooPK id;

    //Getter, Setter...

}

EmbeddedId:

@Embeddable
public class FooPK implements Serializable {
    private static final long serialVersionUID = 1;

    private Integer firstId;
    private Integer SecondId;

    public FavoritenPK() {
    }

    // Setter, Getter...

}

编辑: 我在MappedSuperclass中遇到了@Id的麻烦。尽量不要将@ Id-Property放在Mapped-Superclass中!