我有我的实体类:
@Entity
@Table(name = "TABLE")
public class User {
@Id
@Column
private UUID id;
我正在使用EclipseLink。当我尝试执行以下代码时:
em.createQuery("select t from Table").getResultList();
我有例外:
引起:org.eclipse.persistence.exceptions.ConversionException: 异常描述:对象 [9ABCC78A-4C58-41D2-A672-324F6B095260],班级[类 java.lang.String],无法转换为[class [B]。在 org.eclipse.persistence.exceptions.ConversionException.couldNotBeConverted(ConversionException.java:78) 〜[?:?] at org.eclipse.persistence.internal.helper.Helper.buildBytesFromHexString(Helper.java:266) 〜[?:?] at org.eclipse.persistence.internal.helper.ConversionManager.convertObjectToByteArray(ConversionManager.java:326) 〜[?:?] at org.eclipse.persistence.internal.helper.ConversionManager.convertObject(ConversionManager.java:138) 〜[?:?] at org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform.convertObject(DatasourcePlatform.java:179) 〜[?:?] at org.eclipse.persistence.internal.descriptors.ObjectBuilder.extractPrimaryKeyFromRow(ObjectBuilder.java:3104) 〜[?:?] at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:705) 〜[?:?] at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:689) 〜[?:?] at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:805) 〜[?:?] at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:962) 〜[?:?] at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:573) 〜[?:?] at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1175) 〜[?:?] at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:904) 〜[?:?] at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1134) 〜[?:?] at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:460) 〜[?:?] at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1222) 〜[?:?] at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896) 〜[?:?] at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1857) 〜[?:?] at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839) 〜[?:?] at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804) 〜[?:?] at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258) 〜[?:?] at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:473) 〜[?:?] ... 61更多
我尝试在orm.xml中添加AttributeConverter<UUID, String>
,但我得到了相同的异常
<converter class="com.example.UuidConverter" auto-appy="true"/>
转换器中的'技巧'适用于PostgreSQL(我需要AttributeConverter<UUID, UUID>
,因为JDBC驱动程序返回UUID),但在MSSQL上,我找不到工作解决方案。
如何处理这种情况,以便我的实体也支持MSSQL?
答案 0 :(得分:0)
有一个窍门,希望对其他有相同问题的人有帮助
步骤1创建一个类转换器
import org.eclipse.persistence.internal.helper.DatabaseField;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.DirectCollectionMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.sessions.Session;
import java.util.UUID;
public class MyUUIDConverter implements Converter {
@Override
public Object convertObjectValueToDataValue(Object o, Session session) {
return o;
}
@Override
public UUID convertDataValueToObjectValue(Object o, Session session) {
return (UUID) o;
}
@Override
public boolean isMutable() {
return true;
}
@Override
public void initialize(DatabaseMapping databaseMapping, Session session) {
final DatabaseField field;
if (databaseMapping instanceof DirectCollectionMapping) {
// handle @ElementCollection...
field = ((DirectCollectionMapping) databaseMapping).getDirectField();
} else {
field = databaseMapping.getField();
}
field.setSqlType(java.sql.Types.OTHER);
field.setTypeName("java.util.UUID");
field.setColumnDefinition("your column name in database");
}
}
第2步,使用注释转换器实现您最近创建的类</ p>
@Entity
@Table(name = "TABLE")
@Converter(name = "myUuidConverter", converterClass = MyUUIDConverter.class)
public class User {
第3步在您的媒体资源中使用注释转换
@Convert("myUuidConverter")
private UUID id;