Created
April 30, 2024 09:28
-
-
Save lihongjie0209/aee6025bbf1435b9b11c6a6dea6a1e87 to your computer and use it in GitHub Desktop.
hibernate postgresql 默认值调整
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.invoice.invoiceapi.config; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import io.hypersistence.utils.hibernate.type.json.internal.JsonBinaryJdbcTypeDescriptor; | |
import io.hypersistence.utils.hibernate.type.json.internal.JsonNodeJavaTypeDescriptor; | |
import org.hibernate.boot.model.TypeContributions; | |
import org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy; | |
import org.hibernate.boot.model.naming.Identifier; | |
import org.hibernate.boot.model.naming.PhysicalNamingStrategy; | |
import org.hibernate.engine.jdbc.Size; | |
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; | |
import org.hibernate.service.ServiceRegistry; | |
import org.hibernate.type.BasicTypeRegistry; | |
import org.hibernate.type.SqlTypes; | |
import org.hibernate.type.Type; | |
import org.hibernate.type.descriptor.java.JavaType; | |
import org.hibernate.type.descriptor.jdbc.JdbcType; | |
import org.hibernate.type.descriptor.sql.DdlType; | |
import org.hibernate.type.descriptor.sql.internal.DdlTypeImpl; | |
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry; | |
import org.hibernate.type.internal.BasicTypeImpl; | |
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; | |
import static org.hibernate.type.SqlTypes.VARCHAR; | |
@Configuration | |
@EnableJpaRepositories | |
public class HibernateConfig { | |
@Bean | |
HibernatePropertiesCustomizer hibernatePropertiesCustomizer() { | |
return hibernateProperties -> hibernateProperties.put("hibernate.dialect", new MyPostgreSQLDialet()); | |
} | |
public static class MyPostgreSQLDialet extends org.hibernate.dialect.PostgreSQLDialect { | |
public MyPostgreSQLDialet() { | |
super(); | |
} | |
@Override | |
protected void registerColumnTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) { | |
super.registerColumnTypes(typeContributions, serviceRegistry); | |
DdlTypeRegistry ddlTypeRegistry = typeContributions.getTypeConfiguration().getDdlTypeRegistry(); | |
BasicTypeRegistry basicTypeRegistry = typeContributions | |
.getTypeConfiguration() | |
.getBasicTypeRegistry(); | |
// hibernate auto ddl 覆盖 varchar 类型 为 text 类型 | |
ddlTypeRegistry.addDescriptor(new DdlTypeImpl(VARCHAR, "text", this)); | |
// hibernate auto ddl 注册 jsonb 类型 | |
ddlTypeRegistry.addDescriptor(new OtherDdlType()); | |
// 注册jsonnode java类型 | |
typeContributions.getTypeConfiguration().getJavaTypeRegistry().addDescriptor(JsonNodeJavaTypeDescriptor.INSTANCE); | |
// 注册jsonnode jdbc类型 | |
typeContributions.getTypeConfiguration().getJdbcTypeRegistry().addDescriptor(JsonBinaryJdbcTypeDescriptor.INSTANCE); | |
// 注册jsonnode java类型到jdbc类型的映射 | |
basicTypeRegistry | |
.register( | |
new BasicTypeImpl<>( | |
JsonNodeJavaTypeDescriptor.INSTANCE, | |
JsonBinaryJdbcTypeDescriptor.INSTANCE), JsonNode.class.getCanonicalName()); | |
} | |
public static class OtherDdlType implements DdlType { | |
@Override | |
public int getSqlTypeCode() { | |
return SqlTypes.OTHER; | |
} | |
@Override | |
public String getRawTypeName() { | |
return ""; | |
} | |
@Override | |
public String getTypeName(Long size, Integer precision, Integer scale) { | |
return ""; | |
} | |
@Override | |
public String getTypeName(Size columnSize, Type type, DdlTypeRegistry ddlTypeRegistry) { | |
Class<?> returnedClass = type.getReturnedClass(); | |
if (returnedClass.isAssignableFrom(JsonNode.class)) { | |
return "jsonb"; | |
} | |
return null; | |
} | |
@Override | |
public String getCastTypeName(JdbcType jdbcType, JavaType<?> javaType) { | |
return ""; | |
} | |
@Override | |
public String getCastTypeName(JdbcType jdbcType, JavaType<?> javaType, Long length, Integer precision, Integer scale) { | |
return ""; | |
} | |
} | |
} | |
@Bean | |
public PhysicalNamingStrategy physicalNamingStrategy() { | |
return new CamelCaseToUnderscoresNamingStrategy() { | |
@Override | |
public Identifier toPhysicalColumnName( | |
Identifier logicalName, JdbcEnvironment jdbcEnvironment) { | |
return super.toPhysicalColumnName( | |
new Identifier( | |
logicalName.getText().replace("Entity", ""), | |
logicalName.isQuoted()), | |
jdbcEnvironment); | |
} | |
@Override | |
public Identifier toPhysicalTableName( | |
Identifier logicalName, JdbcEnvironment jdbcEnvironment) { | |
return super.toPhysicalTableName( | |
new Identifier( | |
"t_" + logicalName.getText().replace("Entity", ""), | |
logicalName.isQuoted()), | |
jdbcEnvironment); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment