Search code examples
jakarta-eejpanetbeanseclipselink

JPA - Error when new entities or attributes are added without drop and create


I am developing a Java EE application with JSF, EclipseLink2 as JPA installation and mySQL as the back-end database with Netbeans.

The application works fine except when I add new attributes to existing entities, or create new Entities. If I select the dropandcreate option in Persistance.xml file, the application runs again without errors, but it deletes the whole data base with previous entries.

When I go to production, I can not afford to lose data of the database when I add a new entity. Is there any way to overcome this. I tried to use a database schema, but could not figure how to do it.


Solution

  • You have only three options for schema generation (source with more details):

    • none: generates nothing
    • create-tables: typically creates new tables and keeps data in old ones
    • drop-and-create-tables: old tables are dropped and new ones created, so all data will be lost.

    As you see, any of these does not support your scenario where you have to keep data and add columns. You can probably figure out that there will be many other scenarios not supported by automatic schema generation, like changing type of column for example and transforming data in general.

    Because generating schema will not work in your case, it is better to keep it as a development time tool and learn to write schema for production by yourself. More opinions about this matter can be found from this discussion. It is about Hibernate, so property names are different, but problem is same with EclipseLink.