我必须将查询的 bytea 条目转换为 bigint。这怎么可能做到呢?
更多信息:
我有一个休眠存储库,如下所示 -
@Query(value = "update Sample_Table set other_id = ?1 where id = ?2", nativeQuery = true)
void saveOrUpdateOtherId(Long other_id, Long id);
Hibernate 以某种方式采取id
(在 where 子句中)asbytea
自从'样本表' 这个 id 字段为bigint
因此它会引发类型不匹配问题。
我尝试过使用 CAST 进行转换bytea
to bigint
但它没有成功并且错误消息说bytea
无法投射到bigint
.
我该如何改变bytea
to bigint
?
Edit:
样本表 DAO:
@Table(name = "Sample_Table")
public class Sample{
@Id
@Column(name = "id", unique = true)
@GeneratedValue
private Long id;
@Column(name = "other_id")
private Long other_id;
}
id
字段在这里定义为 Long。
Edit-2如果有人遇到这样的问题,很可能他在查询中传递了空值。
我在存储库查询中遇到了这个问题,该查询插入了具有可为空列的记录。当该列的值为 null 时,hibernate 使用错误的类型,我会在 Postgres 中看到如下异常:
cannot cast type bytea to bigint
最终找到了这篇博文,给出了解决方案:http://www.carbonrider.com/2020/08/15/org-postgresql-util-psqlexception-error-cannot-cast-type-bytea-to-uuid/ http://www.carbonrider.com/2020/08/15/org-postgresql-util-psqlexception-error-cannot-cast-type-bytea-to-uuid/也就是使用Hibernate的TypedParameterValue
。
复制并粘贴他们的片段:
@Query("select * from user where firstname=:name and id=:id", nativeQuery=true)
public List<user> findByNameAndId(@Param("name") String firstName, @Param("id")TypedParameterValue id);
UUID userId = ... //Retrived from request parameter.
TypedParameterValue userIdParam = new TypedParameterValue(new PostgresUUIDType(), userId);
userRepository.findByNameAndId(userName, userIdParam);
拥有特定于 Hibernate 的解决方案而不是纯粹的 JPA 解决方案并不理想,但是 ????♂️。非常感谢“Carbon Rider”或添加该帖子的任何人!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)