2

我正在尝试在我的 Java EE 6 应用程序中运行 SchemaUpdate。在以前的 Hibernate 版本中,我设法创建Ejb3Configuration并运行 SchemaUpdate。差不多是这样的:

Ejb3Configuration cfg = new Ejb3Configuration();
cfg.configure(PERSISTENCE_UNIT_NAME, null);
cfg.setProperty("hibernate.hbm2ddl.auto", "update");
cfg.setProperty("hibernate.dialect", dialect.getClass().getName());
Collection<String> entityClassNames = getEntityClassNames(PERSISTENCE_UNIT_NAME);
for (String className : entityClassNames)
{
    cfg.addAnnotatedClass(Class.forName(className));
}

EntityManagerFactory factory =  Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

new SchemaUpdate(cfg.getHibernateConfiguration()).execute(true, true);

显然,该类Ejb3Config已被弃用且不受支持。那么使用 hibernate 4 进行 SchemaUpdate 的正确方法是什么?

4

1 回答 1

1

You say you're using EE 6. Are you therefore using a persistence.xml and all the standard JPA stuff? If not, then that's a mistake, and the first step is to go back and fix that. Using a persistence.xml, you can specify schema update on startup with a property:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">

    <persistence-unit name="...">
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>

</persistence>

Would that be suitable? Or do you have a partcular reason for doing this manually?

于 2012-12-18T12:32:30.750 回答