【问题标题】:Hibernate analogue for jdbc statement.setBinaryStreamjdbc statement.setBinaryStream 的休眠模拟
【发布时间】:2020-02-21 05:22:48
【问题描述】:

我有下面的 jdbc sn-p,我想在休眠中使用本机查询进行转换。

问题:这是我的 jdbc 代码:

{
   statement = conn.prepareStatement("INSERT INTO CCM_JIGSAW_FILES (FILE_ID,FILE_FOR,FILE_CONTENT) 
                    VALUES (?,?,?)");
   ByteArrayInputStream in = new ByteArrayInputStream(fileContent);//fileContent is byte[]
   statement.setString(1, file_id);
   statement.setString(2, licenseType);
   statement.setBinaryStream(3,in,fileContent.length);
   statement.executeUpdate();
}

已尝试的解决方案:尝试使用休眠模式,但运气不佳

{
   String insertSqlQuery = "INSERT INTO CCM_JIGSAW_FILES (FILE_ID,FILE_FOR,FILE_CONTENT) VALUES (?,?,?)";
   inputStream = new ByteArrayInputStream(fileContent);

   Query insertQuery = entityManager.createNativeQuery(insertSqlQuery);
   insertQuery.setParameter(1, fileId);
   insertQuery.setParameter(2, licenseType);
   insertQuery.setParameter(3, inputStream);// Here i am getting runtime exception.
   insertQuery.executeUpdate();
}
org.hibernate.HibernateException: Could not determine a type for
class: java.io.ByteArrayInputStream

如何将statement.setBinaryStream(3,in,fileContent.length) 转换为休眠模式? 注:数据库中FILE_CONTENT的数据类型为Blob

【问题讨论】:

    标签: java sql hibernate jpa


    【解决方案1】:

    假设您有 CCM_JIGSAW_FILES 表的 POJO 类,并且 POJO 类中 File_Content 的数据类型应为 byte[]

    您可以使用setter方法直接将byte[]保存在File_Content中。

    PojoObject.setterMethod(fileContent);
    

    Refer this example

    【讨论】:

    • 虽然这对小文件很有用,但它需要将整个 blob 存储在内存中,如果您有超过 400MB 的数据,这可能会成为问题。假设这就是提问者专门询问流的原因。
    猜你喜欢
    • 2010-11-24
    • 1970-01-01
    • 2011-05-01
    • 2020-04-05
    • 1970-01-01
    • 2023-03-25
    • 2015-02-01
    • 2012-04-20
    • 2019-02-09
    相关资源
    最近更新 更多