2010-05-02 36 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)时,您可以得到类似这样的东西