如何解决从JPA 2.1带注释的实体类自动生成数据模式,而无需数据库连接
我能够将您的Hibernate解决方案与JPA2.1混合使用:
谢谢你的帮助。
/**
*
*/
package com.stackoverflow.common.util.schema;
import java.io.IOException;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.metamodel;
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.Metadataimplementor;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.dialect.MysqL5InnoDBDialect;
/**
*
*/
public class JPA21Hibernate5ExportSchema {
private static final String JDBC_DRIVER = "org.h2.Driver";
private static final String JDBC_URL = "jdbc:h2:mem:export;DB_CLOSE_DELAY=-1";
private static final String JDBC_USERNAME = "sa";
private static final String JDBC_PASSWORD = "";
/**
*
*/
public JPA21Hibernate5ExportSchema() {
}
public static void main(String[] args) {
try {
JPA21Hibernate5ExportSchema hes = new JPA21Hibernate5ExportSchema();
hes.export(args[0], args[1]);
System.exit(0);
} catch (Exception e) {
e.printstacktrace();
System.exit(1);
}
}
public void export(String persistenceUnitName, String sqlFile) throws IOException, ClassNotFoundException {
final BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
final MetadataSources MetadataSources = new MetadataSources(bsr);
final StandardServiceRegistryBuilder srrBuilder = new StandardServiceRegistryBuilder(bsr)
.applySetting(Environment.CONNECTION_PROVIDER, DriverManagerConnectionProviderImpl.class.getName())
.applySetting(Environment.DIALECT, MysqL5InnoDBDialect.class.getName())
.applySetting(Environment.URL, JDBC_URL).applySetting(Environment.USER, JDBC_USERNAME)
.applySetting(Environment.PASS, JDBC_PASSWORD);
// Use the persistence metamodel to retrieve the Entities classes
metamodel metamodel = this.getmetamodel(persistenceUnitName);
for (final ManagedType<?> managedType : metamodel.getManagedTypes()) {
MetadataSources.addAnnotatedClass(managedType.getJavaType());
}
final StandardServiceRegistry ssr = (StandardServiceRegistry) srrBuilder.build();
final MetadataBuilder MetadataBuilder = MetadataSources.getMetadataBuilder(ssr);
final SchemaExport exporter = new SchemaExport((Metadataimplementor) MetadataBuilder.build());
exporter.setoutputFile(sqlFile);
exporter.setDelimiter(";");
exporter.setFormat(true);
exporter.create(false, true);
}
/**
* Retrieve the JPA metamodel from the persistence unit name
*
* @param persistenceUnitName
* @return
*/
private metamodel getmetamodel(String persistenceUnitName) {
final Properties persistenceProperties = new Properties();
persistenceProperties.setProperty(AvailableSettings.JDBC_DRIVER, JDBC_DRIVER);
persistenceProperties.setProperty(AvailableSettings.JDBC_URL, JDBC_URL);
persistenceProperties.setProperty(AvailableSettings.JDBC_USER, "sa");
persistenceProperties.setProperty(AvailableSettings.JDBC_PASSWORD, "");
persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.DIALECT,
MysqL5InnoDBDialect.class.getName());
final EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName,
persistenceProperties);
return emf.getmetamodel();
}
}
解决方法
两年前,我正在使用以下项目进行开发:
- 春天4.0.3发布
- jpa 2.0
- 休眠4.2.7.Final
- Java 1.6.X
这个项目有一个maven任务hibernate3-maven-plugin,它使我们能够生成数据库模式而无需与数据库(MySQL)进行任何连接。
现在,我们通过以下方式升级该项目:
- Java 1.8
- JPA 2.1
- 春天4.2.4.RELEASE
- 冬眠5.0.6.Final
我知道hibernate3-maven-plugin在JPA 2.1和hibernate> 4.3上不起作用。
我找到的所有解决方案都需要连接到数据库。
有谁知道如何离线生成数据库架构?我所拥有的只是一个persistence.xml,其中列出了所有实体类。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。