4 Replies Latest reply on Jan 3, 2011 8:23 AM by 826749

    OpenJPA "Drop and create tables" option doesn't work

      Hi, I need to drop and recreate my schema in MySql every time a java test is launched.
      In EclipseLink I used the property

      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

      in persistence.xml file and it works fine.
      In OpenJpa I use this property

      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction=&apos;add,deleteTableContents&apos;,ForeignKeys=true)"/>

      (as inserted by NetBeans in the design window after clicking on drop/create radio button) but it doesn't work...

      anyone could help me?
        • 1. Re: OpenJPA "Drop and create tables" option doesn't work
          I don't know what OpenJPA defaults to but the following works fine in EclipseLink using "database" or "both".
          <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
          <property name="eclipselink.ddl-generation.output-mode" value="database"/>

          Is there a reason you need to use OpenJPA. I ask this because EclipseLink is JPA 2.0 compliant, current and flexible.

          thank you
          • 2. Re: OpenJPA "Drop and create tables" option doesn't work
            Hi Michael, thanks for your reply. I'm implementing using openJpa because I've found the FK value setting in child elements simpler than in eclipselink, as this link says:


            I have for example 1 user with 2 email objects associated: when I persist in my MySQL db using eclipselink (following user's guide and other web tutorials) I have 1 user record and 2 email records (in respective tables), but in each email the FK column is null (I'd need to don't call the email.setUser setter method).... I tried it in openJpa setting that options above and it works fine, every email has its FK linking to the right user...

            Although it's not the subject of this post I try to ask you: is this possibile even in EclipseLink? Have you any suggestion about setting FK value in db using eclipselink without calling email.setUser setter method?

            Thank you very much,

            • 3. Re: OpenJPA "Drop and create tables" option doesn't work
              Hello Matthew,

              As the link states, bidirectional relationship maintenence is not provided for in Java and was excluded from JPA despite it existing in EJB2.0. EclipseLink supports EJB2.0, and does have this feature available, but I would strongly discourage it - personally I found it difficult to support and maintain references that use it. Generally it is as simple as putting a helper method on one entity that sets the references on both ie User.setEmail() could set the user's email, and if the email is not null, the email's user. This makes it more clear how and why the email's user is or isn't set, especially if the objects are ever reused outside of a persistence context. This is somewhat described in http://en.wikibooks.org/wiki/Java_Persistence/Relationships under common problems

              That said, the EclipseLink feature can be turned on using a customizer to call setRelationshipPartnerAttributeName on the mappings for both sides of the relationship.

              Best Regards,
              • 4. Re: OpenJPA "Drop and create tables" option doesn't work
                thank you Chris, the link you provided has been very helpful!