【问题标题】:Hibernate @GeneratedValue in a composite key在复合键中休眠 @GeneratedValue
【发布时间】:2015-06-24 00:18:39
【问题描述】:

问题:

我想在hibernate中映射以下内容

我在实现 users 表时遇到的错误是:

无法通过 User.id 的反射设置器设置字段值

我正在使用 MySql 并且字段 id 是自动递增的

@Entity
@Table(name="users")
public class User implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Integer id;

    @Id
    private String username;

    //Other fields  
    public User()
    {

    }

    public User(String username, String email, String firstName,
            String lastName, String password, String authority,
            boolean enabled, boolean reset, boolean deleted) 
    {
        this.username = username;
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        this.authority = authority;
        this.enabled = enabled;
        this.reset = reset;
        this.deleted = deleted;
    }

    public User(int id, String username, String email, String firstName,
            String lastName, String password, String authority,
            boolean enabled, boolean reset, boolean deleted) 
    {
        this.id = id;
        this.username = username;
        this.email = email;
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        this.authority = authority;
        this.enabled = enabled;
        this.reset = reset;
        this.deleted = deleted;
    }

    public int getId() 
    {
        return id;
    }

    public void setId(int id) 
    {
        this.id = id;
    }

问题是,在休眠状态下,如果我想为一个实体使用两个主键,我必须使用注释 @Embedded 但我不确定如何正确使用它

我可以在 id 上使用 @Transient 注释轻松解决问题,但这似乎不是正确的方法

你能帮我解决这个问题吗

更新

感谢@Prasanna,我创建了以下但仍然相同的问题

public class UserPK implements Serializable 
{
/**
     * 
     */
    private static final long serialVersionUID = 1L;


    protected Integer id;
    protected String username;

    public UserPK() 
    {

    }

    public UserPK(Integer id, String username) 
    {
        this.id = id;
        this.username = username;
    }

    @Override
    public int hashCode() 
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result
                + ((username == null) ? 0 : username.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) 
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        UserPK other = (UserPK) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }
}

注意:我还更新了我的用户类

@Entity
@IdClass(UserPK.class)
@Table(name="users")
public class User implements Serializable
{

【问题讨论】:

标签: hibernate annotations


【解决方案1】:

根据 Hibernate 中的当前实现,不可能在复合键中使用 @GeneratedValue,请选择复合键或单个 @GeneratedValue id。

在 Hibernate 中报告了带有自动生成部分的复合 ID 的问题 - HHH-6044

【讨论】:

    【解决方案2】:

    显然,当我在复合键中使用 GeneratedValue 时会出现很多问题

    删除 GeneratedValue 注释解决了我的问题

    谁能解释这是否足以让mysql自动增量选项正常工作

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多