简体   繁体   English

休眠中带有子类的复合键

[英]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 需要通过使用systemId来制作复合键并放置该怎么办..如果我在两个类中都具有@Id,则抛出异常

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: 在您的情况下,最好的解决方案是OneToOne映射:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM