-->
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.  [ 10 posts ] 
Author Message
 Post subject: DataIntegrityViolationException and BatchUpdateException
PostPosted: Wed Sep 24, 2008 6:48 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
I'm using Hibernate with Spring and using Spring's HibernateDaoSupport class to run my DAO. I'm getting this error when I try to insert a new value into my db:

rg.springframework.dao.DataIntegrityViolationException: Hibernate operation: Could not execute JDBC batch update; SQL [insert into user_realms (users_id, realms_id) values (?, ?)]; Duplicate entry '0' for key 1; nested exception is java.sql.BatchUpdateException: Duplicate entry '0' for key 1
Caused by: java.sql.BatchUpdateException: Duplicate entry '0' for key 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:657)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:373)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:632)
at com.tkassembled.database.UserDAOImpl.persistUser(UserDAOImpl.java:52)
at com.tkassembled.service.HibernateServiceImpl.persistUser(HibernateServiceImpl.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
at $Proxy27.persistUser(Unknown Source)
at com.tkassembled.RemotingHandler.persistUser(RemotingHandler.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.red5.server.service.ServiceInvoker.invoke(ServiceInvoker.java:217)
at org.red5.server.service.ServiceInvoker.invoke(ServiceInvoker.java:123)
at org.red5.server.net.rtmp.RTMPHandler.invokeCall(RTMPHandler.java:157)
at org.red5.server.net.rtmp.RTMPHandler.onInvoke(RTMPHandler.java:409)
at org.red5.server.net.rtmp.BaseRTMPHandler.messageReceived(BaseRTMPHandler.java:143)
at org.red5.server.net.rtmp.RTMPMinaIoHandler.messageReceived(RTMPMinaIoHandler.java:119)
at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messageReceived(AbstractIoFilterChain.java:570)
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299)
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53)
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648)
at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorFilter.java:220)
at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:264)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)


I don't know what the deal is, but if it helps I'm using the AnnotatedSessionFactoryBean in Spring and I'm using Spring 2.08.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 7:19 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
If your database is auto-generating its primary keys and one of those values is a primary key make sure you're telling the ids that they are generated native-ly.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 7:34 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
I found the problem, thanks for posting back. I don't think this is solved yet, but I'm working on it.

I have two objects, a one-to-many relationship as such:
Code:
@OneToMany
@JoinTable(name = "user_userinterests")
private List<UserInterest> interests;


My join table is what's throwing the errors. Since I have many UserInterests associated with essentially one User, this explains the problem. My join table inherently looks like this "user_id | interest_id". When it tries to insert a new value with a matching user_id as before, there are errors. So, I'm going to try and fix this.

Do you have any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 7:55 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
In a one-to-many relationship you should have either; a foreign key to the left table in the right table, or a joining table with a unique restraint on the left column. (left being user, right being interests).

So, you should have a column user_userinterests.user_id as well as user_userinterest.interest_id. And you can specify the relationship:

Code:
    @OneToMany
    @JoinColumn(name="user_id")


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 8:07 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
When I change my code to this:

Code:
@OneToMany
@JoinColumn(name = "user_id", unique = false)
private List<UserInterest> interests;


I get a shiny new Exception:

Quote:
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:373)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:632)
at com.tkassembled.database.UserDAOImpl.persistUser(UserDAOImpl.java:52)
at com.tkassembled.service.HibernateServiceImpl.persistUser(HibernateServiceImpl.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
at $Proxy27.persistUser(Unknown Source)
at com.tkassembled.RemotingHandler.persistUser(RemotingHandler.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.red5.server.service.ServiceInvoker.invoke(ServiceInvoker.java:217)
at org.red5.server.service.ServiceInvoker.invoke(ServiceInvoker.java:123)
at org.red5.server.net.rtmp.RTMPHandler.invokeCall(RTMPHandler.java:157)
at org.red5.server.net.rtmp.RTMPHandler.onInvoke(RTMPHandler.java:409)
at org.red5.server.net.rtmp.BaseRTMPHandler.messageReceived(BaseRTMPHandler.java:143)
at org.red5.server.net.rtmp.RTMPMinaIoHandler.messageReceived(RTMPMinaIoHandler.java:119)
at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messageReceived(AbstractIoFilterChain.java:570)
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299)
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53)
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648)
at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorFilter.java:220)
at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:264)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)


I'm determined to get this thing working.
Do I have to manually define the join column in the UserInterest class?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 8:39 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
Ok, I got rid of the error, but uncovered a few more.
If I go into phpMyAdmin and remove the interest_id INDEX in my join table, inserting values into it works fine.

BUT! I'm learning that its more of a problem of persisting UserInterest objects. I don't know WHY this is, but what's happening is this:
Hibernate was throwing the error when trying to commit a value to the join table.
The reason Hibernate was throwing the error was because it was seeing that each value in the interest_id row in the table must be unique. Hibernate was inserting a value of zero on every commit.

Therefore, I've concluded that it's something wrong with persisting my UserInterest class or something like that. Does anyone else understand why I'm having this problem? I'll check through my code and see if something is going wrong somewhere.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 9:18 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
I really can't seem to figure this out.

I'm going to post my source code:
UserInterest
Code:
package com.tkassembled.data ;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.red5.io.amf3.IDataInput;
import org.red5.io.amf3.IDataOutput;
import org.red5.io.amf3.IExternalizable;

/**
*
*/
@Entity @Table(name = "userinterest")
public class UserInterest implements IExternalizable
{
   /**
    * @private
    */
   @Id @GeneratedValue @Column(name = "id")
   private Integer id;
   /**
    * @private
    */
   @Column(name = "realm")
   private Integer realm = 999;
   /**
    * @private
    */
   @Column(name = "level")
   private Integer level = 0;
   /**
    * @private
    */
   @Column(name = "years")
   private Integer years = 0;
   /**
    * Contains values for a User's realms of interest.
    */
   public UserInterest()
   {
      
   }
   /**
    * @see IExternalizable
    */
   public void readExternal(IDataInput input)
   {
      this.id = input.readInt();
      this.realm = input.readInt();
      this.level = input.readInt();
      this.years = input.readInt();
   }
   /**
    * @see IExternalizable
    */
   public void writeExternal(IDataOutput output)
   {
      output.writeInt(this.id);
      output.writeInt(this.realm);
      output.writeInt(this.level);
      output.writeInt(this.years);
   }
   
   public Integer getId() {return this.id;}
   public void setId(Integer id) { this.id = id; }
   
   public Integer getRealm() { return realm; }
   public void setRealm(Integer realm) { this.realm = realm;   }
   
   public Integer getLevel() { return level; }
   public void setLevel(Integer level) { this.level = level; }
   
   public Integer getYears() { return years; }
   public void setYears(Integer years) { this.years = years; }
}


Again, this is an entry in a collection that I've defined in my User class as so:
Code:
   @OneToMany
   @JoinTable(
      name="users_userinterests",
      joinColumns = @JoinColumn(name = "user_id", unique = false),
      inverseJoinColumns = @JoinColumn(name = "interest_id", unique = false)
   )


And, for kicks, here's my Hibernate output and errors:
Quote:
Hibernate:
insert
into
users
(avatar, country, dob, email, full_name, gender, password, privacy, sign_up_date, username)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
users_userinterests
(user_id, interest_id)
values
(?, ?)
Hibernate:
insert
into
users_userinterests
(user_id, interest_id)
values
(?, ?)
Hibernate:
insert
into
users_userinterests
(user_id, interest_id)
values
(?, ?)
org.springframework.dao.DataIntegrityViolationException: Hibernate operation: Could not execute JDBC batch update; SQL [insert into users_userinterests (user_id, interest_id) values (?, ?)]; Duplicate entry '0' for key 1; nested exception is java.sql.BatchUpdateException: Duplicate entry '0' for key 1
Caused by: java.sql.BatchUpdateException: Duplicate entry '0' for key 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:657)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:373)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:632)
at com.tkassembled.database.UserDAOImpl.persistUser(UserDAOImpl.java:52)
at com.tkassembled.service.HibernateServiceImpl.persistUser(HibernateServiceImpl.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
at $Proxy28.persistUser(Unknown Source)
at com.tkassembled.RemotingHandler.persistUser(RemotingHandler.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.red5.server.service.ServiceInvoker.invoke(ServiceInvoker.java:217)
at org.red5.server.service.ServiceInvoker.invoke(ServiceInvoker.java:123)
at org.red5.server.net.rtmp.RTMPHandler.invokeCall(RTMPHandler.java:157)
at org.red5.server.net.rtmp.RTMPHandler.onInvoke(RTMPHandler.java:409)
at org.red5.server.net.rtmp.BaseRTMPHandler.messageReceived(BaseRTMPHandler.java:143)
at org.red5.server.net.rtmp.RTMPMinaIoHandler.messageReceived(RTMPMinaIoHandler.java:119)
at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messageReceived(AbstractIoFilterChain.java:570)
at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageReceived(AbstractIoFilterChain.java:299)
at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(AbstractIoFilterChain.java:53)
at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.messageReceived(AbstractIoFilterChain.java:648)
at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorFilter.java:220)
at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.run(ExecutorFilter.java:264)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)


Help is extremely appreciated in this. I'm totally hung up on development until I can get this done. Looks like it's gonna be a long day today and it's already 6pm :/


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 12:56 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
So from my output, I'm assuming this must be either a MySQL problem or a Hibernate problem. I thought for a bit that there might be a problem in my serialization, but my classes are making it in just fine and I can read them out.

I'm going to do some debugging of my own, I think there's something wrong with how Hibernate is storing the UserInterest objects. In my Hibernate SQL printouts, I'm seeing this:

Hibernate insert new User
Hibernate insert new UserInterest join entry
Hibernate insert new UserInterest join entry
Hibernate insert new UserInterest join entry
Hibernate fails.

Hibernate never inserts the actual UserInterest entities and fails when trying to insert the entries to the join table. WTF? It's inserting repeated values of zero in the interest_id column, which is throwing the BatchUpdateException. What the heck?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 1:24 pm 
Beginner
Beginner

Joined: Wed Sep 24, 2008 5:44 pm
Posts: 34
Make sure you're cascading your updates from User to the Interests!

@OneToMany(cascade=CascadeType.ALL)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 1:39 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
I found an example that kind of helped me find the solution. I had to map a OneToMany in the User class and a ManyToOne in the UserInterest class and add a user field to that class. Then, I had to create a sort of proxy for persisting the objects. Weird, but I understand why it wouldn't work otherwise. Hibernate is an enigma to me :)


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