Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema generation with existing EMF #53

Closed
lukasj opened this issue Mar 26, 2013 · 6 comments
Closed

Schema generation with existing EMF #53

lukasj opened this issue Mar 26, 2013 · 6 comments

Comments

@lukasj
Copy link
Contributor

lukasj commented Mar 26, 2013

I've been running into a conceptual issue with schema generation in JPA 2.1 and how unit tests are typically organized.

Let's say you want a clean database for every unit test method. I prefer to create the schema in the database before every test method, and then drop it after the test method completes.

I have a single EntityManagerFactory for the whole test class, it can be shared by every test method. This is possible with the Hibernate API, the schema generator uses an existing EMF configuration to produce the create/drop scripts.

With the static Persistence.generateSchema() in 2.1 this approach doesn't work. Calling generateSchema() internally builds a new EMF every time.

One of the problems is that you get new automatically generated foreign key names every time you call generateSchema(). The "drop" action therefore always fails, unless you define all foreign key names in your metadata. There could be other automatically generated artifact names, so the "drop" action in general, if called from a static context, is not useful during development.

The current solution then is to specify "drop-and-create" and to build and close an EMF for every unit test method. This means starting and stopping the persistence provider for every test method, slowing down test runs significantly.

A better solution would be an additional Persistence.generateSchema(EMF, properties) method that accepts an existing EMF and some "override" properties.

@lukasj
Copy link
Contributor Author

lukasj commented Mar 26, 2013

@glassfishrobot Commented
Reported by cbauer123

@lukasj
Copy link
Contributor Author

lukasj commented Apr 4, 2013

@glassfishrobot Commented
cbauer123 said:
Additionally, consider adding the database action "create-drop". This would issue CREATE statements when the EMF is build, and DROP statements when the EMF is closed. Granted, this is a useless setting with static Persistence.generateSchema() but it would, in addition to the above proposed changes, give you full flexibility for schema generation. Use case is for example a @BeforeMethod in a test harness that builds an EMF and an @AfterMethod that closes the EMF.

@lukasj
Copy link
Contributor Author

lukasj commented May 5, 2017

@glassfishrobot Commented
This issue was imported from java.net JIRA JPA_SPEC-53

@lukasj
Copy link
Contributor Author

lukasj commented Aug 31, 2018

@gavinking
Copy link
Contributor

This problem is being addressed in #399, and we can close this issue.

@lukasj lukasj added this to To do in 3.2.0 via automation Aug 21, 2023
@lukasj
Copy link
Contributor Author

lukasj commented Aug 21, 2023

done through #431

@lukasj lukasj closed this as completed Aug 21, 2023
3.2.0 automation moved this from To do to Done Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
3.2.0
Done
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants