-->
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.  [ 34 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Deleting one-to-many
PostPosted: Mon Oct 27, 2003 9:09 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
I posted an example previously:
http://forum.hibernate.org/viewtopic.php?p=2175912#2175912
where I have an one-to-many relation.

When I delete the parent class hibernate doesn't delete the child ... I tried to change the cascade atribute in the parent set and hibernate stills deleting only the parent class ...

What's wrong with my mapping?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 10:40 am 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
I looked at the mapping for Query & QueryParameter. Is the table name

query_parameter or nom_query_parameter?

We use cascade delete all the time without problems so something is going on with your mapping files or code.

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 11:58 am 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
The table name is nom_query_parameter ... but it's not the problem ...
My code and mapping works fine for adding / deleting parameters individually and also updating the query .. but when I try to delete the query I got an constraint violation because hibernate just send

"delete from nom_query where id = ? "

when it would to delete first the parameters ...

"delete from nom_query_parameter where query_id = ?"
"delete from nom_query where id = ? "

Any idea?

Thanks, Neimar

Code:

Query 1 ------------- 0...* QueryParameter

<class name="Query"  table="nom_query">
.....
   <set name="parameters" table="nom_query_parameter" inverse="true" lazy="true"
         order-by="param_id asc" cascade="delete">
      <key>
            <column name="query_id"/>
      </key>
      <one-to-many class="com.estobel.model.QueryParameter"/>
   </set>
</class>

<class name="QueryParameter" table="nom_query_parameter">
   
        <composite-id>
      <key-property name="id" column="param_id" type="int"/>
         <key-many-to-one name="query" class="Query" column="query_id"/>
   </composite-id>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 12:46 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 7:53 pm
Posts: 66
Location: Lakeland, Florida USA
No, I don't see any problems but we are not using the key-many-to-one. Our children all have synthetic keys. I would set log4j.logger.net.sf.hibernate to debug and see what hibernate is doing. e.g. is it cascading to the collection. Sorry,

Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 1:24 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
Strange ... hibernate just send an delete to the parent ... an I get an constraint violation ...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 1:35 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Try something like that
Code:
<column nut-null="true"...

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 1:52 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
This way ?

Code:
<class name="Query"  table="nom_query">
.....
   <set name="parameters" table="nom_query_parameter" inverse="true" lazy="true"
         order-by="param_id asc" cascade="delete">
      <key>
            <column name="query_id" [b]not-null="true"[/b]/>
      </key>
      <one-to-many class="com.estobel.model.QueryParameter"/>
   </set>
</class>


Doesn't work !![/b]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 6:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
An inverse="true" collection does not affect schema generation.

Use not-null="true" at the other end of the bidi association.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 6:59 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
In the other end of assoc I have a composite-id with key-many-to-one ... I can't use not-null ...

Code:

<class name="com.estobel.model.Query" table="nom_query">
   <id name="id" type="int" column="id">
           <generator class="native">
      <param name="sequence">nom_query_id_seq</param>
   </generator>

   </id>

   <set name="parameters" table="nom_query_parameter" inverse="true" lazy="true"
      order-by="param_id asc" cascade="delete">
      <key>
         <column name="query_id" not-null="true"/>
      </key>
      <one-to-many class="com.estobel.model.QueryParameter"/>
   </set>
</class>

<class name="com.estobel.model.QueryParameter" table="nom_query_parameter">
   <composite-id>
      <key-property name="id" column="param_id" type="int"/>
      <key-many-to-one name="query" class="com.estobel.model.Query" column="query_id"/>
   </composite-id>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 7:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Sorry, then wtf is your actual problem?? Its incredibly unclear.

Of course a <key-many-to-one> is always not-null, so there is no attribute.

To delete the Query and all its QueryParameters, simply delete the Query instance. Cascade will take care of the rest. There will be no constraint violations unless you are doing something else wierd that you are not telling us about.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 8:00 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
Here's the code I'm using:

[code]
HibernateQueryDAO dao = new HibernateQueryDAO();

Query q = dao.selectFull(1);

//Inserting parameter .. works fine
QueryParameter qp = new QueryParameter();
qp.setQuery(q);
dao.insertParameter(qp);

//Select the query ... works fine .. par


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 8:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
There are enough bizarre things in this small snippet of code that I don't know where to start.


Code:
QueryParameter qp = new QueryParameter();
qp.setQuery(q);


where do you call q.getParameters().add(qp) ?


Code:
session.flush();
tx.commit();


Why do you flush() before commit() ?

Code:
public Query selectFull(int id) throws HibernateException


Where is the transaction management?



[code]//Select the query ... works fine .. par


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 8:51 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
Code:
QueryParameter qp = new QueryParameter();
qp.setQuery(q);

gavin wrote:
where do you call q.getParameters().add(qp) ?

I got the same result using:

q.getParameters().add(qp) ;
q.setQuery(q);
session.update(q); //I dont use this because hibernate executes an insert (for parameter) and after an update (for query)

or

qp.setQuery(q);
session.save(qp); //Here hibernate just insert the parameter.

Code:
session.flush();
tx.commit();

gavin wrote:
Why do you flush() before commit() ?

Sorry, my mistake ... but in this case doesn't make diference.

Code:
public Query selectFull(int id) throws HibernateException

gavin wrote:
Where is the transaction management?

I never use transactions on select ... I should?

[code]//Select the query ... works fine .. par


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 9:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I never use transactions on select ... I should?


Where on earth does everyone get the idea that readonly operations do not require transactions???? I would like to know where this idea originated from! I have never seen ANY book that advocates this. Where did this idea start?

Does the Query class implement equals()/hashCode() correctly?

Why not turn on Hibernate's log?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 9:09 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I mean does the QueryParameter class implement equals()/hashCode() correctly, which requires, by extension, that Query also does, since there is a <key-many-to-one>.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 34 posts ]  Go to page 1, 2, 3  Next

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.