-->
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: mysql saving to database get wrong
PostPosted: Wed Feb 11, 2004 12:05 pm 
Newbie

Joined: Wed Feb 11, 2004 10:56 am
Posts: 2
i get the following exception if i try to save the customer object to the database:


[junit] [DEMO] DEBUG [main] ServiceLocator.<init>(50) | Looking up Session in JNDI
[junit] [DEMO] INFO [main] ServiceLocator.<init>(56) | error communicating with JNDI, assuming testcase
[junit] [DEMO] DEBUG [main] ServiceLocator.getConnection(199) | Opened hibernate session.
[junit] [DEMO] DEBUG [main] CustomerDAOHibernate.saveCustomer(44) | customer's id: 0
[junit] [DEMO] ERROR [main] SessionImpl.execute(2269) | Could not synchronize database state with session
[junit] net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
[junit] at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
[junit] at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:672)
[junit] at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:625)
[junit] at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
[junit] at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2308)
[junit] at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2262)
[junit] at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2187)
[junit] at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
[junit] at org.appfuse.persistence.CustomerDAOHibernate.saveCustomer(CustomerDAOHibernate.java:57)
[junit] at org.appfuse.persistence.CustomerDAOTest.testGetCustomer(CustomerDAOTest.java:38)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[junit] at java.lang.reflect.Method.invoke(Method.java:324)
[junit] at junit.framework.TestCase.runTest(TestCase.java:154)
[junit] at junit.framework.TestCase.runBare(TestCase.java:127)
[junit] at junit.framework.TestResult$1.protect(TestResult.java:106)
[junit] at junit.framework.TestResult.runProtected(TestResult.java:124)
[junit] at junit.framework.TestResult.run(TestResult.java:109)
[junit] at junit.framework.TestCase.run(TestCase.java:118)
[junit] at junit.framework.TestSuite.runTest(TestSuite.java:208)
[junit] at junit.framework.TestSuite.run(TestSuite.java:203)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:325)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:536)


The source code:

public void testGetCustomer() throws Exception {
Customer customer = new Customer();
customer.setFirstName("Matt");
customer.setLastName("Raible");
customer.setOwner(344);

dao.saveCustomer(customer);
dao.getCustomer(customer.getId());
log.info(customer);
assertTrue(customer.getFirstName() != null);
customer = dao.getCustomer("Tomcat23");
assertNotNull(customer);
assertEquals("Tomcat23", customer.getFirstName());


}

The DAO Object:


public void saveCustomer(Customer customer) throws DAOException {
if (log.isDebugEnabled()) {
log.debug("customer's id: " + customer.getId());
}

Transaction tx = null;

try {
//ses.flush();
tx = ses.beginTransaction();
//customer.setId(0);

ses.saveOrUpdate(customer);

tx.commit();
tx = null;
} catch (HibernateException he) {
if (tx != null) {
try {
tx.rollback();
} catch (HibernateException e) {
}
}
log.error(he);
throw new DAOException(he);
}
}

I did not know what i am making wrong.



The mapping used is :

<?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>
<class
name="org.appfuse.persistence.Customer"
table="CUSTOMER"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="id"
type="int"
>
<generator class="native">
</generator>
</id>

<property
name="address"
type="java.lang.String"
update="true"
insert="true"
column="address"
length="99"
/>

<property
name="city"
type="java.lang.String"
update="true"
insert="true"
column="city"
length="99"
/>

<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
column="firstname"
length="99"
/>

<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="lastname"
length="99"
/>

<property
name="owner"
type="int"
update="true"
insert="true"
column="owner"
not-null="true"
/>

<property
name="zip"
type="java.lang.String"
update="true"
insert="true"
column="zip"
length="5"
/>

<property
name="country"
type="java.lang.String"
update="true"
insert="true"
column="country"
length="99"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Customer.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>

and the properties for hibernate:

<property name="database.jar" location="${mysql.jar}"/>
<property name="database.type" value="mysql"/>
<property name="database.name" value="DEMO"/>
<property name="database.host" value="192.168.110.101"/>
<property name="database.username" value="root"/>
<property name="database.password" value="oriwana"/>

<!-- database URL for creating other databases - used in db-create target -->
<property name="database.admin.url"
value="jdbc:${database.type}://${database.host}/DEMO"/>
<property name="database.admin.username" value="root"/>
<property name="database.admin.password" value="oriwana"/>
<property name="hibernate.dialect"
value="net.sf.hibernate.dialect.MySQLDialect"/>
<property name="database.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="database.url"
value="jdbc:${database.type}://${database.host}/${database.name}"/>
<property name="database.username" value="root"/>
<property name="database.password" value="oriwana"/>
<property name="database.show_sql" value="true"/>

<!-- this task is called by tasks that need it in build.xml -->
<target name="generate.database.properties">
<echo>detected missing database.properties file, building from build.properties</echo>
<propertyfile comment="Hibernate Configuration for JUnit tests"
file="${basedir}/database.properties">
<entry key="hibernate.dialect" value="${hibernate.dialect}"/>
<entry key="hibernate.connection.driver_class" value="${database.driver_class}"/>
<entry key="hibernate.connection.url" value="${database.url}"/>
<entry key="hibernate.connection.username" value="${database.username}"/>
<entry key="hibernate.connection.password" value="${database.password}"/>
<entry key="hibernate.connection.show_sql" value="${database.show_sql}"/>
<entry key="hibernate.jdbc.batch_size" value="0"/>
</propertyfile>
<property file="database.properties"/>
</target>

please could you help me?

thanks,

nico


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 12:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I assume you're using a primitive type as id in your object.

unsaved-value="0" is appropriate for you.

Please read the refence guide and the FAQ before posting.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 12:22 pm 
Newbie

Joined: Wed Feb 11, 2004 10:56 am
Posts: 2
emmanuel wrote:
I assume you're using a primitive type as id in your object.

unsaved-value="0" is appropriate for you.

Please read the refence guide and the FAQ before posting.


so i will do.

thanks

Nico


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.