-->
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.  [ 7 posts ] 
Author Message
 Post subject: Trying to save a Object relationship with anohter Object
PostPosted: Wed Aug 11, 2004 11:29 am 
Newbie

Joined: Wed Aug 11, 2004 11:12 am
Posts: 10
Location: Brazil
HI There

I


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 11:35 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
Recheck the line in bold in your mapping :

<many-to-one
name="cidades"
class="br/com/junior/models/Cidades"
cascade="none"
outer-join="auto"
column="cidade"
/>

_________________
Thanks,
Binil Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 1:00 pm 
Newbie

Joined: Wed Aug 11, 2004 11:12 am
Posts: 10
Location: Brazil
binil wrote:
Recheck the line in bold in your mapping :

<many-to-one
name="cidades"
class="br/com/junior/models/Cidades"
cascade="none"
outer-join="auto"
column="cidade"
/>


Hi binil, thanks for answer

I changed cascade="none" to cascade="all", and now I have this exception

Quote:
java.sql.BatchUpdateException: Invalid argument value, message from server: "Duplicate entry '2' for key 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1394)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:59)
at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:56)
at net.sf.hibernate.impl.BatcherImpl.prepareBatchStatement(BatcherImpl.java:109)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:460)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2335)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at br.com.junior.system.DAO.insert(DAO.java:102)


the Clientes ID is 1 and ID from Cidades that I selected is 2


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 2:08 pm 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
Code:
java.sql.BatchUpdateException: Invalid argument value, message from server: "Duplicate entry '2' for key 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1394)


I think this means that you have defined some unique column constraint in your table which is getting violated. The table already has a row with value "2" for that column, but you are trying to insert a new row with "2" for that unique column.

Enable show_sql in your Hibernate properties and see what are the SQL being fired.

Also, searching MySQL lists might help.

_________________
Thanks,
Binil Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 2:12 pm 
Newbie

Joined: Wed Aug 11, 2004 11:12 am
Posts: 10
Location: Brazil
binil wrote:
Code:
java.sql.BatchUpdateException: Invalid argument value, message from server: "Duplicate entry '2' for key 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1394)


I think this means that you have defined some unique column constraint in your table which is getting violated. The table already has a row with value "2" for that column, but you are trying to insert a new row with "2" for that unique column.

Enable show_sql in your Hibernate properties and see what are the SQL being fired.

Also, searching MySQL lists might help.


I already enable.
Before to save Clientes, hibernate is trying to save Cidades too, using insert into clientes (....) and Cliente with ID number 2 already exist.
The correct would be hibernate just save clientes.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 2:55 pm 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
I am curious how this worked:
<many-to-one
name="cidades"
class="br/com/junior/models/Cidades"
cascade="none"
outer-join="auto"
column="cidade"
/>

The class should have been specified with .'s rather than /'s, right?

_________________
Thanks,
Binil Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 3:37 pm 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
I tried the following.

My tables are like :
Code:
SQL> desc my_one;
Name                                      Null?    Type
----------------------------------------- -------- ---------------

MY_ID                                     NOT NULL NUMBER(19)
TWO_ID                                             NUMBER(19)
NAME                                               VARCHAR2(255)

SQL> desc my_two;
Name                                      Null?    Type
----------------------------------------- -------- ---------------

MY_ID                                     NOT NULL NUMBER(19)
NAME                                               VARCHAR2(255)


My code is as :
Code:
package tavant.platform.persistence.hibernate.cascade;

public class One
{
    private long id;
    private String name;
    private Two two;
    // ... getters and setters for those properties
}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="tavant.platform.persistence.hibernate.cascade">
    <class name="One" table="MY_ONE">
        <id name="id" column="MY_ID" type="long">
            <generator class="native"/>
        </id>

        <many-to-one name="two" class="Two"
            cascade="all" outer-join="auto" column="TWO_ID"/>

        <property name="name" column="NAME"/>
    </class>
</hibernate-mapping>


Code:
package tavant.platform.persistence.hibernate.cascade;

public class Two
{
    private long id;
    private String name;
    // ... getters and setters
}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="tavant.platform.persistence.hibernate.cascade">
    <class name="Two" table="MY_TWO">
        <id name="id" column="MY_ID" type="long">
            <generator class="native"/>
        </id>

        <property name="name" column="NAME"/>
    </class>
</hibernate-mapping>


I inserted a row into MY_TWO table as :
Code:
SQL> insert into my_two(my_id, name) values(1, 'Hello Two');


And then, I tried the following code :
Code:
package tavant.platform.persistence.hibernate.cascade;

import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

public class Test
{
    public static void main(String[] args) throws Exception
    {

        SessionFactory sessions = new Configuration()
            .addClass(One.class)
            .addClass(Two.class)
            .buildSessionFactory();

        Session session = sessions.openSession();
        Transaction transaction = session.beginTransaction();
        One one = new One();
        one.setName("One 1");
        Two two = new Two();
        two.setName("Two 2");
        two.setId(1); // a DB row with ID 1 already exists.
        one.setTwo(two);
        session.save(one);
        transaction.commit();
        session.close();

        System.out.println("Saved ONE");
    }
}


It worked fine for me. I use Hibernate 2.1.4 on Oracle 8i. Please try it.

_________________
Thanks,
Binil Thomas


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.