【问题标题】:Composite Key with subclass in hibernate休眠中具有子类的复合键
【发布时间】:2014-07-22 09:34:45
【问题描述】:

我有一个班级系统

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

由类扩展

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

需要使用systemId和place来制作复合键 我怎么能这样做..如果我在两个类中都有@Id,它会抛出异常

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

我该如何解决这个问题?

表格:

System{
systemid PK
systemName
}

PhysicalSystem
{
systemId PK
locationId PK
}

【问题讨论】:

  • 您能发布您所涉及的表格吗?
  • 用表格编辑问题

标签: java hibernate hibernate-mapping hibernate-annotations


【解决方案1】:

就您而言,也许最好的解决方案是 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;
}

【讨论】:

  • 但是我需要在 PhysicalSystem 中扩展 System,因为 System 是抽象类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多