简体   繁体   English

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

[英]How can I get the database dialect from Hibernate for a Custom Type?

so I had written this but it broke with Spring Boot 1.2.0, not sure why it worked (or appeared to) with 1.1.9 所以我已经写了这个,但是它在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;
    }

}

The breaking looks like it might have something to do with attempting to get the nonexistant properties file . 破坏似乎与尝试获取不存在的属性文件有关

How could I get the dialect from Hibernate or Spring? 如何从Hibernate或Spring获得方言?

That very much looks like you were relying on the internal implementation of Spring Boot's packaging. 看起来您似乎完全依赖于Spring Boot包装的内部实现。 it may work, but it's liable to change between versions, which makes your app fragile. 它可能会起作用,但可能会在版本之间进行更改,这会使您的应用程序脆弱。

I can't think of good solution off the top of my head, but a better one is suggested in Retrieve auto-detected hibernate dialect , ie get a reference to the SessionFactory , cast it to SessionFactoryImplementor , then call getDialect on that. 我想不出什么好办法,但是在Retrieve自动检测到的休眠方言中提出了一个更好的建议,即获取对SessionFactory的引用,将其转换为SessionFactoryImplementor ,然后在其上调用getDialect It's still not a good solution since it's also possible that at some future version of Hibernate that cast will cease to work, but that's rather less likely. 这仍然不是一个好的解决方案,因为在Hibernate的某些将来版本中,转换可能会停止工作,但是可能性较小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM