问题
I've created my model classes with JPA 2.0 annotations. At runtime, I will use EclipseLink 2.2.0, but I've been careful to use only pure JPA annotations in my model classes.
Now, how do I generate the schema DDL for the database ?
I want to use the EclipseLink API to generate the DDL from the classes, but not at runtime. Instead, I want a Java class to run on the command line and which outputs the DDL. What this guy did for Hibernate, I want for EclipseLink.
I'd also settle for an Ant task or some plugin for Maven.
Also, I chose to name my file jpa.xml instead of persistence.xml; bonus points if your solution accounts for this as well. Otherwise, I'll just rename my file persistence.xml.
回答1:
You can find your answers in the EclipseLink Documentation, more specifically in the section called Using EclipseLink JPA Extensions for Schema Generation.
There you will find that there is a property called eclipselink.ddl-generation with possible values like NONE | CREATE_ONLY | DROP_AND_CREATE.
There you will find an additional property named eclipselink.ddl-generation.output-mode, whose documentation is in this same page.
It will provide you control on whether you want just to generate a script or actually execute the DDL against the database.
I hope that helps!
回答2:
I know this is a little late but I have been struggling with the same problem. What I found was after doing the configurations like setting the "eclipselink.ddl-generation", then I hooked the following code into our build I got ddl file to generate. EntityManagerHolder class would have to be defined in your context.xml. It is a bit hackish but works.
public class EntityManagerHolder {
@PersistenceContext(type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
protected EntityManager getEntityManager() {
return entityManager;
}
}
public class SQLGeneration {
public static void generateSQLFiles() {
ClassPathXmlApplicationContext application_context = new ClassPathXmlApplicationContext(
"META-INF" + File.separator + "spring" + File.separator
+ "context.xml");
EntityManagerHolder entity_manager_holder = (EntityManagerHolder) application_context
.getBean("entityManagerHolder");
entity_manager_holder.getEntityManager().getEntityManagerFactory()
.createEntityManager();
}
}
来源:https://stackoverflow.com/questions/5992031/generate-ddl-for-jpa-2-0-with-eclipselink