[英]Hibernate update non-reference fields of object
I want to develop a CMS using Java, Spring Data/ MVC/ DI , Hibernate defining REST-like API. 我想使用Java,Spring Data / MVC / DI和Hibernate定义类似REST的API来开发CMS。
I have the following model entities: 我有以下模型实体:
Article
s Article
Section
s Section
Item
Item
All these entities have properties of their own (eg name, type etc.), but as it is obvious they refer to their aggregated entities. 所有这些实体都具有自己的属性(例如名称,类型等),但是很明显,它们是指其聚合的实体。 I need to defined CRUD API methods for each such entity.
我需要为每个这样的实体定义CRUD API方法。
I decided to stray a bit from dogmatical REST and when I do modify I need to pass in only the entity-specific properties (like name, type etc.), but would not affect the aggregations. 我决定从教条化的REST中脱颖而出,当我进行修改时,我只需要传递特定于实体的属性(例如名称,类型等),但不会影响聚合。 Thus I have endpoints like:
因此,我有以下端点:
/articles
- creates an article, no sections /articles
-创建文章,无版块 /articles/{article_id}
- updates basic article properties, does not affect sections /articles/{article_id}
-更新基本文章属性,不影响版面 /articles/{article_id}/sections
- creates a section in the article /articles/{article_id}/sections
在文章中创建一个部分 /articles/{article_id}/sections/{section_id}
- removes the section from the article /articles/{article_id}/sections/{section_id}
-从文章中删除该部分 /articles/{article_id}/sections/{section_id}
- updates basic section properties, does not affect owning article properties, nor aggregated sections and items /articles/{article_id}/sections/{section_id}
-更新基本版块属性,不影响拥有文章的属性,也不影响汇总的版块和项目 So my question is: 所以我的问题是:
When I receive a modify request I get all basic properties of the element along with owning entity identifier. 收到修改请求后,我将获得元素的所有基本属性以及拥有的实体标识符。 How can I effectively combine those with the existing relations in the database, so that I keep all of them and modify the basic properties without the need of copying over all properties one by one.
如何有效地将它们与数据库中的现有关系结合起来,以便保留所有它们并修改基本属性,而无需一一复制所有属性。 Here is an example for the article-section relation.
这是文章-部分关系的示例。
public void modifySection(int articleId, int sectionId, Section section) {
assert(article.owns(sectionId));
Section dbSection = sectionDao.findOne(sectionId);
copyOverProperties(section, dbSection); // this is the thing I do not know how to do
sectionDao.save(dbSection);
}
You require hibernates session.merge(object_name); 您需要休眠session.merge(object_name);
Link : From Hibernate docs 链接: 来自Hibernate文档
Examples from edit functionality of our webapp : Webapp编辑功能的示例:
@Repository
public class GroupCanvasDAOImpl implements GroupCanvasDAO {
private final SessionFactory sessionFactory;
@Autowired
public GroupCanvasDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void editGroupCanvas(GroupCanvas groupCanvas) {
Session session = this.sessionFactory.getCurrentSession();
GroupCanvas groupCanvas1 = (GroupCanvas) session.get(GroupCanvas.class, groupCanvas.getMcanvasid());
// Below 2 steps are not necessary if object was retrieved from DB and //then persisted back-again. If it was newly created to replace an //old-one, then the below 2 lines are needed.
groupCanvas.setGroupAccount(groupCanvas1.getGroupAccount());
groupCanvas.setCanvasowner(groupCanvas1.getCanvasowner());
session.merge(groupCanvas);
session.flush();
}
}
}
If this is not what you are looking for, kindly let me know, I will delete my answer. 如果这不是您想要的,请告诉我,我将删除答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.