public class TrimStringType extends org.hibernate.type.AbstractSingleColumnStandardBasicType<String> implements org.hibernate.type.DiscriminatorType<String>
VARCHAR and String
情景:当数据库字段为 char(10) 类型时,使用jpa插入数据长度不够 10 时,数据库会自动追加空格;
当再次使用jpa查询数据时,返回到前台的该char字段(在java中使用String接收)就会多出一些空格,需要去除。
一般除非字段长度总是固定的,数据库才可以考虑使用char类型,否则请使用varchar类型,这样就可以避免这个问题。
如何使用:在java实体类对应的字段方法或属性添加 @Type(type = TrimStringType.TYPE)
或者 @Type(type = "com.mwj.mwjwork.framework.jpa.TrimStringType")
代码参考自 org.hibernate.type.StringType.java| 限定符和类型 | 字段和说明 |
|---|---|
static TrimStringType |
INSTANCE |
| 构造器和说明 |
|---|
TrimStringType() |
| 限定符和类型 | 方法和说明 |
|---|---|
String |
getName() |
String |
objectToSQLString(String value,
org.hibernate.dialect.Dialect dialect) |
protected boolean |
registerUnderJavaType() |
String |
stringToObject(String xml) |
String |
toString(String value) |
assemble, beforeAssemble, canDoExtraction, canDoSetting, compare, deepCopy, deepCopy, defaultSizes, dictatedSizes, disassemble, extract, extract, fromString, fromStringValue, get, getColumnSpan, getDefaultSize, getDictatedSize, getHashCode, getHashCode, getJavaTypeDescriptor, getMutabilityPlan, getRegistrationKeys, getReplacement, getReturnedClass, getSemiResolvedType, getSqlTypeDescriptor, hydrate, isAnyType, isAssociationType, isCollectionType, isComponentType, isDirty, isDirty, isDirty, isEntityType, isEqual, isEqual, isModified, isMutable, isSame, isXMLElement, nullSafeGet, nullSafeGet, nullSafeGet, nullSafeGet, nullSafeSet, nullSafeSet, nullSafeSet, nullSafeSet, remapSqlTypeDescriptor, replace, replace, resolve, semiResolve, set, setJavaTypeDescriptor, setSqlTypeDescriptor, sqlTypes, toColumnNullness, toLoggableStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitassemble, beforeAssemble, compare, deepCopy, defaultSizes, dictatedSizes, disassemble, getColumnSpan, getHashCode, getHashCode, getReturnedClass, getSemiResolvedType, hydrate, isAnyType, isAssociationType, isCollectionType, isComponentType, isDirty, isDirty, isEntityType, isEqual, isEqual, isModified, isMutable, isSame, nullSafeGet, nullSafeGet, nullSafeSet, nullSafeSet, replace, replace, resolve, resolve, semiResolve, sqlTypes, toColumnNullness, toLoggableStringpublic static final TrimStringType INSTANCE
public String getName()
getName 在接口中 org.hibernate.type.Typeprotected boolean registerUnderJavaType()
registerUnderJavaType 在类中 org.hibernate.type.AbstractStandardBasicType<String>public String objectToSQLString(String value, org.hibernate.dialect.Dialect dialect) throws Exception
Copyright © 2020. All rights reserved.