Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
22 replies Latest Post - ‏2013-04-25T14:47:10Z by ECNV_Andrey_Mironenko
ECNV_Andrey_Mironenko
14 Posts
ACCEPTED ANSWER

Pinned topic Openjpa cascade persist sets foreign key to null

‏2013-04-18T09:29:09Z |

I have 2 entities: Routes and HostsHosts has a FK-field route_id referencing to Routes' PK also called route_id. I create object Routes and add Hosts objects to it's hostsCollection hashset. Then I persist Routesobject. Both primary keys are generated correctly, new Routes record is ok, but Hosts record has NULL in route_id field. What am I doing wrong?

Routes.java:

@Entity

public class Routes implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ROUTE_ID")
private long routeId;
 
@Column(name="MSG_QUEUE_NAME")
private String msgQueueName;
 
@Column(name="RECEIPT_QUEUE_NAME")
private String receiptQueueName;
 
@Column(name="QM_NAME")
private String qmName;
 
@OneToMany(mappedBy="routeId", cascade={CascadeType.PERSIST,CascadeType.REMOVE})
private Set<Hosts> hostsCollection;
 
private static final long serialVersionUID = 1L;
 
public Routes(String mqMgrName, String msgQName, String receiptQName, Set<Hosts> newHosts)
{
this.qmName = mqMgrName;
this.msgQueueName = msgQName;
this.receiptQueueName = receiptQName;
this.hostsCollection = newHosts;
}
...
}

Hosts.java:

 

@Entity
public class Hosts implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="HOST_ID")
private long hostId;
 
@Column(name="HOST_NAME")
private String hostName;
 
@Column(name="KEEP_MSG")
private String keepMsg;
 
@Column(name="SEND_RCPT")
private String sendRcpt;
 
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name="ROUTE_ID", referencedColumnName="ROUTE_ID")
private Routes routeId;
 
@OneToMany(mappedBy="hostId")
private Set<Users> usersCollection;
 
public Hosts(String hostName, String keepMsg, String sendRcpt)
{
this.hostName = hostName;
this.keepMsg = keepMsg;
this.sendRcpt = sendRcpt;
}
...
}

 

 

Creating objects:

public String createNewHost()
{
Set<Hosts> newHosts = new HashSet<Hosts>();
Hosts newHost = new Hosts(this.hostName, this.keepMsg, this.sendRcpt);
newHosts.add(newHost);
Routes newRoute = new Routes(this.mqMgrName, this.msgQName, this.receiptQName, newHosts);
dataBean.setCurrentRoute(newRoute);
return "ok";
}

Persisting objects:

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

public void setCurrentRoute(Routes currentRoute) {
em.persist(currentRoute);
em.flush();
}
Updated on 2013-04-18T09:29:39Z at 2013-04-18T09:29:39Z by ECNV_Andrey_Mironenko
  • bpaskin
    bpaskin
    3894 Posts
    ACCEPTED ANSWER

    Re: Openjpa cascade persist sets foreign key to null

    ‏2013-04-18T11:25:27Z  in response to ECNV_Andrey_Mironenko

    Hi, can you also post your XML settings?

     

    Regards,

    Brian

    • ECNV_Andrey_Mironenko
      14 Posts
      ACCEPTED ANSWER

      Re: Openjpa cascade persist sets foreign key to null

      ‏2013-04-18T11:33:04Z  in response to bpaskin

      Hi, Brian.

      I actually don't use explicit XML entity-mapping, but if it matters here is my persistence.xml:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
      <persistence-unit name="RoutesEntities" transaction-type="JTA">
      <jta-data-source>jdbc/ARCH</jta-data-source>
      <class>biz.me.routes.persistence.JPAEntities.Hosts</class>
      <class>biz.me.routes.persistence.JPAEntities.Routes</class>
      <class>biz.me.routes.persistence.JPAEntities.Users</class>
      </persistence-unit>
      </persistence>

       and orm.xml:

       

      <?xml version="1.0" encoding="UTF-8"?>
      <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0">
      <persistence-unit-metadata>
      <persistence-unit-defaults>
      <cascade-persist ></cascade-persist>
      </persistence-unit-defaults>
      </persistence-unit-metadata>
      </entity-mappings>

       

      • bpaskin
        bpaskin
        3894 Posts
        ACCEPTED ANSWER

        Re: Openjpa cascade persist sets foreign key to null

        ‏2013-04-18T11:41:40Z  in response to ECNV_Andrey_Mironenko

        Thanks.  One last piece of information.  Which version of WAS with Fixpak and which DB and version?  There have been numerous fixes to JPA and DB2 that I am aware of that deal with JPA.  I just want to make sure you are using the correct versions.

        Thanks, Brian

        • ECNV_Andrey_Mironenko
          14 Posts
          ACCEPTED ANSWER

          Re: Openjpa cascade persist sets foreign key to null

          ‏2013-04-18T11:53:08Z  in response to bpaskin

          WAS 7.0.0.27

          openjpa-1.2.3-SNAPSHOT-r422266:1395723

          JDBC driver name  : IBM DB2 JDBC Universal Driver Architecture

          JDBC driver version  : 3.57.82

          DB2 v9.7.700.552
          • bpaskin
            bpaskin
            3894 Posts
            ACCEPTED ANSWER

            Re: Openjpa cascade persist sets foreign key to null

            ‏2013-04-18T12:07:25Z  in response to ECNV_Andrey_Mironenko

            Ok, I will try your code a little later.  I am going to use the JPA provider built into WAS first to see if I get any different results.

            Regards, Brian

            • ECNV_Andrey_Mironenko
              14 Posts
              ACCEPTED ANSWER

              Re: Openjpa cascade persist sets foreign key to null

              ‏2013-04-18T12:12:37Z  in response to bpaskin

              The jpa provider I specified is exactly the one built into WAS.

              • bpaskin
                bpaskin
                3894 Posts
                ACCEPTED ANSWER

                Re: Openjpa cascade persist sets foreign key to null

                ‏2013-04-18T16:31:12Z  in response to ECNV_Andrey_Mironenko

                Hi, When you check the DB is it pulling in the correct FK from the ROUTES DB?  It seems to be working on my side.

                Brian

                • ECNV_Andrey_Mironenko
                  14 Posts
                  ACCEPTED ANSWER

                  Re: Openjpa cascade persist sets foreign key to null

                  ‏2013-04-18T18:06:34Z  in response to bpaskin

                  Nope. It inserts all the values correctly for Routes table, but in Hosts table the field route_id is set to NULL instead of correspoding value from Routes.

                  • bpaskin
                    bpaskin
                    3894 Posts
                    ACCEPTED ANSWER

                    Re: Openjpa cascade persist sets foreign key to null

                    ‏2013-04-18T18:46:45Z  in response to ECNV_Andrey_Mironenko

                    That is really strange since it is setup as a FK.  It shows properly on my implementation and in the DB.  Can you check the DB to make sure that the FK is setup properly?

                    Brian

                    • ECNV_Andrey_Mironenko
                      14 Posts
                      ACCEPTED ANSWER

                      Re: Openjpa cascade persist sets foreign key to null

                      ‏2013-04-19T08:25:59Z  in response to bpaskin

                      As I see everything is fine:

                       

                      CREATE TABLE "DB2ADMIN"."HOSTS" (
                      "HOST_ID" BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY ( START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 NO CYCLE CACHE 20 NO ORDER ), 
                      "HOST_NAME" VARCHAR(255) NOT NULL, 
                      "ROUTE_ID" BIGINT, 
                      "KEEP_MSG" CHAR(1) NOT NULL DEFAULT '0', 
                      "SEND_RCPT" CHAR(1)
                      )
                      DATA CAPTURE NONE 
                      COMPRESS NO;
                       
                      CREATE UNIQUE INDEX "DB2ADMIN"."HOST_NAME_IDX"
                      ON "DB2ADMIN"."HOSTS"
                      ("HOST_NAME" ASC)
                      MINPCTUSED 0
                      ALLOW REVERSE SCANS
                      PAGE SPLIT SYMMETRIC
                      COLLECT STATISTICS
                      COMPRESS NO;
                       
                      ALTER TABLE "DB2ADMIN"."HOSTS" ADD CONSTRAINT "SQL130221173148430" PRIMARY KEY
                      ("HOST_ID");
                       
                      ALTER TABLE "DB2ADMIN"."HOSTS" ADD CONSTRAINT "HOST_ROUTE_FK" FOREIGN KEY
                      ("ROUTE_ID")
                      REFERENCES "DB2ADMIN"."ROUTES"
                      ("ROUTE_ID");

                       

                      • bpaskin
                        bpaskin
                        3894 Posts
                        ACCEPTED ANSWER

                        Re: Openjpa cascade persist sets foreign key to null

                        ‏2013-04-19T14:34:07Z  in response to ECNV_Andrey_Mironenko

                        Hi, is it possible for you to do a simple test.  This is the DDL I created and used:

                         

                        CREATE TABLE "DB2ADMIN"."HOSTS" (
                        "HOST_ID" VARCHAR(20) NOT NULL,
                        "HOST_NAME" VARCHAR(20),
                        "KEEP_MSG" VARCHAR(1),
                        "SEND_RCPT" VARCHAR(1),
                        "ROUTE_ID" VARCHAR(20)
                        )
                        DATA CAPTURE NONE;
                         
                        CREATE TABLE "DB2ADMIN"."ROUTES" (
                        "ROUTE_ID" VARCHAR(20) NOT NULL,
                        "MSG_QUEUE_NAME" VARCHAR(20),
                        "RECEIPT_QUEUE_NAME" VARCHAR(20),
                        "QM_NAME" VARCHAR(20)
                        )
                        DATA CAPTURE NONE;
                         
                        ALTER TABLE "DB2ADMIN"."HOSTS" ADD CONSTRAINT "SQL130418105117440" PRIMARY KEY
                        ("HOST_ID");
                         
                        ALTER TABLE "DB2ADMIN"."ROUTES" ADD CONSTRAINT "SQL130418105117130" PRIMARY KEY
                        ("ROUTE_ID");
                         
                        ALTER TABLE "DB2ADMIN"."HOSTS" ADD CONSTRAINT "SQL130418105117630" FOREIGN KEY
                        ("ROUTE_ID")
                        REFERENCES "DB2ADMIN"."ROUTES"
                        ("ROUTE_ID");
                         
                        • ECNV_Andrey_Mironenko
                          14 Posts
                          ACCEPTED ANSWER

                          Re: Openjpa cascade persist sets foreign key to null

                          ‏2013-04-23T11:14:57Z  in response to bpaskin

                          Well, I did it, but I get an error:

                          <openjpa-1.2.3-SNAPSHOT-r422266:1395723 nonfatal general error> org.apache.openjpa.persistence.PersistenceException: DB2 SQL Error: SQLCODE=-407, SQLSTATE=23502, SQLERRMC=TBSPACEID=2, TABLEID=272, COLNO=0, DRIVER=3.57.82 {prepstmnt 376247917 INSERT INTO Routes (ACTIVE_SINCE, ACTIVE_TILL, MSG_QUEUE_NAME, QM_NAME, RECEIPT_QUEUE_NAME, STATUS) VALUES (?, ?, ?, ?, ?, ?) [params=?, ?, ?, ?, ?, ?]} [code=-407, state=23502]SQLCA OUTPUT[Errp=SQLDFMT1, Errd=-2146041828, 28, 0, 0, -1147, 0]

                          And it seems to me that the problem is in 'route_id varchar(20)'.

                          • bpaskin
                            bpaskin
                            3894 Posts
                            ACCEPTED ANSWER

                            Re: Openjpa cascade persist sets foreign key to null

                            ‏2013-04-23T12:29:34Z  in response to ECNV_Andrey_Mironenko

                            Apologies. You are correct the route_id should be an integer:

                            CREATE TABLE "DB2ADMIN"."HOSTS" ("HOST_ID" VARCHAR(20) NOT NULL,"HOST_NAME" VARCHAR(20),"KEEP_MSG" VARCHAR(1),"SEND_RCPT" VARCHAR(1),"ROUTE_ID" INTEGER NOT NULL) DATA CAPTURE NONE;

                            CREATE TABLE "DB2ADMIN"."ROUTES" ("ROUTE_ID" INTEGER NOT NULL,"MSG_QUEUE_NAME" VARCHAR(20),"RECEIPT_QUEUE_NAME" VARCHAR(20),"QM_NAME" VARCHAR(20)) DATA CAPTURE NONE;

                            ALTER TABLE "DB2ADMIN"."HOSTS" ADD CONSTRAINT "SQL130418105117440" PRIMARY KEY ("HOST_ID");

                            ALTER TABLE "DB2ADMIN"."ROUTES" ADD CONSTRAINT "SQL130418105117130" PRIMARY KEY ("ROUTE_ID");

                            ALTER TABLE "DB2ADMIN"."HOSTS" ADD CONSTRAINT "SQL130418105117630" FOREIGN KEY ("ROUTE_ID") REFERENCES "DB2ADMIN"."ROUTES" ("ROUTE_ID");

                            • ECNV_Andrey_Mironenko
                              14 Posts
                              ACCEPTED ANSWER

                              Re: Openjpa cascade persist sets foreign key to null

                              ‏2013-04-23T13:37:38Z  in response to bpaskin

                              And the same with host_id :)

                              I'll try it and report results in a while.

                              • ECNV_Andrey_Mironenko
                                14 Posts
                                ACCEPTED ANSWER

                                Re: Openjpa cascade persist sets foreign key to null

                                ‏2013-04-23T13:53:51Z  in response to ECNV_Andrey_Mironenko

                                In this case it gives me the same error

                                DB2 SQL Error: SQLCODE=-407, SQLSTATE=23502, SQLERRMC=TBSPACEID=2, TABLEID=272, COLNO=0, DRIVER=3.57.82 {prepstmnt 1944089568 INSERT INTO Routes (ACTIVE_SINCE, ACTIVE_TILL, MSG_QUEUE_NAME, QM_NAME, RECEIPT_QUEUE_NAME, STATUS) VALUES (?, ?, ?, ?, ?, ?) [params=?, ?, ?, ?, ?, ?]} [code=-407, state=23502]

                                And the problem seems to be in identity.

                                 

                                • ECNV_Andrey_Mironenko
                                  14 Posts
                                  ACCEPTED ANSWER

                                  Re: Openjpa cascade persist sets foreign key to null

                                  ‏2013-04-23T14:01:19Z  in response to ECNV_Andrey_Mironenko

                                  I added the identity clause to route_id and host_id definitions and got the same situation as in the beginning: route_id is null.

                            • ECNV_Andrey_Mironenko
                              14 Posts
                              ACCEPTED ANSWER

                              Re: Openjpa cascade persist sets foreign key to null

                              ‏2013-04-25T13:02:50Z  in response to bpaskin

                              Any more ideas?

                              • bpaskin
                                bpaskin
                                3894 Posts
                                ACCEPTED ANSWER

                                Re: Openjpa cascade persist sets foreign key to null

                                ‏2013-04-25T14:22:10Z  in response to ECNV_Andrey_Mironenko

                                Hi, in the DB is it showing null for the route_id or is it showing the correct result?

                                Brian

                                • ECNV_Andrey_Mironenko
                                  14 Posts
                                  ACCEPTED ANSWER

                                  Re: Openjpa cascade persist sets foreign key to null

                                  ‏2013-04-25T14:30:46Z  in response to bpaskin

                                  In Routes table it shows correct route_id (automatically generated) and in Hosts table the route_id is null.

                                  • bpaskin
                                    bpaskin
                                    3894 Posts
                                    ACCEPTED ANSWER

                                    Re: Openjpa cascade persist sets foreign key to null

                                    ‏2013-04-25T14:36:19Z  in response to ECNV_Andrey_Mironenko

                                    Hi,

                                    If you tried this outside of JPA via command line or SQL tool and it is fine, then at this point I would open a PMR for IBM to see what is going on in your case.

                                    Regards, Brian

                                    • ECNV_Andrey_Mironenko
                                      14 Posts
                                      ACCEPTED ANSWER

                                      Re: Openjpa cascade persist sets foreign key to null

                                      ‏2013-04-25T14:47:10Z  in response to bpaskin

                                      Ok, I'll get in touch to ibm support.

                                      You said it worked for you on your environment. Could you please email me your code and ddl? andrey dot mironenko at gmail.com

                                       

                                      Thanks for your concern.

                      • ECNV_Andrey_Mironenko
                        14 Posts
                        ACCEPTED ANSWER

                        Re: Openjpa cascade persist sets foreign key to null

                        ‏2013-04-22T07:22:31Z  in response to ECNV_Andrey_Mironenko

                        Excuse me for so long pause, I'll check it todat a little later.