2010-05-02 38 views
1

我有兩個類,ServiceType和ServiceRequest。每個ServiceRequest必須指定它是什麼類型的ServiceType。所有的ServiceType都是在數據庫中預定義的,並且ServiceRequest是由客戶端在運行時創建的。休眠映射到已存在的對象

這裏是我的.hbm文件:

<hibernate-mapping> 
    <class dynamic-insert="false" dynamic-update="false" mutable="true" name="xxx.model.entity.ServiceRequest" optimistic-lock="version" polymorphism="implicit" select-before-update="false"> 
    <id column="USER_ID" name="id"> 
     <generator class="native"/> 
    </id> 
    <property name="quantity"> 
     <column name="quantity" not-null="true"/> 
    </property> 
    <many-to-one cascade="all" class="xxx.model.entity.ServiceType" column="service_type" name="serviceType" not-null="false" unique="false"/> 
    </class> 
</hibernate-mapping> 

<hibernate-mapping> 
    <class dynamic-insert="false" dynamic-update="false" mutable="true" name="xxx.model.entity.ServiceType" optimistic-lock="version" polymorphism="implicit" select-before-update="false"> 
    <id column="USER_ID" name="id"> 
     <generator class="native"/> 
    </id> 
    <property name="description"> 
     <column name="description" not-null="false"/> 
    </property> 
    <property name="cost"> 
     <column name="cost" not-null="true"/> 
    </property> 
    <property name="enabled"> 
     <column name="enabled" not-null="true"/> 
    </property> 
    </class> 
</hibernate-mapping> 

當我跑,我得到

com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails 

我想我的問題是,當我創建新的ServiceRequest對象,ServiceType是它的一個屬性,因此當我將ServiceRequest保存到數據庫時,Hibernat e嘗試再次插入ServiceType對象,並發現它已經存在。如果是這種情況,我該如何讓Hibernate指向存在的ServiceType而不是試圖再次插入它?

代碼引發錯誤:

/* Get existing Service Type from database */ 
ServiceType st = DAOFactory.getDAOFactory().getServiceTypeDAO().getServiceType(newServiceTypeName); 

/* Set the ServiceType of the new Service Request*/ 
newServiceRequest.setServiceType(st); 

/* Error occurs inside this function which simply calls 
    session.beginTransaction(); 
    session.save(sr); 
    session.getTransaction().commit(); */ 
DAOFactory.getDAOFactory().getServiceRequestDAO().saveData(newServiceRequest); 
return "newRequestDone"; 
+0

您正在運行的導致該異常的代碼是什麼? – oedo 2010-05-02 23:46:51

+0

當我嘗試保存ServiceRequest時發生。它只是一個簡單的session.beginTransaction(); session.save(sr); session.getTransaction()。commit(); – 2010-05-02 23:52:01

回答

0

在缺乏導致異常,我會說你的問題很可能是你想爲每個ServiceRequest,而創建一個新的ServiceType代碼你需要做的是通過id檢索現有的ServiceType,而不是每次都添加一個新的。

如果這聽起來並不可行,顯示您的Java代碼,我會盡力,並進一步幫助:)


進一步的幫助:做你的DAO中使用同一個Hibernate Session?除非您使用merge而不是save,否則從一個會話加載並保存在另一個會話中的實體可能會導致問題。

+0

我更新我的問題以顯示代碼。正如你所看到的,我正在從數據庫請求ServiceType,但是再次保存它。 – 2010-05-03 00:00:36

+0

是的,我爲每個DAO使用同一個會話。 – 2010-05-03 00:10:36

+0

你可以嘗試merge()而不是save()呢?或者在交易中封裝整個事物?否則我就沒有想法了,對不起。 – oedo 2010-05-03 00:15:36

0

您的SQL異常似乎並不符合它試圖創建已存在的ServiceType的假設。您不希望在ServiceType表上看到唯一的約束違規嗎?首先我要檢查的是,數據庫中的FK目標是否匹配hibernate映射?當您的ServiceType的實體指向(BOB_DEV_SERVICE_TYPES)但在ServiceRequest上實施的實際外鍵達到(TINA_TEST_SERVICE_TYPES)時,您可以得到類似這樣的東西