-->
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.  [ 1 post ] 
Author Message
 Post subject: Getting a ConstraintViolationException and I do not know why
PostPosted: Fri Dec 10, 2010 8:45 pm 
Newbie

Joined: Fri Dec 10, 2010 8:36 pm
Posts: 1
I keep getting a ConstraintViolationException. I think it has something to do with using an @ElementCollection, but I am not sure. Perhaps someone could look at the stack error, and the two classes in question and shed some light on the problem. Thanks!

Quote:
19:33:03,235 DEBUG ~ /* insert models.Trade */ insert into Trade (comments, destinationTeamCash, receiver_id, sender_id, sendingTeamCash, id) values (?, ?, ?, ?, ?, ?)
19:33:03,236 DEBUG ~ /* insert collection row models.Trade.receivingTeamPlayers */ insert into Trade_Player (Trade_id, receivingTeamPlayers_id) values (?, ?)
19:33:03,253 WARN ~ SQL Error: 0, SQLState: 23502
19:33:03,253 ERROR ~ Batch entry 0 /* insert collection row models.Trade.receivingTeamPlayers */ insert into Trade_Player (Trade_id, receivingTeamPlayers_id) values ('1903', '1902') was aborted. Call getNextException to see the cause.

19:33:03,253 WARN ~ SQL Error: 0, SQLState: 23502
19:33:03,253 ERROR ~ ERROR: null value in column "sendingteamplayers_id" violates not-null constraint
19:33:03,254 ERROR ~ 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:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1180)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:267)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:259)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:182)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:324)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:791)
at play.db.jpa.JPABase._save(JPABase.java:47)
at play.db.jpa.GenericModel.save(GenericModel.java:172)
at models.Trade.addTrade(Trade.java:41)
at TradeTest.createTrade(TradeTest.java:88)
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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at play.test.TestEngine.run(TestEngine.java:130)
at controllers.TestRunner.run(TestRunner.java:66)
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 play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:408)
at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:403)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:176)
at play.server.PlayHandler$NettyInvocation.execute(PlayHandler.java:169)
at play.Invoker$Invocation.run(Invoker.java:187)
at play.server.PlayHandler$NettyInvocation.run(PlayHandler.java:149)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.sql.BatchUpdateException: Batch entry 0 /* insert collection row models.Trade.receivingTeamPlayers */ insert into Trade_Player (Trade_id, receivingTeamPlayers_id) values ('1903', '1902') was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2598)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2737)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 64 more


Code:
package models;

import java.util.*;
import javax.persistence.*;

import play.db.jpa.Model;

@Entity
@Table(
   name = "Team"
)
public class Team extends Model {
   @Column(nullable=false)
   public String location, name;
   
   public Integer conference, division;
   
   @OneToMany(mappedBy="team")
   public Set<Player> roster;
   
   @OneToMany(mappedBy="sender")
   public Set<Trade> sentTrades;
   
   @OneToMany(mappedBy="receiver")
   public Set<Trade> receivedTrades;
   
   @ManyToOne
   public League league;
   
   @OneToOne
   public TradeBlock tradeBlock;
   
   @ManyToOne
   public UserAccount owner;
   
   public Date inception;
   
   public Team(String location, String name) {
      this.location = location;
      this.name = name;
      this.roster = new HashSet<Player>();
      this.tradeBlock = new TradeBlock(this).save();
      this.sentTrades = new HashSet<Trade>();
      this.receivedTrades = new HashSet<Trade>();
      
      this.inception = new Date();
   }
}


Code:
package models;

import java.util.*;
import java.math.BigInteger;
import javax.persistence.*;

import play.db.jpa.Model;

@Entity
public class Trade extends Model {
   @ManyToOne
   Team sender;
   
   @ManyToOne
   Team receiver;
   
   @ElementCollection
   Set<Player> sendingTeamPlayers, receivingTeamPlayers;
   
   BigInteger sendingTeamCash, destinationTeamCash;
   
   String comments;
   
   public Trade(Team sender
         , Set<Player> sendingTeamPlayers
         , Team receiver
         , Set<Player> receivingTeamPlayers) {
      this.sender = sender;
      this.sendingTeamPlayers = sendingTeamPlayers;
      this.receiver = receiver;
      this.receivingTeamPlayers = receivingTeamPlayers;
   }
   
   public static void addTrade(Long senderId
         , Set<Player> sendersPlayers
         , Long receiverId
         , Set<Player> receiversPlayers) {
      Team sender = Team.findById(senderId);
      Team receiver = Team.findById(receiverId);
      
      new Trade(sender, sendersPlayers, receiver, receiversPlayers).save();
      
      Trade trade = Trade.find("bySenderIdAndReceiverId", sender.id, receiver.id).first();
      
      sender.sentTrades.add(trade);
      receiver.receivedTrades.add(trade);
      
      sender.save();
      receiver.save();
   }
}


Here is the Test that I am using to run the code:

Code:
import java.util.*;

import org.junit.*;
import play.test.*;

import models.*;

public class TradeTest extends UnitTest {
   @Before
   public void setup() {
      Fixtures.deleteAll();
   }
   
   @Test
   public void createTrade() {
      new Team("Boston", "Rebels").save();
      new Team("Easton", "Shovelmen").save();
      new Player("Winthrop", "Fredwards").save();
      new Player("Oscar", "Bravugan").save();
      
      Team team1 = Team.find("byName", "Rebels").first();
      assertNotNull(team1);
      assertEquals(team1.name, "Rebels");
      
      Team team2 = Team.find("byName", "Shovelmen").first();
      assertNotNull(team2);
      assertEquals(team2.name, "Shovelmen");
      
      Player player1 =
         Player.find("byForenameAndSurname", "Winthrop", "Fredwards").first();
      assertNotNull(player1);
      assertEquals(player1.forename, "Winthrop");
      assertEquals(player1.surname, "Fredwards");
      
      Player player2 =
         Player.find("byForenameAndSurname", "Oscar", "Bravugan").first();
      assertNotNull(player2);
      assertEquals(player2.forename, "Oscar");
      assertEquals(player2.surname, "Bravugan");
      
      team1.roster.add(player1);
      team2.roster.add(player2);
      
      player1.team = team1;
      player2.team = team2;
      
      team1.save();
      team2.save();
      
      player1.save();
      player2.save();
      
      team1 = Team.find("byName", "Rebels").first();
      player1 =
         Player.find("byForenameAndSurname", "Winthrop", "Fredwards").first();
      
      team2 = Team.find("byName", "Shovelmen").first();
      player2 =
         Player.find("byForenameAndSurname", "Oscar", "Bravugan").first();
      
      assertNotNull(player1.id);
      assertNotNull(player2.id);
      
      assertNotNull(team1.roster);
      assertEquals(team1.roster.size(), 1);
      assertTrue(team1.roster.contains(player1));
      
      assertNotNull(team2.roster);
      assertEquals(team2.roster.size(), 1);
      assertTrue(team2.roster.contains(player2));
      
      assertNotNull(player1.team);
      assertEquals(player1.team, team1);
      
      assertNotNull(player2.team);
      assertEquals(player2.team, team2);
      
      Set<Player> stp = new HashSet<Player>();
      stp.add(player1);
      
      assertEquals(stp.size(), 1);
      
      Set<Player> rtp = new HashSet<Player>();
      rtp.add(player2);
      
      assertEquals(rtp.size(), 1);
      
      Trade.addTrade(team1.id, stp, team2.id, rtp);
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.