【发布时间】:2021-10-21 09:10:15
【问题描述】:
我试图在 Spring Boot 下使用 Hibernate 懒惰地获取单个 byte[] content java 属性,访问 PostgreSQL 数据库。所以我把测试应用程序放在一起来测试不同的解决方案。其中一个要求我在所述属性上使用@Lob 注释,所以我做到了。现在从数据库中读取实体会导致非常奇怪的错误,确切地说:
Bad value for type long : \x454545454545445455
\x45... 的值是 bytea 列的值而不是 bigint 列的值,为什么即使它是错误的列,它也试图强制它进入 long?为什么一列上的注释会以某种方式影响另一列?
至于修复,删除@Lob 似乎有效(至少在我的堆栈中)但问题仍然无法解释,我想知道发生了什么,而不是盲目地继续前进。是错误还是我完全误解了某些东西?
实体:
@Entity
@Table(name = "blobentity")
public class BlobEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Lob //this annotation breaks code
@Column(name = "content")
@Basic(fetch = FetchType.LAZY)
private byte[] content;
@Column(name = "name")
private String name;
//getters/setters
}
存储库:
@Repository
public interface BlobRepo extends JpaRepository<BlobEntity, Long> {
}
调用代码:
@Autowired
BlobRepo blobrepo;
@GetMapping("lazyBlob")
public String blob () {
var t = blobrepo.findAll().get(0);
var name = t.getName();
var dataAccessedIfLazy = t.getContent();
return t.getName();
}
Postgres DDL:
CREATE TABLE test.blobentity (
id bigserial NOT NULL DEFAULT nextval('test.blobentity_id_seq'::regclass),
"name" varchar NULL,
"content" bytea NULL,
CONSTRAINT blobentity_pk PRIMARY KEY (id)
);
选择结果:
使用过的版本:
PostgreSQL 10.4; springframework.boot 2.4.2;这个spring boot版本自带的hibernate版本
【问题讨论】:
标签: java postgresql hibernate blob