2012-04-12 14 views
1

Hibernate JPAデータ型blobは、Sybaseイメージ・データ型では機能しません。以下は私が使用しているデータ型のサンプルです。誰かがfileContentをSybaseのイメージ・データ型にマップする方法を教えてもらえますか?Hibernate JPA + Sybaseイメージ・データ型

サンプルコードによって引き起こさ

@Column(length=100000) 
private byte[] fileContent; 

例外

:org.hibernate.HibernateException:カラムfile_contentためDEV_eprs.dbo.pr_file_uploadで間違った列型。見つかった:image、expected:varbinary(100000)

@Lobを使用すると、データを取得するときに次の例外が発生しました。

java.lang.UnsupportedOperationException メソッドcom.sybase.jdbc3.jdbc.SybResultSet.getBlob(String)はサポートされていないため、呼び出さないでください。

+0

レガシーサポート以外のブロブの代わりに画像データ型を使用するために何らかの理由があるので、今ロブをサポートしていますか?私はsybaseに精通していませんが、イメージ・データ型はJPAでサポートされているとは思いません。 – siebz0r

+1

Sybaseはblobをサポートしていません。 –

答えて

1

カスタムデータ型を作成することでこの問題を解決しました。以下はその解決策です。これが他人を助けることを望みます。

// LONGVARCHARとLONGVARBINARYのための "画像" を "テキスト" を使用したエンティティ

@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; 
    } 

} 
1

、この場合のためにそうhttps://hibernate.atlassian.net/browse/HHH-3892

を見ます、使用することができます

@Type(type = "image") 
private byte[] fileContent; 

とは、Sybase ASE 15.7(以上)を使用している場合は、それが

@Lob 
private byte[] fileContent