2012-04-12 7 views
1

Hibernate JPA-Datentyp Blob funktioniert nicht mit dem Sybase-Bilddatentyp. Im Folgenden finden Sie ein Beispiel für den Datentyp, den ich verwende. Kann mir jemand sagen, wie man fileContent dem Sybase-Bilddatentyp zuordnen soll?Hibernate JPA + Sybase-Bilddatentyp

Beispielcode

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

Exception

Verursacht durch: org.hibernate.HibernateException: Falscher Spaltentyp in DEV_eprs.dbo.pr_file_upload für Spalte file_content. Gefunden: image, expected: varbinary (100000)

Bei Verwendung von @Lob wurde die folgende Ausnahme beim Abrufen von Daten empfangen.

java.lang.UnsupportedOperationException Die Methode com.sybase.jdbc3.jdbc.SybResultSet.getBlob (String) wird nicht unterstützt und sollte nicht aufgerufen werden.

+0

Gibt es einen Grund für Sie eine Bilddatentyp anstelle eines Blobs zu verwenden, um andere als Legacy-Unterstützung? Ich bin mit Sybase nicht vertraut, aber ich glaube nicht, dass Bilddatentypen von JPA unterstützt werden. – siebz0r

+1

Sybase unterstützt keine Blobs. –

Antwort

1

Ich löste dieses Problem durch Erstellen eines benutzerdefinierten Datentyps, unten ist die Lösung. Hoffe das hilft anderen.

// Ihr Unternehmen

@Type(type = "org.company.project.entities.types.BlobType") 
private byte[] fileContent; 

public byte[] getFileContent() { 
    return fileContent; 
} 

public void setFileContent(byte[] fileContent) { 
    this.fileContent = fileContent; 
} 

// Benutzerdefinierte Datentyp

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

Verwendung von "Text" für LONGVARCHAR und "Bild" für LONGVARBINARY finden https://hibernate.atlassian.net/browse/HHH-3892

so für diesen Fall Sie können

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

und wenn Sie mit Sybase ASE 15.7 (und höher) unterstützt es jetzt Löb, so

@Lob 
private byte[] fileContent