-->
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: updating instead of deleting
PostPosted: Fri Dec 02, 2005 6:49 am 
Newbie

Joined: Thu Dec 01, 2005 6:32 am
Posts: 5
Hy, guys! I got the following problem:when I try to do a delete on a table Telefones, which is linked with a endereco table using a one-to-many association, where the telefones table is the many side, and the endereco table is linked with a contact table in a one-to-one association , and instead of deleting the row on telefones, he try's to update the row making the foreign key column null!here's the mapping of the two tables and the code I tried to execute:


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="roseindia.tutorial.hibernate.Telefone" table="TELEFONES">
    
   <id name="id" type="long" column="ID" >
      <generator class="increment" />
   </id>
   
      <property name="telefone">
      <column name="TELEFONE"/>
     </property>

     <property name="contact">
      <column name="CONTACT"/>
     </property>

   <one-to-one name="Endereco" class="roseindia.tutorial.hibernate.Endereco" cascade="all" />


</class>

</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="roseindia.tutorial.hibernate.Endereco" table="ENDERECO">
    
   <id name="id" type="long" column="CONTACT" >
   <generator class="foreign" >
   <param name="property">Contact</param>
    </generator>
   </id>
   
      <property name="rua">
      <column name="RUA"/>
     </property>
     <property name="bairro">
      <column name="BAIRRO"/>
     </property>
     <property name="complemento">
      <column name="COMPLEMENTO"/>
     </property>
     <property name="numero">
      <column name="NUMERO"/>
     </property>

   <one-to-one name="Contact" class="roseindia.tutorial.hibernate.Contact" cascade="all" />

   <set name="telefones" cascade="all" >
   <key column="CONTACT" />
   <one-to-many class="roseindia.tutorial.hibernate.Telefone" />
   </set>   


</class>

</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="roseindia.tutorial.hibernate.Contact" table="CONTACT">
      <id name="id" type="long" column="ID" >
      <generator class="increment"/>
     </id>

     <property name="firstName">
       <column name="FIRSTNAME" />
     </property>
     <property name="lastName">
      <column name="LASTNAME"/>
     </property>
     <property name="email">
      <column name="EMAIL"/>
     </property>
   <one-to-one    name="Endereco"
      class="roseindia.tutorial.hibernate.Endereco"
      cascade="all" />
   </class>

</hibernate-mapping>


and here it is the code:

Code:
Contact contact3 = (Contact) session.get(Contact.class,new Long(1));
            Query query = session.createQuery("select tel from roseindia.tutorial.hibernate.Telefone as tel where tel.contact = ?");
            query.setLong(0,contact3.getId());
            String q = query.getQueryString();
            
            Transaction t4 = session.beginTransaction();
            Iterator it = query.list().iterator();
            while (it.hasNext()) {
               Telefone t5  =  (Telefone) it.next();
               session.delete(t5);
            }
            t4.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 7:56 am 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
Take a look at the bidirectional one-to-many/many-to-one documentation:
http://www.hibernate.org/hib_docs/v3/re ... irectional

That's what you want to do. The one-to-one from Telefone back to Endereco is not correct.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 8:38 am 
Newbie

Joined: Thu Dec 01, 2005 6:32 am
Posts: 5
thanks


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.