Sorry, I forgot to persist before flushing, this one works:

           Company c = new Company();
           c.setId( (long)i );
           c.setName( "Company "+i );
           c.setCustomers( new HashSet<Customer>() );
           em.persist( c );
           em.flush();
for ( int j = 0; j < 5; j++ ) {
               Customer cus = new Customer();
               cus.setId( 100L*i+j );
               cus.setName( "Customer" + 100L*i+j );
               cus.setCompany( c );
               c.getCustomers().add( cus );
           }
em.merge( c );
           em.flush();



Sebastian

Rajeev Jha schrieb:
Idea is to flush the parent table data before attempting to flush the
child table that uses parent table ID as FK. flush() on entity should
work in your case too.

On Tue, Apr 22, 2008 at 3:14 PM, Sebastian Gauder
<[EMAIL PROTECTED]> wrote:
Ok, this seems a known issue.

 I tried that additional flush() you mentioned, and debugged it. OpenJPA
seems to ignore it, The INSERT-Statement for Company is not executed until
the second flush()...

 Sebastian


 Rajeev Jha schrieb:



https://issues.apache.org/jira/browse/OPENJPA-235

On Tue, Apr 22, 2008 at 2:07 PM, Sebastian Gauder
<[EMAIL PROTECTED]> wrote:


Hey,

 I'm trying to persist the following code:

 Company c = new Company();
 c.setId( (long)i );
 c.setName( "Company "+i );
 c.setCustomers( new HashSet<Customer>() );
         for ( int j = 0; j < 5; j++ ) {
  Customer cus = new Customer();
  cus.setId( 100L*i+j );
  cus.setName( "Customer" + 100L*i+j );
  cus.setCompany(c);
  c.getCustomers().add(cus);
 }
         em.persist(c);
 em.flush();


 with these entities:

 @Entity
 @Table(name = "JBT_Company")
 public class Company {

  protected Long id;

  public void setId(Long inId) {
      this.id = inId;
  }

  @Id
  public Long getId() {
      return id;
  }

  protected String name;

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  protected Set<Customer> customerSet;

  @OneToMany(mappedBy = "company",cascade = CascadeType.ALL)
  public Set<Customer> getCustomers() {
      return customerSet;
  }

  public void setCustomers(Set<Customer> inCustomerSet) {
      this.customerSet = inCustomerSet;
  }
 }


 @Entity
 @Table(name = "JBT_Customer")
 public class Customer {

  protected Long id;

  public void setId(Long inId) {
      this.id = inId;
  }

  @Id
  public Long getId() {
      return id;
  }

  protected String name;

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }

  protected Company company;

  @ManyToOne
  @JoinColumn(name = "company_id")
  public Company getCompany() {
      return company;
  }

  public void setCompany(Company inCompany) {
      this.company = inCompany;
  }
 }


 It works for MySQL, but not for PostgreSQL 8.2. The follwing Exception
is
thrown:

 40235  postgres  INFO   [RMI TCP Connection(6)-10.0.10.88]
openjpa.Runtime
- Starting OpenJPA 1.0.2
 40766  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 3920972> executing prepstmnt 5188636 SELECT NULL AS
SEQUENCE_SCHEMA, relname AS SEQUENCE_NAME FROM pg_class WHERE
relkind='S'
 40782  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 3920972> [16 ms] spent
 41313  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 24050576> executing prepstmnt 17582220 DELETE FROM
JBT_Customer
 41328  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 24050576> [15 ms] spent
 41328  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 24050576> executing prepstmnt 3724097 DELETE FROM
JBT_Company
 41328  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 24050576> [0 ms] spent
 41422  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> executing prepstmnt 27655609 INSERT INTO
JBT_Customer (id, name, company_id) VALUES (?, ?, ?) [params=(long) 3,
(String) Customer03, (long) 0]
 41438  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> [16 ms] spent
 41438  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> executing prepstmnt 29204960 INSERT INTO
JBT_Customer (id, name, company_id) VALUES (?, ?, ?) [params=(long) 0,
(String) Customer00, (long) 0]
 41453  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> [15 ms] spent
 41453  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> executing prepstmnt 13540318 INSERT INTO
JBT_Customer (id, name, company_id) VALUES (?, ?, ?) [params=(long) 1,
(String) Customer01, (long) 0]
 41453  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> [0 ms] spent
 41453  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> executing prepstmnt 5978200 INSERT INTO
JBT_Company (id, name) VALUES (?, ?) [params=(long) 0, (String) Company
0]
 41469  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> [16 ms] spent
 41469  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> executing prepstmnt 13640927 INSERT INTO
JBT_Customer (id, name, company_id) VALUES (?, ?, ?) [params=(long) 4,
(String) Customer04, (long) 0]
 41469  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> [0 ms] spent
 41469  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> executing prepstmnt 19188912 INSERT INTO
JBT_Customer (id, name, company_id) VALUES (?, ?, ?) [params=(long) 2,
(String) Customer02, (long) 0]
 41485  postgres  TRACE  [RMI TCP Connection(6)-10.0.10.88]
openjpa.jdbc.SQL
- <t 31001198, conn 11834918> [16 ms] spent
 22.04.2008 10:12:26
org.springframework.remoting.support.RemoteInvocationTraceInterceptor
invoke
 WARNUNG: Processing of RmiServiceExporter remote call resulted in fatal
exception: com.denkwerk.jbt.server.spring.SpringBenchmarkBean.buildUp
 <openjpa-1.0.2-r420667:627158 fatal general error>
org.apache.openjpa.persistence.PersistenceException: The transaction has
been rolled back.  See the nested exceptions for details on the errors
that
occurred.
  at

org.apache.openjpa.kernel.BrokerImpl.newFlushException(BrokerImpl.java:2108)
  at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1955)
  at
org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1853)
  at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1624)
  at

org.apache.openjpa.kernel.DelegatingBroker.flush(DelegatingBroker.java:973)
  at

org.apache.openjpa.persistence.EntityManagerImpl.flush(EntityManagerImpl.java:488)
  at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source)
  at

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at

org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:193)
  at $Proxy7.flush(Unknown Source)
  at com.denkwerk.jbt.elements.benchmarks.B03.B03.buildUp(B03.java:48)
  at
com.denkwerk.jbt.server.AbstractRunner.buildUp(AbstractRunner.java:21)
  at

com.denkwerk.jbt.server.spring.PostgresQLBenchmarkBeanImpl.buildUp(PostgresQLBenchmarkBeanImpl.java:21)
  at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
  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:310)
  at

org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
  at

org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
  at

org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
  at

org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
  at

org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
  at $Proxy8.buildUp(Unknown Source)
  at sun.reflect.GeneratedMethodAccessor19.invoke(Unknown Source)
  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:310)
  at

org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
  at

org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
  at

org.springframework.remoting.support.RemoteInvocationTraceInterceptor.invoke(RemoteInvocationTraceInterceptor.java:70)
  at

org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
  at

org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
  at $Proxy8.buildUp(Unknown Source)
  at sun.reflect.GeneratedMethodAccessor36.invoke(Unknown Source)
  at

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at

org.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:205)
  at

org.springframework.remoting.support.DefaultRemoteInvocationExecutor.invoke(DefaultRemoteInvocationExecutor.java:38)
  at

org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:78)
  at

org.springframework.remoting.rmi.RmiBasedExporter.invoke(RmiBasedExporter.java:72)
  at

org.springframework.remoting.rmi.RmiInvocationWrapper.invoke(RmiInvocationWrapper.java:72)
  at sun.reflect.GeneratedMethodAccessor15.invoke(Unknown Source)
  at

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
  at sun.rmi.transport.Transport$1.run(Transport.java:159)
  at java.security.AccessController.doPrivileged(Native Method)
  at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
  at
sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
  at

sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
  at

sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
  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)
 Caused by: <openjpa-1.0.2-r420667:627158 nonfatal general error>
org.apache.openjpa.persistence.PersistenceException: ERROR: insert or
update
on table "jbt_customer" violates foreign key constraint
"fkf1ca16c177e66402"
 Detail: Key (company_id)=(0) is not present in table "jbt_company".
{prepstmnt 27655609 INSERT INTO JBT_Customer (id, name, company_id)
VALUES
(?, ?, ?) [params=(long) 3, (String) Customer03, (long) 0]} [code=0,
state=23503]
 FailedObject: [EMAIL PROTECTED]
  at

org.apache.openjpa.jdbc.sql.DBDictionary.newStoreException(DBDictionary.java:3946)
  at

org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:97)
  at

org.apache.openjpa.jdbc.sql.SQLExceptions.getStore(SQLExceptions.java:67)
  at

org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(PreparedStatementManagerImpl.java:108)
  at

org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flush(PreparedStatementManagerImpl.java:73)
  at

org.apache.openjpa.jdbc.kernel.ConstraintUpdateManager.flush(ConstraintUpdateManager.java:543)
  at

org.apache.openjpa.jdbc.kernel.ConstraintUpdateManager.flush(ConstraintUpdateManager.java:105)
  at

org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:89)
  at

org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:72)
  at

org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:514)
  at

org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:130)
  ... 54 more
 Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: ERROR:
insert
or update on table "jbt_customer" violates foreign key constraint
"fkf1ca16c177e66402"
 Detail: Key (company_id)=(0) is not present in table "jbt_company".
{prepstmnt 27655609 INSERT INTO JBT_Customer (id, name, company_id)
VALUES
(?, ?, ?) [params=(long) 3, (String) Customer03, (long) 0]} [code=0,
state=23503]
  at

org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:192)
  at

org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$800(LoggingConnectionDecorator.java:57)
  at

org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeUpdate(LoggingConnectionDecorator.java:858)
  at

org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:269)
  at

org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:269)
  at

org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement.executeUpdate(JDBCStoreManager.java:1363)
  at

org.apache.openjpa.jdbc.kernel.PreparedStatementManagerImpl.flushInternal(PreparedStatementManagerImpl.java:97)
  ... 61 more
 NestedThrowables:
 ...

 Seems, that OpenJPA has Problems with the order of INSERT Statements
?!?
 Sebastian





--
diplomand software-entwicklung

denkwerk gmbh | vogelsanger straße 66 | d-50823 köln
telefon +49 221 2942 100 | telefax +49 221 2942 101
GF: Axel Schmiegelow, Marco Zingler, Jochen Schlaier, Frank Zimpel
HRB 32063 Amtsgericht Köln - www.denkwerk.com

Reply via email to