Hibernate @GeneratedValue在复合键中

时间:2015-04-17 08:37:52

标签: hibernate annotations

问题:

我想在休眠enter image description here

中映射以下内容

我实现用户表时遇到的错误是:

无法通过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;
    }

问题是在hibernate中如果我想为实体使用两个主键我必须使用注释@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;
    }
}

注意:我还用

更新了我的User类
@Entity
@IdClass(UserPK.class)
@Table(name="users")
public class User implements Serializable
{

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

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

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

任何人都可以解释这是否足以让mysql自动增量选项正常工作