gpt4 book ai didi

java - 如何从 Hibernate 获取自定义类型的数据库方言?

转载 作者:行者123 更新时间:2023-12-04 18:05:25 36 4
gpt4 key购买 nike

所以我写了这篇文章,但它与 Spring Boot 1.2.0 不兼容,不确定为什么它可以(或看起来)与 1.1.9 一起工作

package com.xenoterracide.rpf.infrastructure.db;

import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.PostgresUUIDType;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.UUIDTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Properties;

public class UUIDCustomType extends AbstractSingleColumnStandardBasicType {
public static final String UUID = "uuid-custom";
private static final Logger log = LoggerFactory.getLogger( UUIDCustomType.class );

private static final long serialVersionUID = 902830399800029445L;

private static final SqlTypeDescriptor SQL_DESCRIPTOR;
private static final JavaTypeDescriptor TYPE_DESCRIPTOR;

static {
Properties properties = new Properties();
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
properties.load( loader.getResourceAsStream( "database.properties" ) );
}
catch ( IOException e ) {
throw new RuntimeException( "Could not load properties!", e );
}

String dialect = properties.getProperty( "dialect" );
SQL_DESCRIPTOR
= dialect.equals( "org.hibernate.dialect.PostgreSQLDialect" )
? PostgresUUIDType.PostgresUUIDSqlTypeDescriptor.INSTANCE
: VarcharTypeDescriptor.INSTANCE;

log.debug( "dialect {}", dialect );

TYPE_DESCRIPTOR = UUIDTypeDescriptor.INSTANCE;
}

public UUIDCustomType() {
super(SQL_DESCRIPTOR, TYPE_DESCRIPTOR);
}

@Override
public String getName() {
return UUID;
}

}

looks like it might have something to do with attempting to get the nonexistant properties file .

如何从 Hibernate 或 Spring 获取方言?

最佳答案

这很像您依赖于 Spring Boot 包装的内部实现。它可能会工作,但它可能会在版本之间发生变化,这会使您的应用程序变得脆弱。

我想不出好的解决方案,但在 Retrieve auto-detected hibernate dialect 中提出了一个更好的解决方案,即获取对 SessionFactory 的引用,将其转换为 SessionFactoryImplementor,然后对其调用 getDialect。这仍然不是一个好的解决方案,因为在 Hibernate 的某些 future 版本中,强制转换也有可能停止工作,但这种可能性很小。

关于java - 如何从 Hibernate 获取自定义类型的数据库方言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27751052/

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