【发布时间】:2015-11-25 06:09:46
【问题描述】:
考虑以下两个表:
| User | UserAttribute |
|---------- |-------------------|
| userId(PK)| attributeId(PK) |
| firstName | userId |
| lastName | name |
| other | locale |
| active | value |
在原始 hibernate-3.2.2 中,一对多双向关系正常工作:
@Entity
@Table(name = "User")
public class UserHbm {
@Id
@GeneratedValue(generator = "id-generator")
@Column(name = "userId")
private long id;
@Column
private String firstName;
@Column
private String lastName;
@Column
private String other;
@Column
private boolean active = true;
@OneToMany (mappedBy = "user", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
@MapKey(columns = { @Column(name = "name"), @Column(name = "locale") })
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Map<AttributeKeyHbm, UserAttributeHbm> attributes = null;
//other methods, getter & setting, etc...
}
@Entity
@Table(name = "UserAttribute")
public class UserAttributeHbm {
@Id
@GeneratedValue(generator = "id-generator")
@Column(name="attributeId")
private long id;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@JoinColumn(name="userId")
private UserHbm user;
@Column
private String name;
@Column
private Locale locale = Locale.ENGLISH;
@Column
private String value;
// other methods...
}
public class AttributeKeyHbm implements UserType {
protected static int[] SQL_TYPES = { Types.VARCHAR, Types.VARCHAR };
private String name;
private Locale locale;
public AttributeKeyHbm(String name, Locale locale) {
this.name = name;
this.locale = locale;
}
//other override methods, assemble, deepCopy & nullSafeGet, etc...
}
使hibernate 从 3.2.2 迁移到 4.3.11 的困难在于将用户类型 AttributeKeyHbm 作为 attributes 中 UserHbm 中的键
AttributeKeyHbm 是 Hibernate 的装扮 UserType,包含分别来自 UserAttributeHbm、name 和 local 的两列。
由于hibernate注解@MapKey是deprecated,我尝试一一使用下面的注解,替换原来的@MapKey:
@MapKeyType(value=@Type(type="com.xxx.xxx.AttributeKeyHbm"))
@MapKeyJoinColumns(value={ @MapKeyJoinColumn(name = "name"), @MapKeyJoinColumn(name = "locale")})
@MapKeyJoinColumn(name = "AttributeKeyHbm")
但这一切都以这个例外结束:
org.hibernate.MappingException: collection index mapping has wrong number of columns: com.xxx.xxx.UserHbm.attributes type: com.xxx.xxx.AttributeKeyHbm
所以我的问题是:
- 如何在
UserHbm和hibernate-4.3.11中实现相同的功能,因为不能放弃AttributeKeyHbm,因为它已经被其他API 大量使用。 - 由于
AttributeKeyHbm有两列,实现接口UserType而不是CompositeUserType是否正确或足够
【问题讨论】:
-
为什么不将
AttributeKeyHbm标记为@Embeddable?在这种情况下,您可以将@Column添加到类的属性中。 -
@TobiasLiefke 你能详细说明一下吗?
-
什么不清楚?你读过Hibernate documentation of Maps吗?
标签: java hibernate hibernate-mapping