【问题标题】:Hibernate mapping - Could not determine type for pg-uuid休眠映射 - 无法确定 pg-uuid 的类型
【发布时间】:2023-03-26 09:42:01
【问题描述】:

我实际上是在尝试使用带有 java 注释的 Hibernate ORM 进行映射。我将 PostgreSQL 用于我的数据库及其 UUID 类型。正如我在其他帖子中看到的那样,当我想将 UUID pgsql 类型映射到 UUID Java 类型时,我应该将 @Type(type="pg-uuid") 添加到每个 UUID 字段。问题是当我得到这个时,它似乎没有被休眠识别:

org.hibernate.MappingException:无法确定类型:pg-uuid,表:ev_session,列:[org.hibernate.mapping.Column(user_id)]

我在 Google 上找不到任何提及这一点的内容,所以我真的不知道应该去哪里看。

这是我的映射类。该表使用两个 UUID 作为主键,这就是为什么我必须创建一个嵌套类来表示它。不过我不确定我做对了。

@Entity
@Table(name="ev_session")
public class SessionDb {

    ////////////////////////
    // VARIABLES
    ////////////////

    @Id
    private PrimaryKey primaryKey;
    @Column(name="date")
    private Date timestamp;

    ////////////////////////
    // NESTED CLASSES
    ////////////////

    @Embeddable
    private class PrimaryKey implements Serializable {
        private static final long serialVersionUID = 7124577164356450734L;

        @Type(type="pg-uuid")
        @Column(name="user_id")
        public UUID userID;
        @Type(type="pg-uuid")
        @Column(name="key")
        public UUID token;
    }

    ////////////////////////
    // CONSTRUCTORS
    ////////////////

    public SessionDb() {
        this.primaryKey = new PrimaryKey();
    }

    ////////////////////////
    // METHODS
    ////////////////

    @Override
    public String toString() {
        return this.primaryKey.token + " associated to " + this.primaryKey.userID + " at " + this.timestamp; 
    }

    ////////////////////////
    // GETTERS/SETTERS
    ////////////////

    public final UUID getUserID() {
        return this.primaryKey.userID;
    }

    public final void setUserID(UUID userID) {
        this.primaryKey.userID = userID;
    }

    public final UUID getToken() {
        return this.primaryKey.token;
    }

    public final void setToken(UUID token) {
        this.primaryKey.token = token;
    }

    public final Date getTimestamp() {
        return timestamp;
    }

    public final void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

}

感谢您的帮助

【问题讨论】:

  • 您使用的是哪个版本的Hibernate
  • hibernate 3.2.6.ga,hibernate-annotations 3.2.0.ga,Hibernate-commons-annotations 3.2.0.Final

标签: java hibernate postgresql mapping uuid


【解决方案1】:

我猜你应该使用Generator:

@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")

看看here。 更多文档here

【讨论】:

  • 对不起,我不确定我明白了。我遇到的问题是我无法从数据库中获取。无论如何,谢谢,它可能有用!
【解决方案2】:

如果是xml映射,你可以添加

<typedef class="org.hibernate.type.PostgresUUIDType" name="pg-uuid" />

https://developer.jboss.org/thread/229747 中所述,但我不知道如何使用注释进行配置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 2019-02-12
    • 2021-01-11
    • 2014-10-26
    相关资源
    最近更新 更多