简体   繁体   English

具有相同标识符值的另一个对象已与以下会话相关联:实体的XXX

[英]A different object with the same identifier value was already associated with the session: XXX, of entity

We have a ASP.NET MVC 3 C# project running NHibernate 3 and Castle.ActiveRecord for MySQL, and we trying to get "one session per request" to work with this tutorial . 我们有一个运行NHibernate 3和MySQL的Castle.ActiveRecord的ASP.NET MVC 3 C#项目,我们试图获得“每个请求一个会话”来使用本教程

And it seems to work for some stuff, but when we do SaveAndFlush() , the command gives us an error: 它似乎对某些东西SaveAndFlush() ,但是当我们执行SaveAndFlush() ,命令给我们一个错误:

A different object with the same identifier value 
was already associated with the session: 142

And if we try to do only Save() we got the same message so it has nothing to do with the Flush() function. 而且,如果我们尝试仅执行Save() ,则会得到相同的消息,因此与Flush()函数无关。

I have find some result when I search but nothing I can use to get it to work. 我在搜索时找到了一些结果,但是没有什么可以使它起作用。

Something I have not tested because I don't know how I do, is, 我没有测试过,因为我不知道该怎么做,

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")

Any ideas? 有任何想法吗?

Basically the problem might lie in that you are trying to Save() an object that already exists. 基本上,问题可能出在您试图Save()一个已经存在的对象。 If the object has an identifier already set ( object.Id = 142 ) then the object does not need to be saved. 如果对象具有已设置的标识符( object.Id = 142 ),则无需保存该对象。

If you make changes to it, and need to save those changes you need to use the Update() method (as mentioned by @OskarBerggren) or the SaveOrUpdate() which basically checks and decides whether to save the object or update it. 如果对其进行更改,并且需要保存这些更改,则需要使用Update()方法(如@OskarBerggren所述)或SaveOrUpdate() ,该方法基本上会检查并决定是否保存对象或更新对象。 The last one might work best in your case. 在您的情况下,最后一个可能效果最好。 Change your current, 改变你的电流,

session.Save(object);

to, 至,

session.SaveOrUpdate(object);

The Session object is from NHibernate's SessionFactory implementation. Session对象来自NHibernate的SessionFactory实现。 However, I see you are using ActiveRecordMediator , so you can use, 但是,我看到您正在使用ActiveRecordMediator ,因此您可以使用,

ActiveRecordMediator.GetSessionFactoryHolder().CreateSession()

to create a Session you can use to save your models. 创建Session您可以用来保存模型。

暂无
暂无

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

相关问题 javax.persistence.EntityExistsException:具有相同标识符值的另一个对象已与会话关联 - javax.persistence.EntityExistsException: a different object with the same identifier value was already associated with the session @MapsId和具有与会话关联的相同标识符值的两个对象 - @MapsId and two objects with the same identifier value associated with the session PHP Session值在同一代码块中不同 - PHP Session value different in the same block of code 附加类型为“”的实体失败,因为相同类型的另一个实体已经具有相同的主键值 - Attaching an entity of type “” failed because another entity of the same type already has the same primary key value Mysql 如何 select 只有同一表中的行和相同的标识符但不同的值 - Mysql how to select only row in same table and same identifier but different value 我想防止删除与其他实体相关联的实体 - I would want to prevent deletion of an entity if its associated with a different entity 实体副本已分配给不同的实体(Spring/Hibernate) - Entity copy was already assigned to a different entity (Spring/Hibernate) <a>单击时</a>如何将与<a>链接</a>关联的值设置<a>为会话变量</a> - How to set the value associated with an <a> link as a session variable, when clicked 具有相同会话数据的不同页面上的不同行 - Different row on different page with same session data 在为GenerationType.IDENTITY提供标识符值作为生成标识符的策略时,传递给分离的实体 - Detached entity passed to persist when providing identifier value for GenerationType.IDENTITY as strategy to generate the identifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM