mysql注解忽略实体类映射,映射一个java.util.Map<实体,实体GT;在使用Hibernate JPA注解...

I seem to be having an issue with mapping a collection in hibernate where the key and the resource are both entities to be mapped by hibernate.

Whereas from the documentation (my main resource for this issue has been this, but I of course welcome any others: http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/collections.html) it would appear that this should be a relatively simple task, I just can't seem to get the key of the map to persist.

My code as it stands looks like this:

Parent class:

@Entity

public class Parent {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private int id;

@MapKey

@MapKeyClass(Key.class)

@ManyToMany(cascade = CascadeType.ALL)

private Map map;

Key class:

@Table(name="ParentKey")

@Entity

public class Key implements Comparable {

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private int id;

@Column(name = "hierarchyKey")

private int key;

The resource class is a relatively simple POJO and persists fine, so I would assume that the issue is not in that class.

I did come across the annotation @MapKeyManyToMany in some documentation on the hibernate forums, but I believe this has since been deprecated.

The schema of the database is unimportant at this stage, the main concern is simply making sure everything persists to the database. We're using MySQL for the underlying database.

As I'm sure you can probably guess, any search involving the words map, hibernate, entity and key return a large amount of questions not related to this issue, though I'm sure this must be a situation that occurs quite often in terms of mapping situations.

I don't often submit questions, but I am an active reader, so please let me know if any more info is required.

解决方案

This works as specified. Operations are not cascaded to the key of the map. They are cascaded only to the value. In documentation this is told with following words:

When the target collection is a java.util.Map, the cascade element

applies to the map value.

Key must be persisted separately.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值