Hibernate:通过更改复合 ID 主键的一部分来插入新行?
我试图将新行插入数据库,但未能让 Hibernate 识别出必须插入我的对象。我有一个 Sponsor 表、一个 Course 表和一个 SponsorCourse 表,它是两者之间的交集。 SponsorCourse 的主键是 SponsorCoursePK 类的复合 ID,它包含两个整数:赞助商的主键和课程的主键。 SponsorCoursePK 实现 equals() &哈希码()。
我的目标是 Course 表的深层副本,包括其所有 SponsorCourse 行。
- 我首先向 Hibernate 询问相关赞助课程的列表 与现有的课程行。
- 接下来,我创建一个新的 Course 对象并将其提交到数据库。 (为了 遗留原因,Course 对象不由 Hibernate 管理)。
- 接下来,我迭代 Hibernate 为我提供的旧课程的 SponsorCourses 列表,并将 SponsorCourse 主键中保存课程 ID 的部分从旧课程 ID 更改为新创建课程的 ID。
- 接下来,我调用 saveOrUpdate 来尝试让 Hibernate 创建新行。
当我查看 Hibernate 发送的 SQL 时,不存在“插入”语句:Hibernate 无法识别我的新对象是“脏”的并且需要插入,即使我已经更改了主键的一部分。如果我更改 SponsorCourse 的任何其他值,Hibernate 会将对象识别为脏对象并插入新行。
我的替代方法是创建全新的 SponsorCourse 对象并保留它们。我很困惑,因为对于主键不是复合主键的其他子表,它似乎可以获取一行,更改主键,然后允许 Hibernate 使用新的脏对象插入新行主键。
Hibernate 文档,特别是第 8.4 章(复合 id)和Hibernate 3.0 参考文献的第 10 章(讨论对象的修改)不足以帮助我理解。
我是否认为我可以更改 Hibernate 获取的对象的主键并认为它会插入新行,从而出现错误?在这种情况下我应该总是创建自己的新对象吗?或者,这是尝试通过更改作为复合 ID 的主键部分来强制插入的独特问题吗?
谢谢。显然,我是这个框架的新手。
I am trying to insert new rows into the database but failing to get Hibernate to recognize that my objects must be inserted. I have a Sponsor table, a Course table, and a SponsorCourse table which is an intersection between the two. The primary key of a SponsorCourse is a composite-id of class SponsorCoursePK which holds two integers: the primary key of a Sponsor and the primary key of a Course. SponsorCoursePK implements equals() & hashcode().
My goal is a deep copy of the Course table, including all of its SponsorCourse rows.
- I first ask Hibernate for a list of the SponsorCourses associated
with the existing Course row. - Next I make a new Course object and commit that to the database. (For
legacy reasons, Course objects are not managed by Hibernate). - Next I iterate over the list of SponsorCourses which Hibernate gave me for the old course, and change the portion of the SponsorCourse's primary key which holds the courseid from the old courseid to the id of the newly created course.
- Next I call saveOrUpdate to attempt to get Hibernate to create the new rows.
When I look at the SQL Hibernate sends, no 'insert' statements exist: Hibernate fails to recognize that my new objects are 'dirty' and need to be inserted, even though I have changed a portion of the primary key. If I change any other value of the SponsorCourse, Hibernate will recognize the object as dirty and insert a new row.
My alternate approach is to create entirely new SponsorCourse objects and persist those. I am confused because for other child tables where the primary key is not a composite primary key, it appears to work to fetch a row, change the primary key, and then allow Hibernate to insert a new row based on the dirty object with a new primary key.
The Hibernate docs, in particular Chapter 8.4 (composite id's) & Chapter 10 (discussing modification of objects) of the Hibernate 3.0 reference have not been enough to help me understand.
Am I expecting something wrong by thinking I can change the primary key of an object fetched by Hibernate and thinking it will insert a new row? Should I always create my own, new, objects in that type of situation? Or, is it a problem unique with attempting to force an insert by changing part of a primary key which is a composite-id?
Thank you. Probably obviously, I am new to the framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试以下操作:
http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/Session.html
祝你好运
I would try the following:
http://docs.jboss.org/hibernate/core/3.5/api/org/hibernate/Session.html
Good luck