使用Hibernate SQLQuery返回Postgres UUID

时间:2016-12-21 20:19:23

标签: java sql postgresql hibernate uuid

我已经阅读了几个关于PostgreSQLHibernateUUID s的答案的问题,但没有一个是关于我面临的这个问题。

检查此域名实体:

import java.io.Serializable;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

@Entity
@Table(name = "message")
public class Message implements Serializable {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "my.UUIDGenerator")
    @Type(type = "pg-uuid")
    @Column(name = "id", columnDefinition = "uuid")
    private UUID                id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "account")
    private Account             account;

    @Type(type = "pg-uuid")
    @Column(name = "user_created")
    private UUID                userCreated;

    @Column(name = "message", columnDefinition = "text")
    private String              message;
}

我想用这个SQL查询查询它:

StatelessSession session = sessionFactory.openStatelessSession();
sql = "select "
+ "cast(id as varchar) as \"id\" " // ugly, but works
// + "id " // does not work, produces o.h.MappingException: No Dialect mapping for JDBC type: 1111
+ "from message m "
+ "where m.account = :account "
+ "and m.chat = :chatId ";

SQLQuery query = session.createSQLQuery(sql);
query.setParameter("account", accountId);
query.setParameter("chatId", chatId);
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String, Object>> result = query.list();
session.close();

如代码所示,返回UUID不起作用。

获取返回值的唯一(坏)方法是将其强制转换为varchar。

方言Hibernate使用的配置如下:

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect

为了完整性,数据库表的定义如下:

create table message (
   id             uuid        not null,
   user_created   uuid,
   message        text,
   account        uuid,
   chat           uuid
);

alter table public.message add constraint message_pkey primary key (id);

我使用以下环境:

  • Linux 4.8.14
  • OpenJDK运行时环境(版本1.8.0_111-b16)
  • PostgreSQL 9.5.5
  • Hibernate 5.0.11.Final
  • Spring Boot 1.4.2.RELEASE

1 个答案:

答案 0 :(得分:0)

在我看来,这是可行的

    List<String> messageIdStringArrayList
        = currentSession().createSQLQuery(String.format(
            "select cast(m.id as varchar) " +
                    "from message m " +
                    "where m.account = '%s' " +
                    "and " +
                    "m.chat = '%s'",
            String.valueOf(accountId), String.valueOf(chatId))).list();

    List<UUID> messageIdArrayList = new ArrayList<>();

    if (CollectionUtils.isNotEmpty(messageIdStringArrayList)) {
        for (String idString : messageIdStringArrayList) {
            messageIdArrayList.add(UUID.fromString(idString));
        }
    }