I want to develop a CMS using Java, Spring Data/ MVC/ DI , Hibernate defining REST-like API.
I have the following model entities:
Article
s Section
s 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.
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. Thus I have endpoints like:
/articles
- creates an article, no sections /articles/{article_id}
- updates basic article properties, does not affect sections /articles/{article_id}/sections
- creates a section in the article /articles/{article_id}/sections/{section_id}
- removes the section from the article /articles/{article_id}/sections/{section_id}
- updates basic section properties, does not affect owning article properties, nor aggregated sections and items 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);
Link : From Hibernate docs
Examples from edit functionality of our 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.