-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: hb 3.0.4: mant-to-one relation violates db constraint
PostPosted: Thu Aug 18, 2005 7:30 pm 
Newbie

Joined: Thu Aug 18, 2005 6:59 pm
Posts: 2
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?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 18, 2005 7:42 pm 
Beginner
Beginner

Joined: Thu Feb 17, 2005 9:20 pm
Posts: 36
Location: Vancouver, WA
Remove delete-orphans cascade option or use bidirectional mapping with inverse="true" set. It is not clear what is life cycle of those object and relationships between them to tell more how to better implement that.

_________________
Vasyl Zhabko


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 4:53 am 
Newbie

Joined: Thu Aug 18, 2005 6:59 pm
Posts: 2
Basilio wrote:
Remove delete-orphans cascade option

This is unacceptable. Orphaned entities have to be removed from database.

Basilio wrote:
or use bidirectional mapping with inverse="true" set.

I have made such investigation. Nothing changed.

Basilio wrote:
It is not clear what is life cycle of those object and relationships between them to tell more how to better implement that.

TemplateModelBean is created with first TemplateBean refering to it (db constraint is guarantee).
TemplateModelBean may be reffered with many TemplateBean.
TemplateModelBean may exist as long as any TemplateBean refers to it.
When no TemplateBean refers TemplateModelBean then TemplateModelBean have to be removed (This is why cascade="..., delete-orphan" is used.).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.