| Java 類型 | Hibernate 類型 | 標準 SQL 類型 |
|---|---|---|
| byte[] | binary | VARBINARY、BLOB |
| String | text | CLOB |
| java.sql.Clob | clob | CLOB |
| java.sql.Blob | blob | BLOB |
@Lob
public String getC() { return c; }
@Lob
public byte[] getB() { return b; }
String、char[]、Character[]、java.sql.Clob → 映射為 CLOBbyte[]、Byte[]、Serializable、java.sql.Blob → 映射為 BLOB可用 @Column(columnDefinition="longtext") 指定原生類型(不推薦,降低可移植性)。
MySQL 示例:無 @Lob 時 byte[] 映射為 TINYBLOB (256B);加 @Lob 映射為 LONGBLOB (4GB)。
BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\19204.jpg"));
byte[] b = new byte[in.available()];
in.read(b);
bean.setB(b);
in.close();
Session session = HibernateSessionFactory.getSession();
User user = (User)session.get(User.class, 1);
byte[] b = user.getB();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:\\192041.jpg"));
out.write(b);
out.close();
@Lob 保證跨數據庫兼容性