6

I have a mysql database that I want to add the functionality of adding a new table to the database. I could probably easily find the example of the JPQL for this but how would I then automatically generate the entity for this new table so that I could reference it in the rest of my JPA code for updating and deleting from the table I usually reference the entity not the actual table itself.

The Entities I have now I've used eclipe to generate from the tables I created. But after deployment we obviously won't have Eclipse to do this so we need it done automatically every time a table is created.

3 Answers 3

7

Creating and mapping new tables dynamically is not something that JPA supports. It would need creating new Java classes on the fly and dynamically update the mapping metadata. You can do everything you want with JDBC or native queries. But maybe you should explain why you need to create new tables on the fly.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't know that I'll need to be creating tables very often. I think that if you create your schema correctly you should have to create new tables. But if we did I could always just take the server project down rebuild and deploy with the new mappings. I just wanted to know if it was possible because some one asked if I could do it.
To add a new mapping, you'll indeed have to redeploy a new version of your application. You'll probably also have to execute some schema migration script, because there's a good chance this new table impacts the existing ones (new foreign keys, design changes, etc.). Just make sure to test the whole procedure before doing it on your production server to make sure down time is as short as possible.
Good point. Although I think at least for the question that was brought up to me these tables would be independent of the other tables without relationships and keys. But to generate the entities I'd have to redeploy so it still would be a bad idea.
4

There are a couple of ways to create the tables. The most common for production usage is that the DBA (or developer in that role) creates the database schema (tables etc) independently of the application.

This is typical, since the application then models the data in the database, and there's a common wisdom that says the database normally outlives the application.

It is possible to let the JPA provider automatically create the schema. There are even a couple of attributes on the JPA annotations that are specifically for this purpose (e.g. the nullable attribute JoinColumn).

Activating this happens with provider specific properties in persistence.xml. E.g. hibernate.hbm2ddl.auto for Hibernate (as used in JBoss AS) and eclipselink.ddl-generation for EclipseLink (as used in GlassFish).

See http://wiki.eclipse.org/EclipseLink/Examples/JPA/Migration/JBoss for some examples.

Inserting an entity in a table is a completely different thing. From your question is somehow get the feeling that you might not fully understand the difference between those two. It happens via e.g. EntityManager#persist, but I'm not 100% sure if this is what you're asking.

Comments

1

you can configure your hibernate properties or persistence.xml to update db on server startups. it will make table updates automaticly. look next to the hibernateDialect parameter.

<property name="hibernate.hbm2ddl.auto" value="update" /> 

is what you want. i don't think you want to create entity classes on the fly and want tables to be created without redeploy as @JB Nizet says in his answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.