【问题标题】:Hibernate JPA + Sybase image datatypeHibernate JPA + Sybase 图像数据类型
【发布时间】:2012-04-25 03:22:44
【问题描述】:

Hibernate JPA 数据类型 blob 不适用于 Sybase 图像数据类型。下面是我正在使用的数据类型的示例。有人能告诉我如何将 fileContent 映射到 Sybase 图像数据类型吗?

示例代码

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

异常

原因:org.hibernate.HibernateException:列 file_content 的 DEV_eprs.dbo.pr_file_upload 中的列类型错误。找到:图像,预期:varbinary(100000)

使用@Lob 时,检索数据时收到以下异常。

java.lang.UnsupportedOperationException com.sybase.jdbc3.jdbc.SybResultSet.getBlob(String) 方法不受支持,不应调用。

【问题讨论】:

  • 除了传统支持之外,您还有什么理由使用图像数据类型而不是 blob?我对 sybase 不熟悉,但我认为 JPA 不支持图像数据类型。
  • Sybase 不支持 blob。

标签: java hibernate jpa sybase


【解决方案1】:

对 LONGVARCHAR 使用“文本”,对 LONGVARBINARY 使用“图像”,请参阅 https://hibernate.atlassian.net/browse/HHH-3892

所以对于这种情况,你可以使用

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

如果您使用的是 Sybase ASE 15.7(及更高版本),它现在支持 Lob,所以

@Lob
private byte[] fileContent

【讨论】:

    【解决方案2】:

    我通过创建自定义数据类型解决了这个问题,下面是解决方案。希望这对其他人有所帮助。

    //你的实体

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

    【讨论】:

      猜你喜欢
      • 2020-03-28
      • 2011-10-20
      • 2017-03-22
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      相关资源
      最近更新 更多