Java 如何使用注释在休眠中映射“地图”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2327971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you map a "Map" in hibernate using annotations?
提问by Omar Kooheji
Using annotations how do you map a field in an entity which is a "Map" (Hashtable) of String to a given object? The object is annotated and instances of it are already stored in the hibernate databse.
使用注释如何将实体中的字段映射到给定对象,该实体是字符串的“映射”(哈希表)?对象被注释并且它的实例已经存储在休眠数据库中。
I've found the syntax for definging a map with a simple key and value as such:
我找到了使用简单的键和值定义地图的语法,如下所示:
<class name="Foo" table="foo">
...
<map role="ages">
<key column="id"/>
<index column="name" type="string"/>
<element column="age" type="string"/>
</map>
</class>
And oddly with an entity as the key and a simple type as the value like so:
奇怪的是,以实体为键,以简单类型为值,如下所示:
<class name="Foo" table="foo">
...
<map role="ages">
<key column="id"/>
<index-many-to-many column="person_id"
class="Person"/>
<element column="age" type="string"/>
</map>
</class>
<class name="Person" table="person">
...
<property name="name" column="name"
type="string"/>
</class>
But I don't see how to do this for a simple key to element mapping, and I don't see how to do this using annotations.
但是我看不到如何为元素映射的简单键执行此操作,也看不到如何使用注释来执行此操作。
采纳答案by Pascal Thivent
You could simply use the JPA annotation@MapKey
(note that the JPA annotation is different from the Hibernate one, the Hibernate @MapKey
maps a database column holding the map key, while the JPA's annotation maps the property to be used as the map's key).
您可以简单地使用JPA 注释@MapKey
(请注意,JPA 注释与 Hibernate 注释不同,Hibernate@MapKey
映射一个包含映射键的数据库列,而 JPA 的注释映射要用作映射键的属性)。
@javax.persistence.OneToMany(cascade = CascadeType.ALL)
@javax.persistence.MapKey(name = "name")
private Map<String, Person> nameToPerson = new HashMap<String, Person>();
回答by Jeff Walker
You should probably use a UserType or UserCollectionType. Or, you can use a custom tupleizer.
您可能应该使用 UserType 或 UserCollectionType。或者,您可以使用自定义元组化器。
see hibernate core documentationfor the concepts and hibernate annotations documentationfor the equivalent annotation approach.
有关概念,请参阅hibernate 核心文档,有关等效注释方法,请参阅hibernate annotations 文档。
Let me know if that isn't what you are asking for.
如果这不是您要的,请告诉我。
回答by whiskeysierra
@CollectionOfElements(fetch = FetchType.LAZY)
@JoinTable(name = "JOINTABLE_NAME",
joinColumns = @JoinColumn(name = "id"))
@MapKey(columns = @Column(name = "name"))
@Column(name = "age")
private Map<String, String> ages = new HashMap<String, String>();
回答by Juan
I know this question is very old but maybe this could help someone.
我知道这个问题很老了,但也许这可以帮助某人。
Other posibility is something like that:
其他可能性是这样的:
@Entity
@Table(name = "PREFERENCE", uniqueConstraints = { @UniqueConstraint(columnNames = { "ID_DOMAIN", "ID_USER", "KEY" })})
public class Preferences {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
private Long id;
@Column(name = "ID_DOMAIN", unique = false, nullable = false")
private Long domainId;
@Column (name = "PREFERENCE_KEY")
@Enumerated(EnumType.STRING)
private PreferenceKey key;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_USER", referencedColumnName = "ID")
private User user;
}
and
@Entity
@Table(name = "USER", uniqueConstraints = { @UniqueConstraint(columnNames = { "ID_DOMAIN", "LOGIN" })})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
private Long id;
@Column(name = "ID_DOMAIN", unique = false, nullable = false")
private Long domainId;
// more fields
@ElementCollection(fetch = FetchType.LAZY)
@JoinColumns({@JoinColumn(name = "ID_USER", referencedColumnName = "ID"), @JoinColumn(name = "ID_DOMAIN", referencedColumnName = "ID_DOMAIN")})
@OneToMany(targetEntity = Preferences.class, fetch = FetchType.LAZY)
@MapKey(name = "key")
private Map<PreferenceKey, Preferences> preferencesMap;
}
That only produces two tables User and Preferences, note that PreferenceKey is unique for a User into a domain
只生成两个表 User 和 Preferences,注意 PreferenceKey 对于域中的 User 是唯一的