简体   繁体   中英

Hibernate. Generic Composite PK issue

I have an issue with composite primary key in Hibernate.

Example:

I have a class that represents base primary key for all instances of this.

public abstract class PrimaryKey implements Serializable { /* ... */ }

It has nothing but implements java.io.Serializable interface and can be used in generics or another class's methods as parameter to narrow an accepted classes.

Another primary key class should inherit it and add your specific fields as keys. For example:

public class PassportPK extends PrimaryKey {

    private String number;
    private String series;

    public PassportPK() {}

    public PassportPK(String number, String series) {
        this.number = number;
        this.series = series;
    }

    // Getters/setters are below.
}

Then it's used in the appropriate entity like this:

@Entity
@Table(name = "T_PASSPORTS")
@IdClass(PassportPK.class)
public class Passport implements Serializable {

    @Id
    @Column(name = "F_NUMBER")
    private String number;
    @Id
    @Column(name = "F_SERIES")
    private String series;

    public Passport() {}

    // Getters/setters are below.
}

Everything works fine if I have a deal with entities like this.

But some entities in my project have a simple primary key like int, long, String, and etc.

In this case I'd like to have a generic primary key like this one:

public class SimplePK<T extends Serializable> extends PrimaryKey {

    /**
    * Represents a simple key field of entity (i.e. int, long, String, ...);
    */
    private T id;

    public SimplePK() {}

    public SimplePK(T id) {
        this.id = id;
    }

    public T getId() {
        return this.id;
    }

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

QUESTION: How can I resolve it in annotation mapping way?

ps When I'm trying to resolve it like in the previous example (via @IdClass(SimplePK.class) I catch a "org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [application-context.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Property com.testprj.entity.SimplePK.id has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type" exception.

pps I use Hibernate with Spring framework for wiring components.

I would be grateful for any help!

I think that you can't use a generic type inside the id Class. Used the @IdClass to indicate the Class for the Composite Primary Key, if you want to used only one attribute like primary key you have to use @Id over the declaration and remove @IdClass.

Example:

@Entity
@Table(name = "T_PASSPORTS")
public class Passport implements Serializable {

    @Id
    private String id; //Or int, long...

    public Passport() {}

    // Getters/setters are below.

}

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