Persisting Entities to a Database Java
I'm just starting to work with persistence and databases in my use of webapps. I have created several Entity classes and have a database connection set up in Eclipse, but I'm not sure how to generate the tables from those entities. This is all I've managed to come up with so far:
public class Main {
@Resource
static UserTransaction utx;
@PersistenceUnit
static EntityManagerFactory emf;
public static void main(String[] args) throws Exception {
EntityManager em 开发者_运维问答= emf.createEntityManager();
utx.begin();
em.joinTransaction();
User user = new User();
user.setUsername("cvolkernick");
em.persist(user);
utx.commit();
em.flush();
em.close();
emf.close();
}
}
You can create Tables from Entity declarations, using the Dali JPA Tools. As long as your project is configured with a JPA facet, you will have access to these tools. Creating tables from entities is as simple as right-clicking on your project, and then browsing through the context menu, as shown in the below screenshot:
Note that it is preferable to create tables in this manner, or by hand-coding the DDL statements yourself. It is not recommended to have the JPA provider create the tables for you, as you would lose the ability to place the table definitions under version control. They're also suboptimal and not meant to be used in production. Even for small projects, it is not worth the trouble, as the ability of the JPA provider (EclipseLink or Hibernate) to generate fine-tuned DDL statements is quite limited. In any case, if you wish to have the JPA provider do this work for you, details are as follows:
If your JPA provider is EclipseLink:
Your persistence.xml file ought to look like the following:
<?xml version="1.0" encoding="UTF-8"?>
<persistence ...>
<persistence-unit ...>
...
<properties>
<!-- valid values include 'none, 'drop-and-create-tables' and 'create-tables' -->
<property name="eclipselink.ddl-generation" value="create-tables"/>
<!-- valid values include 'both', 'database' and 'sql-script' -->
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
...
</properties>
</persistence-unit>
</persistence>
You can configure these values from Eclipse, by opening the persistence.xml
file with the Persistence XML editor:
Details of the EclipseLink schema generation properties and values can be found in the EclipseLink wiki.
If your JPA provider is Hibernate:
Your persistence.xml file ought to look like the following:
<?xml version="1.0" encoding="UTF-8"?>
<persistence ...>
<persistence-unit ...>
...
<properties>
<!-- valid values include 'validate', 'update', 'create' and 'create-drop' -->
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
...
</properties>
</persistence-unit>
</persistence>
Unlike the EclipseLink values, the ones for Hibernate cannot be configured from the Persistence XML editor of Eclipse. You can find other Hibernate properties in the Hibernate Core documentation.
Also, note that the in the case of both EclipseLink and Hibernate, you might need to specify additional properties like the connection pool configuration and the database dialect, to aid the schema generators.
精彩评论