-->
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.  [ 2 posts ] 
Author Message
 Post subject: ConstraintViolationException using @OneToMany and @JoinTable
PostPosted: Thu Mar 20, 2008 4:35 pm 
Newbie

Joined: Thu Mar 20, 2008 12:57 pm
Posts: 1
Hibernate version:3
MySQL 5
JBoss 4
MyEclipse

I have searched these forums and the web related to this exception and not been able to figure out the issue in my case:

ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

I am using Java, Jboss, Hibernate with MyEclipse. I have a one-to-many relationship defined between 2 entities that can exist independently.

The way the relationship is supposed to work, is as follows. At any time, there may be 1 or more DHePointInstance instances in the system. Each DHeSession can add in as many of these instances as they like into their pointsInSession collection. The issue Im having is that each session can only add in a DHePointInstance that has not been added into any other DHeSession pointsInSession collection. In other words, it's a one to many as long as a DHePointInstance is not added into more than one DHeSession's pointsInSession collection.

Is this just how it is supposed to work or is there a different way of defining the relationship that will allow me to have it work the way I want, which is the same DHePointInstance may be added into many different DHeSession.pointsInSession collection's.

The class definitions and generated sql tables are below, as well as the full exception trace.

Here are the class definitions :
Code:
@Entity
@Table(name="session")

public class DHeSession extends DHaEntityBean
{
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;   
   @Temporal(TemporalType.TIMESTAMP)
   private Date startTime;   
   @Temporal(TemporalType.TIMESTAMP)
   private Date endTime;   

   @OneToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH})
   private DHeUser user;

   @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.ALL})      
   @JoinTable(name="session_TO_orderedItemHolder")   
   @MapKey(name="position")
   @Sort(type=SortType.COMPARATOR, comparator=DHhIntegerComparator.class)
   @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)      
   protected SortedSet<DHeOrderedItemHolder> orderedItems = new TreeSet<DHeOrderedItemHolder>();   
   
   @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH})
   @JoinTable(name="session_TO_pointInstance")   
   private Collection<DHePointInstance> pointsInSession = new Vector<DHePointInstance>();

......

This is the relationship that is causing the problem :
@OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH})
@JoinTable(name="session_TO_pointInstance")
private Collection<DHePointInstance> pointsInSession = new Vector<DHePointInstance>();



Here is the DHePointInstance class Definition :

Code:
@Entity
@Table(name="pointInstance")
public class DHePointInstance extends DHePointObjectWithAttributes
{
   private String description;
   
   @ManyToOne(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
   @JoinColumn(name="pointImplId")      
   private DHePointImpl pointImpl;
      
   @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.ALL})
   @MapKey(name="portType")
   @JoinTable(name="pointInstance_TO_portMapping")   
   @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)   
   protected Map<DHnPortType,DHePortMapping> portMappings = new HashMap<DHnPortType,DHePortMapping>();
   
   @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.ALL})
   @MapKey(name="name")
   @JoinTable(name="pointInstance_TO_parameters")   
   @org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)   
   protected Map<DHnPointInstanceParameterName,DHePointInstanceParameter> parameters = new HashMap<DHnPointInstanceParameterName,DHePointInstanceParameter>();
      
   @ManyToOne
   @JoinColumn(name="pointGroup")
   private DHePointGroup pointGroup;

   // ioDriverClassName used to be defined at the impl level but we moved it to the instance so that we can
   // easily run emulators for the device points.
   private String ioDriverClassName;
   


Table Definitions

Code:
CREATE TABLE `session` (
  `id` int(11) NOT NULL auto_increment,
  `startTime` datetime default NULL,
  `endTime` datetime default NULL,
  `user_id` int(11) default NULL,
  PRIMARY KEY  (`id`),
  KEY `FK76508296D30EAACD` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
set names utf8;


Code:
CREATE TABLE `session_TO_pointInstance` (
  `session_id` int(11) NOT NULL,
  `pointsInSession_id` int(11) NOT NULL,
  UNIQUE KEY `pointsInSession_id` (`pointsInSession_id`),
  KEY `FKC190C60AF0E6E157` (`pointsInSession_id`),
  KEY `FKC190C60A434E27E6` (`session_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
set names utf8;


Code:
CREATE TABLE `pointInstance` (
  `id` int(11) NOT NULL,
  `description` varchar(255) default NULL,
  `pointGroup` int(11) default NULL,
  `pointImplId` int(11) default NULL,
  `ioDriverClassName` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `FKCA8BE456B2D801` (`pointImplId`),
  KEY `FKCA8BE45C23B0F18` (`pointGroup`),
  KEY `FKCA8BE4518EE120D` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
set names utf8;



Full Stack Trace of Exception :

16:23:32,492 INFO [STDOUT] Hibernate:
insert
into
session_TO_pointInstance
(session_id, pointsInSession_id)
values
(?, ?)
16:23:32,492 INFO [STDOUT] Hibernate:
insert
into
session_TO_pointInstance
(session_id, pointsInSession_id)
values
(?, ?)
16:23:32,499 WARN [JDBCExceptionReporter] SQL Error: 1062, SQLState: 23000
16:23:32,499 ERROR [JDBCExceptionReporter] Duplicate entry '17' for key 1
16:23:32,499 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
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.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:515)
at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1491)
at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1110)
at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:324)
at org.jboss.tm.TxManager.commit(TxManager.java:240)
at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:102)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:211)
at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:79)
at $Proxy389.updateSession(Unknown Source)
at com.dh.ui.message.session.DHzSessionDetails.addPointInstanceButtonClickHandler(DHzSessionDetails.java:236)
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:585)
at bsh.Reflect.invokeMethod(Unknown Source)
at bsh.Reflect.invokeObjectMethod(Unknown Source)
at bsh.Name.invokeMethod(Unknown Source)
at bsh.BSHMethodInvocation.eval(Unknown Source)
at bsh.BSHPrimaryExpression.eval(Unknown Source)
at bsh.BSHPrimaryExpression.eval(Unknown Source)
at bsh.Interpreter.eval(Unknown Source)
at bsh.Interpreter.eval(Unknown Source)
at org.zkoss.zk.scripting.bsh.BSHInterpreter.exec(BSHInterpreter.java:80)
at org.zkoss.zk.scripting.util.GenericInterpreter.interpret(GenericInterpreter.java:283)
at org.zkoss.zk.ui.impl.PageImpl.interpret(PageImpl.java:843)
at org.zkoss.zk.ui.impl.EventProcessor.process0(EventProcessor.java:170)
at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:138)
at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:452)
at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.run(EventProcessingThreadImpl.java:370)
Caused by: java.sql.BatchUpdateException: Duplicate entry '17' for key 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1213)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:912)
at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:519)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 49 more
16:23:32,520 ERROR [STDERR] Mar 20, 2008 4:23:32 PM org.zkoss.zk.ui.impl.UiEngineImpl handleError:764
SEVERE: >>java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=dhl01/148, BranchQual=, localId=148] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update)
>>org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=dhl01/148, BranchQual=, localId=148] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update)
>>javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
>>org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
[SQL: 1062, 23000]
>>java.sql.BatchUpdateException: Duplicate entry '17' for key 1
>> at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1213)
>> at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:912)
>> at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:519)
>> at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
>> at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
>>...



Thanks
-Andy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 23, 2008 11:09 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
I didn't check all of your code, but here is the real fault:
Quote:
>>java.sql.BatchUpdateException: Duplicate entry '17' for key 1

You should have a look of what your app is doing!

Carlo


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