Hibernate 中如何注释 blob 列?

发布于 2024-07-23 04:35:29 字数 266 浏览 2 评论 0原文

Hibernate 中如何注释 blob 列? 到目前为止,我有一堂课:

@Column( name = "FILEIMAGE" )
private byte[ ] fileimage ;
//
public byte[ ] getFileimage ( ) { return this.fileimage ; }
public void setFilename ( String filename ) { this.filename = filename ; }

How is a blob column annotated in Hibernate?
So far I have a class that has:

@Column( name = "FILEIMAGE" )
private byte[ ] fileimage ;
//
public byte[ ] getFileimage ( ) { return this.fileimage ; }
public void setFilename ( String filename ) { this.filename = filename ; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2024-07-30 04:35:29

@Lob 应该可以解决 blob 和 clob (使用 String 作为类型)

@Column( name = "FILEIMAGE" )
@Lob(type = LobType.BLOB)
private byte[] fileimage;

@Lob should do the trick for blob and clob (use String as type)

@Column( name = "FILEIMAGE" )
@Lob(type = LobType.BLOB)
private byte[] fileimage;
星軌x 2024-07-30 04:35:29

我在 JBoss 7 和 Java 7 中使用了 hibernate 4,发现表中的 BLOB 列不像我在 hibernate 2 中那样工作。幸运的是,我通过阅读其他人的解决方案解决了这个问题。
我的解决方案:

  1. 表在db中,列仍然在BLOB中定义; 将 hibernate 映射从 type="blob" 更改为 type="binary"
  2. 在 Java getter/setter 中,使用 byte[] 而不是 < code>BLOB (javax.sql)
  3. 更改正确获取和设置此列的 Java 代码。 如果涉及InputStram,则使用byte[]来读取/写入BLOB列; 如果使用 java.sql.ResultSet 读取数据库,请确保使用 getBytes() 而不是 getBlob() 方法。

I used hibernate 4 in JBoss 7 and Java 7, and found out the BLOB column in my table doesn't work like what I have for hibernate 2. Fortunately, I solved it by reading other people solutions.
My solution:

  1. Table in db, the column still defined in BLOB; change the hibernate mapping from type="blob" to type="binary"
  2. In Java getter/setter, using byte[] instead of BLOB (javax.sql)
  3. Change in the Java code which get and set this column properly. If involving InputStram, use byte[] to read/write to BLOB column; If reading from DB by using java.sql.ResultSet, make sure use getBytes() instead of getBlob() method.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文