简体   繁体   中英

JPA subclass with @IdClass and no attributes

I have a subclass Entity with no @Id or @Column attribute.

but my subclass entity has @IdClass as follows

@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);
    }
}

When I try to deploy my project. I am getting exception from 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)

complete exception is in http://pastebin.com/SnhQ1ZVQ

Hibernate is trying to find @Id class from my Entity which is not there.

How can I resolve this issue.

My super class is as follows

@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
   }

If I have a primary key with more than one column I use the normal @Id Property on the class I want to use as Pk. The Id-Class is annotated with @Embeddable...

example:

Entity:

@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...

}

EDIT: I had troubles having the @Id in MappedSuperclass. Try not to put the @Id-Property in Mapped-Superclass!

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