0

I have one java project. In that I use @Entity. In my project have some constraints. Tables should be created automatically. So I use JPa @Entity. what the problem is that, when ever the Program is restarting, the new table is created by the Entity. So my old data got losed. I need those type of data. Trap Entity should not overwrite my content in Database? How to solve it?

Termininja
  • 6,620
  • 12
  • 48
  • 49
Vinoth
  • 7
  • 2
  • 10
  • What ORM are you using? Hibernate? – Kevin Bowersox Aug 01 '13 at 09:34
  • You have configured a "drop and create tables" DDL option with which ever provider you are using instead of just a create tables option. Check your provider settings and docs for how to change it – Chris Aug 01 '13 at 11:52

2 Answers2

0

In the persistence.xml file there should be a setting to turn off the auto generation of DLL. Here is an example of a persistence.xml file using hibernate where the hibernate.hbm2ddl.auto property has been set to validate, which will cause the tables not to be overwritten. If your relying on Hibernate to create your DDL you would not want to have this set the first time your run the application, it would be set after running once and successfully creating the DDL (Tables).

If this setting were set to create-drop the schema would recreate the tables each time the persistence context is created, causing all data to be lost. This property will be specific to your ORM vendor's implementation.

The best advice is to find the documentation for your vendor's specific setting and then choose the option which best fits your needs. This post does a good job of describing the possible values for the setting in Hibernate.

<persistence-unit name="toThought" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>  
      <property name="hibernate.hbm2ddl.auto" value="validate"/>        
    </properties>
</persistence-unit>

In EclipseLink this property is:

<property name="eclipselink.ddl-generation" value="create-tables"/>
Community
  • 1
  • 1
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
0

use <property name="hibernate.hbm2ddl.auto" value="update"/> in your persistence.xml . If you have not generate the schema then first use <property name="hibernate.hbm2ddl.auto" value="create"/> and the schema will be generated. Then use <property name="hibernate.hbm2ddl.auto" value="update"/> since now you have the schema.

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50