简体   繁体   English

休眠注释Java映射

[英]Hibernate Annotations Java Map

I need some help with hibernate annotations. 我需要休眠注释的帮助。

I have the following 2 entities: 我有以下2个实体:

public class Custom {
 private Map<KeyObject, ValueObject> properties;

 @oneToMany(mappedBy = "customId", cascade = CascadeType.All)
 @MapKey(name = "keyObject")
 public Map<KeyObject, ValueObject> getProperties();

.....
}

public class ValueObject {
 private KeyObject keyObject;
 private Long customId;
 private String value;

...getters and setters
}

I have a simple dao class to save, update, select, and delete records. 我有一个简单的dao类,用于保存,更新,选择和删除记录。

If I remove a record from the map in the Custom object and then call customDao.save(custom) my changes are never persisted to the database. 如果我从Custom对象的地图中删除一条记录,然后调用customDao.save(custom),我的更改将永远不会持久保存到数据库中。 It is not throwing any errors either. 它也不会引发任何错误。

I saw in the hibernate examples I can define the customId as a Custom object instead of a Long in the KeyValue table but I did not want to do this. 我在休眠示例中看到,我可以将customId定义为Custom对象,而不是KeyValue表中的Long,但是我不想这样做。

Am I setting up my entity's incorrectly? 我的实体设置不正确吗?

When an association has the mappedBy attribute, it means: I'm not the owner of this association. 当关联具有mappedBy属性时,表示:我不是该关联的所有者。 Any change done to this side of the association won't matter for Hibernate. 对于关联的这一方面所做的任何更改对于Hibernate都无关紧要。 What will matter is the other side of the association. 重要的是协会的另一面。

I'm surprised this even works, because I would have expected Hibernate to require a ManyToOne on the other side, of type Custom . 我什至惊讶这甚至行得通,因为我希望Hibernate在另一端需要Custom类型的ManyToOne。 But what's sure is that if you don't set the custom/customId field to null in KeyValue/ValueObject, Hibernate won't set this column to null. 但是可以确定的是,如果您没有在KeyValue / ValueObject中将custom / customId字段设置为null,则Hibernate不会将此列设置为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM