Hibernate 3.6.7:@ANY和Foreign-Key的问题

时间:2011-08-24 21:25:55

标签: hibernate relation any

我有实体“用户”和“客户”:

@Entity
@Table(name = "USR_USER")
public class User extends PersistentObject {

   [...]

   @Any(metaColumn = @Column(name = "USR_OWNERTYPE"))
   @AnyMetaDef(idType = "long", metaType = "string", metaValues = {
         @MetaValue(targetEntity = Customer.class, value = "CST"),
         @MetaValue(targetEntity = Client.class, value = "CLT") })
   @JoinColumn(name = "USR_OWNERID")
   private PersistentObject owner;
}

@Entity
@Table(name = "CST_CUSTOMER")
public class Customer extends PersistentObject {

   @Id
   @GeneratedValue
   @Column(name="CST_ID")
   private long id;

   [...]

   // @OneToMany(mappedBy = "owner")
   private transient Set<User> users;
}

运行它(使用spring),我得到以下异常:

Caused by: org.hibernate.MappingException: Foreign key (FK35B91BB65D577CEF:USR_USER [USR_OWNERTYPE,USR_OWNERID])) must have same number of columns as the referenced primary key (CST_CUSTOMER [CST_ID])

我不明白这一点,因为我将@AnyMetaDef中的“Customer”(在“User”中)映射到常量“CST”。

我做错了什么?

提前感谢您的帮助 托马斯

1 个答案:

答案 0 :(得分:1)

“无法为这种关联指定外键约束。” - http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-types-anymapping

编辑:你想要的@Any的另一端是@OneToAny,而且不存在。可能是您在不适当的地方使用@Any映射。再次from the docs:“这不是映射多态关联的常用方法,您应该仅在特殊情况下使用它。例如,对于审计日志,用户会话数据等。”

仔细查看inheritance mapping,看看其中一个是不是更合适的方式来映射您的关系。