Hello everyone!
I'm using Hibernate 3.0.4 with PostgreSQL 8.0.1 on Win32.
My problem is with many-to-one relation mapping.
There is such model:
Code:
(many) templates |---------------------------------| template_models(one)
and constraint in DDL (PostgreSQL):
Code:
CREATE TABLE TEMPLATES (
...
CONSTRAINT FK_TEMPLATE_REFERENCE_MODEL
FOREIGN KEY (TEMPLATE_MODELS_ID)
REFERENCES TEMPLATE_MODELS (TEMPLATE_MODELS_ID)
...
)
I have made such relation definition in TemplateBean's hibernate mapping file (removed not important parts):
Code:
<class name="TemplateBean"
table="templates"
lazy="false">
...
<many-to-one name="templateModelBean"
class="TemplateModelBean"
column="template_models_id"
cascade="save-update,delete-orphans"
/>
...
</class>
TemplateModelBean's mapping doesn't contain any reference mapping to TemplateBean.
This is the problem.... when I do delete like this:
Code:
1: TemplateBean templateBean=...;
2: transaction=hibernateSession.beginTransaction();
3: hibernateSession.delete(templateBean);
4: transaction.commit();
I always get this exception in line 4:
Code:
java.sql.SQLException: ERROR: update or delete on "template_models" violates foreign key constraint "fk_template_reference_model" on "templates"
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1471)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1256)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:334)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2451)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1722)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
Workaround is to remove constraint in DDL - but this is not the solution I want. Problem is that Hibernate removes object graph in wrong order.
Am I missing some hibernate-mapping-magic ?
How to guide Hibernate to delete TemplateBean before TemplateModelBean?
Does anyone had similar problem?