Remove custom Postgres Dialect. Replace with DatabaseAwareLobType

This commit is contained in:
Tim Donohue
2022-03-01 17:06:19 -06:00
parent 57b19fa71a
commit 62c0e28f54
6 changed files with 63 additions and 73 deletions

View File

@@ -93,7 +93,7 @@ public class ResourcePolicy implements ReloadableEntity<Integer> {
private String rptype; private String rptype;
@Lob @Lob
@Type(type = "org.hibernate.type.MaterializedClobType") @Type(type = "org.dspace.storage.rdbms.hibernate.DatabaseAwareLobType")
@Column(name = "rpdescription") @Column(name = "rpdescription")
private String rpdescription; private String rpdescription;

View File

@@ -59,7 +59,7 @@ public class MetadataValue implements ReloadableEntity<Integer> {
* The value of the field * The value of the field
*/ */
@Lob @Lob
@Type(type = "org.hibernate.type.MaterializedClobType") @Type(type = "org.dspace.storage.rdbms.hibernate.DatabaseAwareLobType")
@Column(name = "text_value") @Column(name = "text_value")
private String value; private String value;

View File

@@ -0,0 +1,57 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.storage.rdbms.hibernate;
import org.apache.commons.lang.StringUtils;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.descriptor.java.StringTypeDescriptor;
import org.hibernate.type.descriptor.sql.ClobTypeDescriptor;
import org.hibernate.type.descriptor.sql.LongVarcharTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
/**
* A Hibernate @Type used to properly support the CLOB in both Postgres and Oracle.
* PostgreSQL doesn't have a CLOB type, instead it's a TEXT field.
* Normally, you'd use org.hibernate.type.TextType to support TEXT, but that won't work for Oracle.
* https://github.com/hibernate/hibernate-orm/blob/5.6/hibernate-core/src/main/java/org/hibernate/type/TextType.java
*
* This Type checks if we are using PostgreSQL.
* If so, it configures Hibernate to map CLOB to LongVarChar (same as org.hibernate.type.TextType)
* If not, it uses default CLOB (which works for other databases).
*/
public class DatabaseAwareLobType extends AbstractSingleColumnStandardBasicType<String> {
public static final DatabaseAwareLobType INSTANCE = new DatabaseAwareLobType();
public DatabaseAwareLobType() {
super( getDbDescriptor(), StringTypeDescriptor.INSTANCE );
}
public static SqlTypeDescriptor getDbDescriptor() {
if ( isPostgres() ) {
return LongVarcharTypeDescriptor.INSTANCE;
} else {
return ClobTypeDescriptor.DEFAULT;
}
}
private static boolean isPostgres() {
ConfigurationService configurationService = DSpaceServicesFactory.getInstance().getConfigurationService();
String dbDialect = configurationService.getProperty("db.dialect");
return StringUtils.containsIgnoreCase(dbDialect, "PostgreSQL");
}
@Override
public String getName() {
return "database_aware_lob";
}
}

View File

@@ -1,67 +0,0 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.storage.rdbms.hibernate.postgres;
import java.sql.Types;
import org.hibernate.dialect.PostgreSQL82Dialect;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.PostgresUUIDType;
import org.hibernate.type.descriptor.sql.LongVarcharTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
/**
* UUID's are not supported by default in hibernate due to differences in the database in order to fix this a custom
* sql dialect is needed.
* Source: https://forum.hibernate.org/viewtopic.php?f=1&amp;t=1014157
*
* @author kevinvandevelde at atmire.com
*/
public class DSpacePostgreSQL82Dialect extends PostgreSQL82Dialect {
@Override
public void contributeTypes(final org.hibernate.boot.model.TypeContributions typeContributions,
final ServiceRegistry serviceRegistry) {
super.contributeTypes(typeContributions, serviceRegistry);
typeContributions.contributeType(new InternalPostgresUUIDType());
}
@Override
protected void registerHibernateType(int code, String name) {
super.registerHibernateType(code, name);
}
protected static class InternalPostgresUUIDType extends PostgresUUIDType {
@Override
protected boolean registerUnderJavaType() {
return true;
}
}
/**
* Override is needed to properly support the CLOB on metadatavalue in Postgres and Oracle.
*
* @param sqlCode {@linkplain java.sql.Types JDBC type-code} for the column mapped by this type.
* @return Descriptor for the SQL/JDBC side of a value mapping.
*/
@Override
public SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) {
SqlTypeDescriptor descriptor;
switch (sqlCode) {
case Types.CLOB: {
descriptor = LongVarcharTypeDescriptor.INSTANCE;
break;
}
default: {
descriptor = super.getSqlTypeDescriptorOverride(sqlCode);
break;
}
}
return descriptor;
}
}

View File

@@ -80,9 +80,9 @@ db.url = jdbc:postgresql://localhost:5432/dspace
db.driver = org.postgresql.Driver db.driver = org.postgresql.Driver
# Database Dialect (for Hibernate) # Database Dialect (for Hibernate)
# * For Postgres: org.dspace.storage.rdbms.hibernate.postgres.DSpacePostgreSQL82Dialect # * For Postgres: org.hibernate.dialect.PostgreSQL94Dialect
# * For Oracle: org.hibernate.dialect.Oracle10gDialect # * For Oracle: org.hibernate.dialect.Oracle10gDialect
db.dialect = org.dspace.storage.rdbms.hibernate.postgres.DSpacePostgreSQL82Dialect db.dialect = org.hibernate.dialect.PostgreSQL94Dialect
# Database username and password # Database username and password
db.username = dspace db.username = dspace

View File

@@ -80,9 +80,9 @@ db.url = jdbc:postgresql://localhost:5432/dspace
db.driver = org.postgresql.Driver db.driver = org.postgresql.Driver
# Database Dialect (for Hibernate) # Database Dialect (for Hibernate)
# * For Postgres: org.dspace.storage.rdbms.hibernate.postgres.DSpacePostgreSQL82Dialect # * For Postgres: org.hibernate.dialect.PostgreSQL94Dialect
# * For Oracle: org.hibernate.dialect.Oracle10gDialect # * For Oracle: org.hibernate.dialect.Oracle10gDialect
db.dialect = org.dspace.storage.rdbms.hibernate.postgres.DSpacePostgreSQL82Dialect db.dialect = org.hibernate.dialect.PostgreSQL94Dialect
# Database username and password # Database username and password
db.username = dspace db.username = dspace