JPA ManyToMany error during entity save

632 views
Skip to first unread message

Demented

unread,
Aug 2, 2011, 11:13:48 PM8/2/11
to play-framework
Anyone know why I could be getting the following error? Below the
error message I have copied added my yml file along with
Commentary.java and Tag.java. I can't figure out what is wrong with
the setup for the entities. When I try to load the file on startup
that is when I see this error since the bootstrap job is suppose to
run at startup. Any help is greatly appreciated!

2:10:23,364 WARN ~ SQL Error: 23002, SQLState: 23002
22:10:23,364 ERROR ~ Referential integrity constraint violation:
"FK30E3420481FC98DE: PUBLIC.COMMENTARY_TAG FOREIGN
KEY(KEYINDIVIDUALSCITED_ID) REFERENCES PUBLIC.TAG(ID)"; SQL statement:
insert into Commentary_Tag (Commentary_id, stocksCited_id) values
(?, ?) [23002-149]
22:10:23,364 WARN ~ SQL Error: 23002, SQLState: 23002
22:10:23,364 ERROR ~ Referential integrity constraint violation:
"FK30E3420481FC98DE: PUBLIC.COMMENTARY_TAG FOREIGN
KEY(KEYINDIVIDUALSCITED_ID) REFERENCES PUBLIC.TAG(ID)"; SQL statement:
insert into Commentary_Tag (Commentary_id, stocksCited_id) values
(?, ?) [23002-149]
22:10:23,365 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.engine.ActionQueue.executeActions(ActionQueue.java:
268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:
188)
at
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:
345)
at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:
51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at
org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:
795)
at play.db.jpa.JPABase._save(JPABase.java:47)
at play.test.Fixtures.loadModels(Fixtures.java:207)
at Bootstrap.doJob(Bootstrap.java:14)
at play.jobs.Job.doJobWithResult(Job.java:50)
at play.jobs.Job.call(Job.java:146)
at play.jobs.Job.run(Job.java:132)
at play.jobs.JobsPlugin.afterApplicationStart(JobsPlugin.java:116)
at
play.plugins.PluginCollection.afterApplicationStart(PluginCollection.java:
431)
at play.Play.start(Play.java:513)
at play.Play.detectChanges(Play.java:596)
at play.Invoker$Invocation.init(Invoker.java:186)
at play.server.PlayHandler$NettyInvocation.init(PlayHandler.java:168)
at play.Invoker$Invocation.run(Invoker.java:263)
at play.server.PlayHandler$NettyInvocation.run(PlayHandler.java:200)
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: org.h2.jdbc.JdbcBatchUpdateException: Referential integrity
constraint violation: "FK30E3420481FC98DE: PUBLIC.COMMENTARY_TAG
FOREIGN KEY(KEYINDIVIDUALSCITED_ID) REFERENCES PUBLIC.TAG(ID)"; SQL
statement:
insert into Commentary_Tag (Commentary_id, stocksCited_id) values
(?, ?) [23002-149]
at
org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:
1105)
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)
... 28 more
22:10:23,368 ERROR ~

@67a5g50kn
Error during job execution (Bootstrap)

Execution exception (In /app/Bootstrap.java around line 14)
RuntimeException occured : Cannot load fixture initial-data.yml:
org.hibernate.exception.ConstraintViolationException: Could not
execute JDBC batch update


intial-data.yml

Tag(stock1):
name: atvi

Tag(stock2):
name: aapl

Tag(stock3):
name: amzn

Tag(indi1):
name: Warren Buffet

Tag(indi2):
name: Bill Gross

Tag(indi3):
name: Barack Obama

Commentary(foolCommentary):
title: first fool commentary
introduction: this is where the introduction for commentary goes
details: this is where the details go
submissionDate: 2011-07-26
stocksCited:
- stock1
individualsCited:
- indi1

Commentary.java

package models;

import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
public class Commentary extends Model {

@Required
public String title;
@Required
public String introduction;
@Required
@Lob
public String details;
@Required
public Date submissionDate;
@ManyToMany(cascade=CascadeType.PERSIST)
public Set<Tag> stocksCited;
@ManyToMany(cascade=CascadeType.PERSIST)
public Set<Tag> keyIndividualsCited;
@ManyToOne
public Publisher publisher;

public Commentary(String title, String introduction, String details,
Publisher publisher) {
this.title = title;
this.introduction = introduction;
this.details = details;
this.stocksCited = new TreeSet<Tag>();
this.keyIndividualsCited = new TreeSet<Tag>();
this.submissionDate = new Date();
this.publisher = publisher;
}
}

Tag.java

package models;

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

import play.db.jpa.*;

@Entity
public class Tag extends Model implements Comparable<Tag> {

public String name;

private Tag(String name) {
this.name = name;
}

public String toString() {
return name;
}

public int compareTo(Tag otherTag) {
return name.compareTo(otherTag.name);
}

public static Tag findOrCreateByName(String name) {
Tag tag = Tag.find("byName", name).first();
if(tag == null) {
tag = new Tag(name);
}
return tag;
}

}

Demented

unread,
Aug 2, 2011, 11:30:57 PM8/2/11
to play-framework
one thing I just noticed is that when I take out one of the Tag Sets
out of my commentary class (say I remove keyIndividualsCited Set) then
everything works fine and the stocksCited join column is updated
properly. any thoughts??

Demented

unread,
Aug 2, 2011, 11:40:42 PM8/2/11
to play-framework
wow ok this is retarded but all I had to do was give one of the Set's
a jointable name. Why can't JPA know which Set I am using and save
into the join table associated with that Set? I must be doing
something wrong but anyhow this worked and got me over the hump.

@JoinTable(name="KeyIndividualsCited_Tag")

!

Hans
Reply all
Reply to author
Forward
0 new messages