jdbc语句的Hibernate模拟.setBinaryStream



我有下面的jdbc片段,我想在hibernate中使用Native查询进行转换。

问题:这是我的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();
}

尝试过的解决方案:尝试过hibernate,但没有运气

{
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)转换为hibernate?注:数据库中FILE_CONTENT的数据类型为Blob

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

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

PojoObject.setterMethod(fileContent);

参考本示例

最新更新