简体   繁体   English

如何在休眠状态下不向表1和表2添加数据而向表3创建数据和向表3添加数据

[英]How to create and add data to Table 3 without adding data to Table 1 and Table 2 in hibernate

I am new to Hibernate and not able to find out a way of the situation that I have. 我是Hibernate的新手,无法找到解决这种情况的方法。 It is kind of Many to Many relationship but the only difference is that I want the data to be only inserted in table 3 and not in table 1 & table 2. 这是一种多对多关系,但是唯一的区别是我希望将数据仅插入表3中,而不插入表1和表2中。

Table 1 and Table 2 already has all the data present. 表1和表2已经存在所有数据。

Current Requirement: 当前要求:

Table 1 has user id, first name, last name (Already Exists) Table 2 has document id, document name, document content (Already Exists) Table 3 should have user id and document id Both Table 1 & Table 2 already has all the data prepopulated. 表1具有用户ID,名字,姓氏(已存在)表2具有文档ID,文档名称,文档内容(已存在)表3应该具有用户ID和文档ID表1和表2都已经具有所有数据预填充。 Now if a user views a document, I want to make an entry in the third table without persisting or updating in Table 1 & Table 2. We want to maintain data on the documents which are viewed by a user so that even when the user logged off and login back we can show him only the docs which are unread. 现在,如果用户查看文档,我想在第三个表中输入一个条目,而又不保留或更新表1和表2中的内容。我们希望维护用户查看的文档上的数据,以便即使用户登录时也是如此。关闭并重新登录,我们可以只向他显示未读的文档。 What is the best use of Hibernate in this situation? 在这种情况下,Hibernate的最佳用法是什么?

You don't need to update the people nor the document table. 您不需要更新人员或文档表。 Hibernate is able to add entries to your third table (the join table) if you configure everything right. 如果您配置正确,Hibernate可以将条目添加到第三个表(联接表)中。 You just annotate a collection with @ManyToMany and make sure you are working with entities from the DB instead of creating new ones: 您只需使用@ManyToMany注释集合,并确保您使用的是数据库中的实体,而不是创建新的实体:

  • load person from DB 从数据库加载人
  • load document from DB 从数据库加载文档
  • put document into the list of the person 将文件放入人员名单
  • save person entity 保存人实体

See here: http://www.mkyong.com/hibernate/hibernate-many-to-many-relationship-example-annotation/ for a good walkthrough 参见此处: http ://www.mkyong.com/hibernate/hibernate-many-to-many-relationship-example-annotation/

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

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