Reputation: 7990
So I know that if I've an object (lets say Message
object) properly annotated I can just do:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(databaseName);
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
try {
transaction.begin();
Message msg = new Message("Message");
entityManager.persist(msg);
transaction.commit();
}
And the table will be created with whatever value there is in Message
annotation. In this case let's say Message
's object has this annotation for the table name:
@Table(name = "tableMessage")
Therefore, the table tableMessage
will be created and the Message
object will be saved into the table tableMessage
.
My question is, let's say I've a text field where the user will input the name for the table to where he wants to save the Message object (and create the table if it does not exist).
How can I do this? If I knew before hand I could just go to the Message
class and change it manually but I don't know.
PS; I'm using hibernate as my implementation for the JPA.
Upvotes: 0
Views: 608
Reputation: 26077
Creating and mapping new tables dynamically is not supported by JPA
. 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.
for more in fo, go here
Upvotes: 1