Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.hibernate.MappingException:没有JDBC类型的方言映射:2000_Java_Spring_Hibernate_Hibernate Mapping - Fatal编程技术网

Java org.hibernate.MappingException:没有JDBC类型的方言映射:2000

Java org.hibernate.MappingException:没有JDBC类型的方言映射:2000,java,spring,hibernate,hibernate-mapping,Java,Spring,Hibernate,Hibernate Mapping,我正在尝试将表的一列映射到JSONUserType上 我有一个模型: @Entity @TypeDefs({ @TypeDef(name = "JSONUserType", typeClass = JSONUserType.class) }) @Table( name = "my_table") public class MyClass { .... ..... @Column(name = "permissions") @Type(type = "JSONUserType", paramete

我正在尝试将表的一列映射到JSONUserType上

我有一个模型:

@Entity
@TypeDefs({ @TypeDef(name = "JSONUserType", typeClass = JSONUserType.class) })
@Table( name = "my_table")
public class MyClass {
....
.....
@Column(name = "permissions")
@Type(type = "JSONUserType", parameters = { @Parameter(name = "classType", 
value = "java.util.HashMap") })
private Map<String, Boolean> permissions;
......
......
Getter and setter
.....
.....
}
@实体
@TypeDefs({@TypeDef(name=“JSONUserType”,typeClass=JSONUserType.class)})
@表(name=“my_表”)
公共类MyClass{
....
.....
@列(name=“权限”)
@Type(Type=“JSONUserType”,parameters={@Parameter(name=“classType”,
value=“java.util.HashMap”)})
私有地图权限;
......
......
接二连三
.....
.....
}
我已经实现了JSONUserType:

package it......model;

import java.io.IOException;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Objects;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Hibernate {@link UserType} implementation to handle JSON objects
 *
 * @see https://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/usertype/ UserType.html
 */
public class JSONUserType implements UserType, ParameterizedType, Serializable {

    private static final long serialVersionUID = 1L;

    private static final ObjectMapper MAPPER = new ObjectMapper()
    // do NOT serialize null properties
    .setSerializationInclusion(Include.NON_NULL)
    // serialize dates using ISO format instead of epoch time
    .setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));

    private static final String CLASS_TYPE = "classType";
    private static final String COLLECTION_CLASS_TYPE = "collectionClassType";

    private static final int[] SQL_TYPES = new int[] { Types.JAVA_OBJECT };

    private Class<?> classType;
    private Class<?> collectionClassType;
    private int sqlType = Types.LONGVARCHAR; // before any guessing

    @Override
    public void setParameterValues(Properties params) {

        // Retrieve the class name from params
        String classTypeName = params.getProperty(CLASS_TYPE);
        try {
            // Retrieve the classType by reflection
            classType = ReflectHelper.classForName(classTypeName, this.getClass());
            // If class is a collection, we may have to retrieve the classType of its elements
            if (Collection.class.isAssignableFrom(classType)) {
                // Retrieve the elements class name from params
                String collectionClassTypeName = params.getProperty(COLLECTION_CLASS_TYPE);
                if (collectionClassTypeName != null) {
                    try {
                        // Retrieve the elements classType by reflection
                        collectionClassType = ReflectHelper.classForName(collectionClassTypeName, this.getClass());
                    }
                    catch (ClassNotFoundException e) {
                        throw new HibernateException("collectionClassType not found : " + collectionClassTypeName, e);
                    }
                }
            }
        }
        catch (ClassNotFoundException e) {
            throw new HibernateException("classType not found : " + classTypeName, e);
        }

        sqlType = Types.OTHER;

    }

    @Override
    public Object assemble(Serializable cached, Object owner) throws HibernateException {

        return deepCopy(cached);
    }

    @Override
    public Object deepCopy(Object value) throws HibernateException {

        Object copy = null;
        if (value != null) {

            try {
                return MAPPER.readValue(MAPPER.writeValueAsString(value), classType);
            }
            catch (IOException e) {
                throw new HibernateException("unable to deep copy object", e);
            }
        }
        return copy;
    }

    @Override
    public Serializable disassemble(Object value) throws HibernateException {

        try {
            return MAPPER.writeValueAsString(value);
        }
        catch (JsonProcessingException e) {
            throw new HibernateException("unable to disassemble object", e);
        }
    }

    @Override
    public boolean equals(Object x, Object y) throws HibernateException {

        if (x == null && y == null) return true;
        if (x == null) return false;
        if (y == null) return false;

        try {
            return MAPPER.writeValueAsString(x).equals(MAPPER.writeValueAsString(y));
        }
        catch (JsonProcessingException e) {
        }
        return false;
    }

    @Override
    public int hashCode(Object x) throws HibernateException {

        return Objects.hash(x);
    }

    @Override
    public boolean isMutable() {

        return true;
    }

    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {

        if (rs.getString(names[0]) == null) return null;

        Object obj = null;
        try {
            // If we have defined a class type for the collection elements, cast JSON string to Collection<collectionClassType>
            if (collectionClassType != null) {
                obj = MAPPER.readValue(rs.getString(names[0]), MAPPER.getTypeFactory().constructCollectionType((Class<? extends Collection>) classType, collectionClassType));
            }
            // Else simply cast JSON string to classType
            else {
                obj = MAPPER.readValue(rs.getString(names[0]), classType);
            }
        }
        catch (IOException e) {
            throw new HibernateException("unable to read object from result set", e);
        }
        return obj;
    }

    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {

        if (value == null) {
            st.setNull(index, sqlType);
        } else {
            try {
                st.setObject(index, MAPPER.writeValueAsString(value), sqlType);
            }
            catch (JsonProcessingException e) {
                throw new HibernateException("unable to set object to result set", e);
            }
        }
    }

    @Override
    public Object replace(Object original, Object target, Object owner) throws HibernateException {

        return deepCopy(original);
    }

    @Override
    public Class<?> returnedClass() {

        return classType;
    }

    @Override
    public int[] sqlTypes() {

        return SQL_TYPES;
    }
}
package it……模型;
导入java.io.IOException;
导入java.io.Serializable;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Types;
导入java.text.simpleDataFormat;
导入java.util.Collection;
导入java.util.Objects;
导入java.util.Properties;
导入org.hibernate.hibernateeexception;
导入org.hibernate.engine.spi.SharedSessionCompactmentor;
导入org.hibernate.internal.util.ReflectHelper;
导入org.hibernate.usertype.ParameterizedType;
导入org.hibernate.usertype.usertype;
导入com.fasterxml.jackson.annotation.JsonInclude.Include;
导入com.fasterxml.jackson.core.JsonProcessingException;
导入com.fasterxml.jackson.databind.ObjectMapper;
/**
*Hibernate{@link UserType}实现来处理JSON对象
*
*@见https://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/usertype/ UserType.html
*/
公共类JSONUserType实现UserType、ParameterizedType、Serializable{
私有静态最终长serialVersionUID=1L;
私有静态最终ObjectMapper MAPPER=新ObjectMapper()
//不要序列化空属性
.setSerializationInclusion(包括.NON_NULL)
//使用ISO格式而不是历元时间序列化日期
.setDateFormat(新的SimpleDateFormat(“yyyy-MM-dd'T'HH:MM:ss'Z'));
私有静态最终字符串CLASS_TYPE=“classType”;
私有静态最终字符串集合\u CLASS\u TYPE=“collectionClassType”;
私有静态final int[]SQL_TYPES=new int[]{TYPES.JAVA_OBJECT};
私有类类型;
私有类collectionClassType;
private int sqlType=Types.LONGVARCHAR;//在任何猜测之前
@凌驾
公共void setParameterValues(属性参数){
//从参数中检索类名
字符串classTypeName=params.getProperty(类类型);
试一试{
//通过反射检索类类型
classType=ReflectHelper.classForName(classTypeName,this.getClass());
//如果类是一个集合,我们可能必须检索其元素的类类型
if(Collection.class.isAssignableFrom(classType)){
//从参数中检索元素类名
字符串collectionClassTypeName=params.getProperty(集合类类型);
if(collectionClassTypeName!=null){
试一试{
//通过反射检索元素类类型
collectionClassType=ReflectHelper.classForName(collectionClassTypeName,this.getClass());
}
catch(classnotfounde异常){
抛出新的HibernateException(“未找到collectionClassType:”+collectionClassTypeName,e);
}
}
}
}
catch(classnotfounde异常){
抛出新的HibernateException(“未找到类类型:“+classTypeName,e”);
}
sqlType=Types.OTHER;
}
@凌驾
公共对象汇编(可序列化缓存,对象所有者)引发HibernateException{
返回deepCopy(缓存);
}
@凌驾
公共对象deepCopy(对象值)引发HibernateException{
对象复制=空;
if(值!=null){
试一试{
返回MAPPER.readValue(MAPPER.writeValueAsString(value),类类型);
}
捕获(IOE异常){
抛出新的HibernateException(“无法深度复制对象”,e);
}
}
返回副本;
}
@凌驾
公共可序列化反汇编(对象值)引发HibernateException{
试一试{
返回MAPPER.writeValueAsString(值);
}
捕获(JsonProcessingException e){
抛出新的HibernateException(“无法反汇编对象”,e);
}
}
@凌驾
公共布尔等于(对象x,对象y)抛出HibernateException{
如果(x==null&&y==null)返回true;
如果(x==null)返回false;
如果(y==null)返回false;
试一试{
返回MAPPER.writeValueAsString(x).equals(MAPPER.writeValueAsString(y));
}
捕获(JsonProcessingException e){
}
返回false;
}
@凌驾
public int hashCode(对象x)抛出HibernateeException{
返回Objects.hash(x);
}
@凌驾
公共布尔可交换(){
返回true;
}
@凌驾
公共对象nullSafeGet(结果集rs、字符串[]名称、SharedSessionContractCompleteMentor会话、对象所有者)引发HibernateeException、SQLException{
if(rs.getString(名称[0])==null)返回null;
objectobj=null;
试一试{
//如果我们为集合元素定义了类类型,则将JSON字符串强制转换为集合
if(collectionClassType!=null){

obj=MAPPER.readValue(rs.getString(名称[0]),MAPPER.getTypeFactory().ConstructionCollectionType((类您需要扩展
mysql5innodbdialent
方言并提供映射。它与将以下构造函数添加到自定义类一样简单(前提是您已经配置了usertype和其他构造函数)

public MyCustomDialect() {
    super();
    this.registerColumnType(Types.JAVA_OBJECT, "json"); // JAVA_OBJECT = 2000
}

我创建了扩展方言的自定义类(类型是java.sql.Types?),但我得到了一个错误:通过JDBC语句执行DDL时出错……原因是:com
Caused by: org.hibernate.MappingException: No Dialect mapping for JDBC type: 2000
at org.hibernate.dialect.TypeNames.get(TypeNames.java:70)
at org.hibernate.dialect.TypeNames.get(TypeNames.java:101)
at org.hibernate.dialect.Dialect.getTypeName(Dialect.java:346)
at org.hibernate.mapping.Column.getSqlType(Column.java:231)
at org.hibernate.mapping.Table.sqlAlterStrings(Table.java:473)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.migrateTable(AbstractSchemaMigrator.java:295)
at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:75)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:203)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:110)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:183)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)
public MyCustomDialect() {
    super();
    this.registerColumnType(Types.JAVA_OBJECT, "json"); // JAVA_OBJECT = 2000
}