這裏我再次在我自學的hibernate和個人實驗項目中獲得更多的理解。 這裏是我的環境的描述: 我有一個超級模型實體,所有我的模型繼承from.it只有id屬性 我使用在hibernate網站上找到genericDAO模式。 現在我的問題是,我使用列表而不是設置爲我的一對多映射(這裏沒有必要避免重複),當我在列表索引或列表索引屬性中引用超級實體ID時,我有這個錯誤: org.hibernate.NonUniqueObjectException:用同樣的標識值不同的對象已經與會話 這裏相關的是超級實體接口hibernate集合映射與列表和超級實體類
public interface Model extends Serializable {
public Long getId();
public void setId(Long id);
}
//here is its implementation.they are not in the same physical file
public abstract class ModelImpl implements Model {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
這裏是它的映射文件
<class abstract="true" name="ModelImpl">
<!--i removed doctype and stuffs-->
<id name="id">
<generator class="assigned"/>
</id>
</class>
這裏是POJO接口上,這是我的父母
public interface Message extends Model {
Date getDateSent();
String getGlobalStatus();
} // 其實現是在這裏,但在不同的物理文件 公共類MessageImpl擴展ModelImpl實現信息{ 私人字符串globalStatus; private List response = new ArrayList(); 私人日期dateSent; // setter和getter .... }
它的映射文件是像這樣:
<union-subclass extends="ModelImpl" name="MessageImpl">
<property name="globalStatus"/>
<property name="dateSent" type="timestamp"/>
</SET> - >
我評論了這個集合,因爲它給了一個鑄造錯誤,這讓我意識到我的電子郵件RROR 所以這是父Response類是在這裏:
public interface Response extends Model {
String getGatewayMessageId();
Message getMessageId();
}
//its implementation but in different physical file
public class ResponseImpl extends ModelImpl implements Response{
private static final long serialVersionUID = 1L;
private Message messageId;
private String gatewayMessageId;
//setters and getters..
}
讓我的測試過程中基本上是that.now當我試圖挽救其孩子的消息,它拋出這樣的:
組織.hibernate.NonUniqueObjectException:具有相同標識符值的不同對象已與會話相關聯
我可以執行所有persitent方法,例外情況是我剛纔提到的方法。 我在這裏有點失落因爲他們都是繼承模型ID所以我做錯了什麼? 感謝閱讀。我不'真正理解這裏的問題。
你想保存具有已被其他實體使用的ID的實體? – 2009-08-07 19:20:20