[英]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.