---
title: "NonUniqueObjectException问题的解决"
author: "ak47947"
date: "2008-12-05T13:55:00Z"
tags: ["Hibernate"]
slug: "nonuniqueobjectexception"
draft: false
lastmod: "2008-12-05T13:55:00Z"
id: 441605
url: "https://ak47947.iteye.com/blog/441605"
---

在公司做模块的时候遇到了NonUniqueObjectException 错误，

1. adifferentobjectwiththesameidentifiervaluewasalreadyassociatedwiththesession:
2. \[com.......#1462109];
3. nestedexceptionisorg.hibernate.NonUniqueObjectException:
4. adifferentobjectwiththesameidentifiervaluewasalreadyassociatedwiththesession:
5. \[com. .....#1462109]

看错误的原因是因为针对一个对象加载了引用，由于有多个引用指向一个对象，在你去update的时候，出现了对象不同步问题，hibernate不知道要保存哪个对象，查了下原因，原来是我所做的hibernate映射在主表中对子表做映射的时候设置了lazy="false",这样主表在查取数据的时候就会相应的也把子表的数据取出来，然后在子表的编辑页面做update操作的时候就又加载了一个引用，现在去更新它的时候，hibernate就不知道更新哪个了。

解决办法，将lazy="true" 或者默认不设置即可。