gpt4 book ai didi

java - 如何在运行时从 Hibernate 检索 hibernate.hbm2ddl.auto 值?

转载 作者:行者123 更新时间:2023-12-02 00:13:57 25 4
gpt4 key购买 nike

我正在尝试解决 Web 应用程序中可能存在的配置错误。我想在运行时从 Hibernate 检索 hibernate.hbm2ddl.auto 值,这可能吗? JPA EntityManager 已成功创建。

谢谢

最佳答案

检查源代码后,用于构建SessionFactory的所有配置参数都将存储在其Settings中属性(property) 。

给定一个 EntityManager 实例,您可以通过以下代码获取用于构建它的 SessionFactory:

Session session = entityManager.unwrap(Session.class);
SessionFactoryImpl sessionImpl = (SessionFactoryImpl)session.getSessionFactory();

并从 SessionFactoryImpl 获取 Settings 实例:

Settings setting = sessionImpl.getSettings();

但是,根据以下code如何从配置参数构建此 Settings 实例:

Settings settings = new Settings();
String autoSchemaExport = properties.getProperty( Environment.HBM2DDL_AUTO );
if ( "validate".equals(autoSchemaExport) ) {
settings.setAutoValidateSchema( true );
}
if ( "update".equals(autoSchemaExport) ) {
settings.setAutoUpdateSchema( true );
}
if ( "create".equals(autoSchemaExport) ) {
settings.setAutoCreateSchema( true );
}
if ( "create-drop".equals( autoSchemaExport ) ) {
settings.setAutoCreateSchema( true );
settings.setAutoDropSchema( true );
}

hibernate.hbm2ddl.auto 的实际值不会存储在 Settings 实例中。它仅解析为 autoDropSchemaautoCreateSchemaautoValidateSchema 的不同值。您必须使用这些属性来确定 hibernate 的实际值.hbm2ddl.auto

关于java - 如何在运行时从 Hibernate 检索 hibernate.hbm2ddl.auto 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12238740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com