Java Hibernate JPA+;Sybase图像数据类型
Hibernate JPA数据类型blob不能与Sybase image数据类型一起使用。下面是我正在使用的数据类型的示例。有人能告诉我如何将文件内容映射到Sybase图像数据类型吗 示例代码Java Hibernate JPA+;Sybase图像数据类型,java,hibernate,jpa,sybase,Java,Hibernate,Jpa,Sybase,Hibernate JPA数据类型blob不能与Sybase image数据类型一起使用。下面是我正在使用的数据类型的示例。有人能告诉我如何将文件内容映射到Sybase图像数据类型吗 示例代码 @Column(length=100000) private byte[] fileContent; 例外情况 原因:org.hibernate.HibernateException:DEV_eprs.dbo.pr_file_上载列文件内容时列类型错误。找到:映像,应为:varbinary(100000
@Column(length=100000)
private byte[] fileContent;
例外情况
原因:org.hibernate.HibernateException:DEV_eprs.dbo.pr_file_上载列文件内容时列类型错误。找到:映像,应为:varbinary(100000)
使用@Lob时,检索数据时收到以下异常
java.lang.UnsupportedOperationException
不支持com.sybase.jdbc3.jdbc.SybResultSet.getBlob(字符串)方法,因此不应调用该方法。我通过创建自定义数据类型解决了此问题,下面是解决方案。希望这能帮助别人 //你的实体
@Type(type = "org.company.project.entities.types.BlobType")
private byte[] fileContent;
public byte[] getFileContent() {
return fileContent;
}
public void setFileContent(byte[] fileContent) {
this.fileContent = fileContent;
}
//自定义数据类型
public class BlobType implements UserType {
public int[] sqlTypes() {
return new int[] { Types.BLOB };
}
public Class returnedClass() {
return Blob.class;
}
public Object nullSafeGet(ResultSet aResultSet, String[] aColName, Object aObject)
throws HibernateException, SQLException {
return getBlobFromBinaryStream(aResultSet, aColName[0]);
}
private byte[] getBlobFromBinaryStream(ResultSet aResultSet, String aColName)
throws SQLException {
byte[] theBuff = new byte[2 * 1024];
InputStream theInStream = aResultSet.getBinaryStream(aColName);
ByteArrayOutputStream theBaos = new ByteArrayOutputStream();
int n = 0;
try {
if (theInStream != null)
{
while (-1 != (n = theInStream.read(theBuff))) {
theBaos.write(theBuff, 0, n);
}
}
theBaos.flush();
} catch (IOException e) {
e.printStackTrace();
}
return theBaos.toByteArray();
}
public void nullSafeSet(PreparedStatement aStmt, Object aValue, int aIndex)
throws HibernateException, SQLException {
aStmt.setBytes(aIndex, (byte[]) aValue);
}
public boolean equals(Object x, Object y) {
if ((x == y) ||
(x != null && y != null && Arrays.equals(
((byte[]) x),
((byte[]) y)))) {
return true;
}
return false;
}
public int hashCode(Object aArg) throws HibernateException {
return aArg.hashCode();
}
public boolean isMutable() {
return false;
}
public Object assemble(Serializable aSerializableObject, Object aObject) throws HibernateException {
return null;
}
public Serializable disassemble(Object aObject) throws HibernateException {
return null;
}
public Object replace(Object aObject1, Object aObject2, Object aObject3) throws HibernateException {
return null;
}
public Object deepCopy(Object aValue) {
return aValue;
}
}
使用“文本”表示LONGVARCHAR,使用“图像”表示LONGVARBINARY,请参见
因此,对于这种情况,您可以使用
@Type(type = "image")
private byte[] fileContent;
如果您使用的是Sybase ASE 15.7(及更高版本),那么它现在支持Lob,所以
@Lob
private byte[] fileContent
除了旧式支持之外,您是否有任何理由使用图像数据类型代替blob?我不熟悉sybase,但我认为JPA不支持图像数据类型。sybase不支持Blob。