2013-03-14 68 views
0

我正在使用JPA從Oracle XMLType列檢索數據。我創建了一個定製:oracle.xdb.XMLType不能轉換爲java.lang.String錯誤

@Override 
public void customize(final ClassDescriptor descriptor) throws Exception { 
    descriptor.removeMappingForAttributeName("content"); 
    DirectToXMLTypeMapping mapping = new DirectToXMLTypeMapping(); 
    mapping.setAttributeName("content"); //name of the atribute on the Entity Bean 
    mapping.setFieldName("CONTENT"); //name of the data base column 
    mapping.getField().setColumnDefinition("XMLTYPE"); 
    descriptor.addMapping(mapping); 
} 

和列在我的實體類是:

@Basic(optional = false) 
@NotNull 
@Lob 
//@Column(name = "CONTENT", columnDefinition="XMLTYPE")  
private String content; 

然而,當我運行我的程序,我得到的錯誤「java.lang.ClassCastException: oracle.xdb.XMLType cannot be cast to java.lang.String at entities.Sqdocument._persistence_set(Sqdocument.java) at org.eclipse.persistence.internal.descriptors.PersistenceObjectAttributeAccessor.setAttributeValueInObject(PersistenceObjectAttributeAccessor.java:46) at org.eclipse.persistence.mappings.DatabaseMapping.setAttributeValueInObject(DatabaseMapping.java:1532) at org.eclipse.persistence.mappings.DatabaseMapping.readFromRowIntoObject(DatabaseMapping.java:1423) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildAttributesIntoObject(ObjectBuilder.java:448) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:803) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:607) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:564) at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:777) at org.eclipse.persistence.queries.ReadObjectQuery.executeObjectLevelReadQuery(ReadObjectQuery.java:462) at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1150) at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:852) at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1109) at org.eclipse.persistence.queries.ReadObjectQuery.execute(ReadObjectQuery.java:421) at org.eclipse.persistence.internal.sessions.AbstractSession.internalExecuteQuery(AbstractSession.java:2946) at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1602) at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1584) at org.eclipse.persistence.internal.indirection.NoIndirectionPolicy.valueFromQuery(NoIndirectionPolicy.java:323) at org.eclipse.persistence.mappings.ForeignReferenceMapping.valueFromRowInternal(ForeignReferenceMapping.java:2135) at org.eclipse.persistence.mappings.OneToOneMapping.valueFromRowInternal(OneToOneMapping.java:1716) at org.eclipse.persistence.mappings.ForeignReferenceMapping.valueFromRow(ForeignReferenceMapping.java:2024) at org.eclipse.persistence.mappings.ForeignReferenceMapping.readFromRowIntoObject(ForeignReferenceMapping.java:1369) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildAttributesIntoObject(ObjectBuilder.java:448) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:803) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildWorkingCopyCloneNormally(ObjectBuilder.java:719) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObjectInUnitOfWork(ObjectBuilder.java:672) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:605) at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:564) at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:777) at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:783) at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:434) at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1150) at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:852) at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1109) at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:393) 」 可能是什麼問題呢?謝謝。

回答

0

Eclipselink正在成功檢索XML類型。問題是XMLType不是字符串的一個實例,所以它不能被自動轉換。

您需要編寫一個Converter來在XMLType和String之間進行轉換。如果要以任何方式更改數據,您還需要編寫轉換器的另一端,該轉換器從字符串轉換爲XMLType。

看看Conversions and Converters寫一個轉換器的幫助。

+0

感謝您的回答。我見過這些轉換器,但不知道應該使用哪一個? – Yangrui

+0

我會推薦使用@Converter元素。查看http://www.eclipse.org/eclipselink/api/2.0/org/eclipse/persistence/mappings/converters/Converter.html獲取API。 –

相關問題