我必须将查询的bytea条目转换为bigint。这是怎么做到的呢?
更多信息:
我有一个hibernate存储库如下所示-
@Query(value = "update Sample_Table set other_id = ?1 where id = ?2", nativeQuery = true)
void saveOrUpdateOtherId(Long other_id, Long id);Hibernate以id (在where子句中)作为bytea,因为'Sample_Table‘有这个id字段作为bigint,因此它引发了类型错配问题。
我尝试过使用CAST将bytea转换为bigint,但是它没有成功,错误信息msg说bytea不能传递给bigint。
如何将bytea更改为bigint?
编辑:
Sample_Table 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字段在这里定义为“长”。
编辑-2如果有人得到这样的问题,他很可能是在查询中传递空值。
发布于 2020-10-16 05:46:34
在存储库查询中,我遇到了这个问题,该查询插入了一个具有可空列的记录。当该列的值为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/,它使用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解决方案,而是♂️。大感谢“碳骑士”或谁添加了这个帖子!
发布于 2016-03-31 09:43:37
除了从适当填充的十六进制字符串中传递位数据类型之外,似乎没有一个简单的函数可以将字节(内存块)转换为基本数据类型:
SELECT ('x'||lpad(encode('\001'::bytea, 'hex'), 16, '0'))::bit(64)::bigint或者,如果bytea已经是8个字节,并且Postgres安装使用默认设置bytea_output = 'hex'运行,则使用cast to text and remove the leading backslash:
SELECT right(bytea_val::text, -1)::bit(64)::bigint
FROM (SELECT '\x0000000000000001'::bytea as bytea_val) x发布于 2018-05-15 22:02:00
下面的表达式对我有效,可以从bytes::bytea转换为bigint
get_byte(bytes, 0)::bigint << 8
| get_byte(bytes, 1) << 8
| get_byte(bytes, 2) << 8
| get_byte(bytes, 3) << 8
| get_byte(bytes, 4) << 8
| get_byte(bytes, 5) << 8
| get_byte(bytes, 6) << 8
| get_byte(bytes, 7)这也正确地处理了符号位。
https://stackoverflow.com/questions/32944267
复制相似问题