- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我之前问题的后续问题:Generate an SQL DB creation script with Hibernate 4
目标是让命令行工具能够生成具有给定持久性单元的 SQL 架构的文件(类似于 Hibernate 工具中存在的 hibernatetool-hbm2ddl Ant 任务)。
根据我之前问题的答案,这可以通过 org.hibernate.tool.hbm2ddl.SchemaExport
实现。
我想指定一个 PersistenceUnit
,而不是将所有实体添加到 Configuration
(如上一个答案中所建议的)。
是否可以添加一个持久化单元到 Hibernate Configuration
?
有点像
Properties properties = new Properties();
properties.put( "hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect" );
...
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory( "persistentUnitName", properties );
Configuration configuration = new Configuration();
... missing part ...
SchemaExport schemaExport = new SchemaExport( configuration );
schemaExport.setOutputFile( "schema.sql" );
...
编辑作为示例persistence.xml
评论中的要求。每个类都用@Entity
<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_1_0.xsd"
version="1.0"
>
<persistence-unit
name="doiPersistenceUnit"
transaction-type="JTA"
>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/doi</jta-data-source>
<class>ch.ethz.id.wai.doi.bo.Doi</class>
[...]
<class>ch.ethz.id.wai.doi.bo.DoiPool</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.connection.characterEncoding" value="utf8" />
<property name="hibernate.connection.charSet" value="utf8" />
</properties>
</persistence-unit>
</persistence>
最佳答案
好吧,如果您的类是通过 xml 映射 (hbm
s) 映射的 - 您可以将包含 xml 的 documnet 或 jar 文件直接添加到 Configuration
实例,使用config.addJar(myJarFile)
和 config.add(myXmlFile)
。
但是,如果您希望扫描您的注释类 - 我知道通过 Hibernate 没有这样简单的选项(addPackage
添加元数据,不 类(class))。
您可以实现您自己的扫描逻辑,并使用 config.addAnnotatedClass(myAnnotatedClass)
添加所有带注释的类(或者可能根据您知道包含 ORM 的特定包执行此操作类,因此可能会节省一些时间)。
更新 2
哦,更好的是,您可以通过 getManagedTypes()
迭代持久性单元的 ManagedType
:
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory( unitName, config.getProperties() );
final Set<ManagedType<?>> managedTypes =
entityManagerFactory.getMetamodel().getManagedTypes();
for ( ManagedType<?> managedType : managedTypes ) {
final Class<?> javaType = managedType.getJavaType();
config.addAnnotatedClass( javaType );
}
更新
您可以确定每个 Entity
的 PersistenceUnit
- 无需解析 xml - 通过检查相关的 EntityManagerFactory
:
Class aClass = ... // get the class from your scanning
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory( unitName, config.getProperties() );
ManagedType<?> managedType = null;
try {
managedType = entityManagerFactory.getMetamodel().managedType( aClass );
} catch ( IllegalArgumentException e ) {
// happens when aClass isn't a type managed by the persistence unit
}
if ( managedType != null ) {
config.addAnnotatedClass( aClass );
}
确保为每个持久化单元使用不同的 Configuration
实例。否则,带注解的类将堆积起来,DDL 也是如此。
我试过了,效果很好——为两个不同的持久化单元打印了两个不同的 DDL。
关于java - Hibernate SchemaExport 和持久化单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161090/
我正在使用 Arquillian、JBoss、JPA/Hibernate、H2 DB 和 Maven 运行测试。在我的测试 persistence.xml 文件中,我有: 现在我有一个 User
我将 NHibernate 用于我的应用程序的 DAL,尤其是 NHibernate 的 SchemaExport 函数,用于在执行单元测试之前删除/重新创建我的数据库模式。我遇到的问题是,当我运行单
我有一个像这样的遗留数据库结构 table t1 ( c0 bigint, // pk c10 bigint, // deleted flag ) table t2 ( c0 bigin
我之前问题的后续问题:Generate an SQL DB creation script with Hibernate 4 目标是让命令行工具能够生成具有给定持久性单元的 SQL 架构的文件(类似于
我正在学习 Nhibernate 并正在制作一个测试项目。我想根据实体生成表格。在测试项目中,我使用的是 sqlite 并且可以看到输出:“drop table if exists Player”但之
我正在使用 schemaExport 为我的自动测试创建一个内存数据库。我有几个 (5) 类将它们的 HiLo 标识映射到同一个数据库表,每个类使用一列。 这给了我一个表 hibernate_uniq
我有一个这样的映射: HasMany(x => x.Orders).KeyColumn("CustomerID"); 这导致schemaexport生成这样的约束: alter table [Cust
我很不确定为什么我的实体类没有创建我所需的表架构 @Entity @Table(name = "User") public class User implements Serializable {
出现此错误之前的最后一件事“我记得”正在更新为openjdk8 Error 2015-01-12 14:19:44,751 [localhost-startStop-1] ERROR hbm2ddl.
我需要帮助解决我的应用程序中的问题。 我已经在 Visual Studio 中创建了一个项目,并且我已经安装并配置了 Fluent NHibernate。我能够正确配置所有内容,并且能够连接到我的数据
按照本教程学习 NHibernate Your first NHibernate based application我到了你打电话的地步 new SchemaExport(cfg).Execute(t
我想使用 Hibernate 和 MySql 创建嵌入对象的列表。 但是我咳嗽了一堆错误: Hibernate: alter table USERS drop foreign key FK_qymdw
引自persistence.xml: ... 这是我在日志输出中看到的: Sep 30,
尝试在 NHibernate 2.1.2.4000 中使用 IInterceptor,我得到了以下测试代码: public class TestingNHibernateInterceptors {
当数据对象模型发生变化(向类添加新属性)时,流畅的自动映射是否有一种方法可以改变模式,目前它只会删除并重新创建数据库模式,这将丢失数据库中的所有数据.它可以使用 Alter table 而不是 dro
我很想知道更多关于这段代码是如何执行的以及执行时的预期结果。 /// /// Sets up NHibernate, and adds an ISessionFact
当我将SchemaExport与SQL Server 2005一起使用时,它会生成唯一的键名,例如: UQ__员 worker 数__03317E3D 如何生成类似UQ__Employees__Nam
请查看我的要求:使用 SchemaExport 导出应用了 BeanValidation 约束的数据库架构(例如,@Length(32) 将创建数据库约束:column(32))。 在 Hiberna
我想使用 DDD在一个新项目中首先对我的类进行建模,然后根据类库生成数据库模式。我的计划是使用 NHibernate hbm2ddl 工具 SchemaExport 来做到这一点。 问题是我无法让 S
我们有一个 Java Spring 项目,使用 JPA 和 Hibernate 4 进行 ORM。我们专门使用 Java 配置,因此我们没有任何 hibernate.properties 或 pers
我是一名优秀的程序员,十分优秀!