简体   繁体   中英

How to inherit @Id from mapped superclass?

I want to create a primary composite key and use an @Id field from a parent class. But it does not work. Why?

@MappedSuperclass
static abstract class SuperEntity {
    @Id
    private Long id;
}

@Entity
@IdClass(SuperPK.class)
public static class ChildEntity extends SuperEntity {
    @Id
    private String lang;
}


public class SuperPK {
    public SuperPK(Long id, String lang) {
        //...
    }
}

Result: Property of @IdClass not found in entity ChildEntity: id

I found an open issue regarding this bug.

One of the comments states to override the getters for the ID properties as a workaround.

@Entity
@IdClass(SuperPK.class)
public static class ChildEntity extends SuperEntity {
    @Id
    private String lang;

    @Override @Id
    public Long getId() {
        return super.getId();
    }
}

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