简体   繁体   English

如何获取带有 bytea 类型 id 列的表?

[英]How to fetch table with bytea type id column?

I have a PostgreSQL database and I want to fetch some data with Hibernate.我有一个 PostgreSQL 数据库,我想用 Hibernate 获取一些数据。 I got an entity like;我有一个像这样的实体;

@Entity
public class User {

    @Id
    @Type(type = "uuid-binary")
    @GenericGenerator(name = "user-generator", strategy = "uuid2")
    @GeneratedValue(generator = "user-generator")
    @Column(name = "user_id", length = 16, unique = true, nullable = false)
    private UUID userId;


    ....

}

When I try to get all User values with Hibernate there is no problem.当我尝试使用 Hibernate 获取所有用户值时,没有问题。 Hibernate maps UUID fields. Hibernate 映射 UUID 字段。 But when I try to get a specific User with UUID it returns empty result.但是当我尝试使用 UUID 获取特定用户时,它返回空结果。 How can I fetch a single User with userId value?如何获取具有 userId 值的单个用户?

List<User> users = userDao.getAll();  //I can get all users with non-empty userId fields
userDao.getWithId(users.get(0).getUserId())  // returns null.

users.get(0).getUserId()  //returns java.util.UUID instance like "33333-3333..."

EDIT:编辑:

public User getWithId(UUID Id) {
    EntityManager em = emFactory.createEntityManager();
    try {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(User.class);
        Root<User> typeRoot = criteriaQuery.from(User.class);
        criteriaQuery.select(typeRoot).where(criteriaBuilder.equal(typeRoot.get("userId"), Id));
        final Query query = em.createQuery(criteriaQuery);
        return (User) JPAHelpers.getSingleResultOrNull(query);
    } catch (Exception e) {
        throw e;
    } finally {
        em.close();
    }
}

Although, you didn't share the implementation of getWithId() method.虽然,您没有分享getWithId()方法的实现。 I am assuming that you're using the JPA Repository methods.我假设您正在使用 JPA Repository 方法。 Try annotating the UUID field with:尝试使用以下方法注释UUID字段:

@Type(type="org.hibernate.type.UUIDBinaryType")

or或者

@Type(type="org.hibernate.type.UUIDCharType") // if column is VARCHAR

Or try defining the columnDefinition parameter in @Column annotation:或者尝试在@Column注释中定义columnDefinition参数:

@Column(name = "user_id", columnDefinition = "BINARY(16)", unique = true, nullable = false)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:列“id”是 uuid 类型,但表达式是 bytea 类型 - ERROR: column "id" is of type uuid but expression is of type bytea 如何使用Java将Bytea列下载为文件 - How to download bytea column as file using Java 错误:“ publish_date”列的类型为date,但表达式的类型为bytea - ERROR: column “publish_date” is of type date but expression is of type bytea PostgreSQL 使用 JPA 和 Hibernate 抛出“列的类型为 jsonb,但表达式的类型为 bytea” - PostgreSQL throws "column is of type jsonb but expression is of type bytea" with JPA and Hibernate hibernate-spatial-4.0 创建 bytea 列类型而不是几何 - hibernate-spatial-4.0 creates bytea column type instead of geometry 如何使用MyBatis从BYtea列获取byte []? - How can I get byte[] from bytea column with MyBatis? 如何在java中获取数据库表列名 - How to fetch database table column names in java 当 NULL LocalDate 作为输入时,本机命名查询失败,出现异常“列的类型为日期,但表达式的类型为 bytea” - Native named query fails with exception "column is of type date but expression is of type bytea"when NULL LocalDate is given as input 使用GenerationType.IDENTITY时在帮助程序表中获取bytea类型的字段 - Getting field of type bytea in helper table when using GenerationType.IDENTITY org.postgresql.util.PSQLException:错误:“ geo_detail”列的类型为point,但是表达式的类型为bytea? - org.postgresql.util.PSQLException: ERROR: column “geo_detail” is of type point but expression is of type bytea any solution please?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM