简体   繁体   中英

Composite Key with subclass in hibernate

I have a Class system

@Entity
abstract class System{
@Id
int systemId;
//setter and getters..
}

and which is extended by class

@Entity
class PhysicalSystem extends System
{
@Id
int place;
//setter and getters..
}

in need to make the composite key by using the systemId and place how can i do this.. if i have @Id in both class its throws exception

Initial SessionFactory creation failed.java.lang.ClassCastException: org.hibernate.mapping.JoinedSubclass cannot be cast to org.hibernate.mapping.RootClass

How can i solve this?

Tables:

System{
systemid PK
systemName
}

PhysicalSystem
{
systemId PK
locationId PK
}

In your case, maybe the best solution is a OneToOne mapping:

@Entity
public class PhysicalSystem implements Serializable {

    @EmbeddedId
    private PhysicalSystemKey key;  

    @JoinColumns({JoinColumn(name = "key.systemId", referencedColumnName = "ctvId"})
    @OneToOne(mappedBy = "physicalSystem")
    private System system;

    // ...
}

@Embeddable
public class PhysicalSystemKey {

    private Long systemId;

    private Long locationId;

    // ...
}

@Entity
public class System implements Serializable {

    @Id
    private Long systemId;

    @OneToOne(mappedBy = "system")
    private PhysicalSystem physicalSystem;
}

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