mapping multiple sets in one table in hibernate

只谈情不闲聊 提交于 2019-12-01 15:28:01

As Maurizio wrote, you need to use the where attribute to retrieve only the rows that belong to each collection.

To insert rows using the additional column, you need to add the following element inside the <set>:

<sql-insert>
  insert into user_logs(user_id, what, [when], type)
  values(?, ?, ?, 'access')
</sql-insert>

That essentially tells Hibernate to use that specific SQL instead of generating it. Note that I had to manually escape the when column.

Bonus: to add the extra column to the DB, use the following (after closing the <class>)

<database-object>
  <create>alter table user_logs add type char(6)</create>
  <drop></drop>
</database-object>

Interesting bit: I wrote and tested this using NHibernate, but it was ported directly, so it should work exactly the same.

Did you try to map set in this way:

<set name="accessLogs" table="user_logs" where="type='access'">    

why doesn't it work to just combine the lists into one list? Both of your lists are of the same class.

EDIT - If you want to preserve having 2 lists, then create a third list that is the combination, and only persist that. You will have to control access to accessors of the two lists, so you know when to update the third, but shouldn't be too hard.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!