How to use JPA 2.1 property javax.persistence.schema-generation.database.action?

时光怂恿深爱的人放手 提交于 2019-12-21 04:40:28

问题


Allowed values for javax.persistence.schema-generation.database.action are

  • none (that doesn't serve anything...)
  • create (which only works the first time the application is started because all further starts fail due to most databases (e.g. derby 11.x) fail if a schema that already exist is created
  • drop-and-create (which wouldn't ever persist any data which raises the question why it's part of a persistence standard specification - assuming debugging purpose)
  • drop (symetrically fails create succeeds

which leaves absolutely no option to use any of these value nor do I see any sense in their specification - assuming you don't want the user of your application to start the application once (with create), hack the persistence.xml file, repackage you application and use it with none. How to use them to persist Java objects accross application restarts, then?

I know about Hibernate's hibernate.hbm2ddl.auto which works great, but I'm trying to get the mystery solved for a JPA 2.1 implementation portable approach.


回答1:


Hardly any mystery (and they are defined in the spec, or in the docs for any decent JPA implementation). This is run when the EMF is created (or as a separate up front operation if invoked via Persistence.).

"none" means do nothing ... so do like JPA has always done things i.e just persistence. It isn't there to "serve any purpose", just to not do anything extra!

"create" means create the tables for the entities (so it is assumed to not exist yet). Clearly most JPA providers will check existence of tables before creation, so it won't "fail on subsequent uses".

"drop" means drop the tables for the entities (so when running as stand alone process, clean up after tests for example).

"drop-and-create" drop and create the tables for the entities, so get rid of what was there and start from scratch. Nothing to do with persisting data, but then none of these options are.

As said, some of these options are most useful for testing. In a real world situation most people would generate DDL, refine it to meet their DBAs requirements, and generate it manually, then just run JPA hence no use this property at runtime



来源:https://stackoverflow.com/questions/34447653/how-to-use-jpa-2-1-property-javax-persistence-schema-generation-database-action

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!