2009-08-07 41 views
0

這裏我再次在我自學的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所以我做錯了什麼? 感謝閱讀。我不'真正理解這裏的問題。

+0

你想保存具有已被其他實體使用的ID的實體? – 2009-08-07 19:20:20

回答

0

解決它。我有一個管理器(它創建的POJO和DAO),其注入新的ID對每個新創建的POJO.and測試過程中我將ID添加manually.thanks對您有所幫助

0

聽起來就像你試圖用一個已經存在的ID保存一個實體。你意識到,當你使用聯合子類方法時,你可能共享相同ID空間的所有子類,是嗎?

購買方式,你是否有理由手動分配ID或將生成ID的幫助?

+0

是的,它確實做到了。它是我要做的一項要求。你讓我發現把相同的ID賦予不同的pojo對象很重要。現在我的測試單獨運行良好。但是當構建它失敗時findbyexample ResponseDAOImpl的方法(就像我說的,當你單獨運行ResponseDAOImplTest類時運行良好)。原因是:org.hibernate.PropertyAccessException:在調用model.ResponseImpl.messageId的setter時發生IllegalArgumentException。我必須承認我有點失落 – 2009-08-12 10:37:14

+0

這實際上意味着你的setter方法拋出了IllegalArgumentException。你應該能夠看到爲什麼在你自己的代碼。 – Henning 2009-08-19 19:21:42