I have 4 tables
Customer has one-to many relation with Orders
Orders has many-to-one relation with Customer
Orders has one-to-many relation with LineItem
LineItem has many-to-one relation with Orders
LineItem has one-to-many relation with ItemConf
ItemConf has many-to-one relation with LineItem
all above are bidirectional
table got generated and all foreignkey relations are eastablished correctly.
i am able to insert and select data successfully.
when i try to delete Customer i am getting foreignkey violation exception from ItemConf,
on looking into the Log i found that its trying to delete LineItem rows before deleting from ItemConf.
please give me a solution
Hibernate version:3
Mapping documents: Customer: -------------------------------------------------------------------------- <class name="Customer" table="customer"> <id name="customerId" type="string" column="customerId" /> <bag name="order" inverse="true" cascade="delete"> <key column="customerId"/> <one-to-many class="Order"/> </bag> </class> -------------------------------------------------------------------------------- Orders: <class name="Order" table="orders" > <id name="orderId" type="java.lang.Long" column="orderId" > <generator class="increment"/> </id> <many-to-one class="Customer" name="customer" column="customerid" insert="false" update="false" />
<bag name="items" cascade="all" inverse="true"> <key column="orderId"/> <one-to-many class="LineItem" /> </bag> </class> -------------------------------------------------------------------------------- LieItem:
<class name="LineItem" table="lineitem"> <composite-id> <key-property name="orderId" column="orderId" type="java.lang.Long"/> <key-property name="itemId" column="itemId" type="string" /> </composite-id>
<bag name="configurations" cascade="all" inverse="true"> <key> <column name="itemId"/> <column name="orderId"/> </key> <one-to-many class="ItemConfiguration"/> </bag> <many-to-one class="Order" column="orderId" name="Order" insert="false" update="false"/>
</class> --------------------------------------------------------------------------------- ItemConf:
<class name="ItemConfiguration" table="itemconfiguration"> <id name="Id" type="java.lang.Long" column="id"> <generator class="increment"/> </id>
<many-to-one class="LineItem" insert="false" update="false" name="lineItem" cascade="delete" > <column name="orderId"/> <column name="itemId"/> </many-to-one> </class> ------------------------------------------------------
Code between sessionFactory.openSession() and session.close(): Customer customer=(Customer)session.load(Customer.class,customerId);
session.delete(customer);
Full stack trace of any exception that occurs: 13:03:12,861 INFO [STDOUT] Hibernate: delete from lineitem where orderId=? and itemId=? 13:03:12,908 WARN [JDBCExceptionReporter] SQL Error: 1451, SQLState: 23000 13:03:12,908 ERROR [JDBCExceptionReporter] Cannot delete or update a parent row: a foreign key constraint fails (`30ss/itemconfiguration`, CONSTRAINT `FK5DF7D0C 3E3D9A5B1` FOREIGN KEY (`orderId`, `itemId`) REFERENCES `lineitem` (`orderId`, ` itemId`)) 13:03:12,908 ERROR [AbstractFlushingEventListener] Could not synchronize databas e state with session
Name and version of the database you are using: MySQl 5
The generated SQL (show_sql=true): 13:03:12,861 INFO [STDOUT] Hibernate: delete from lineitem where orderId=? and itemId=?
|