在组合键中休眠@GeneratedValue [英] Hibernate @GeneratedValue in a composite key

查看:95
本文介绍了在组合键中休眠@GeneratedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

我想在hibernate中映射以下内容

I want to map the following in hibernate

实现用户表时出现的错误是:

The error I get when I implement users table is:

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

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

I am using MySql and the field id is auto incremented

@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,但是我不确定如何正确使用它

The problem is that in hibernate if I wish to use two primary keys for an Entity I have to use the annotation @Embedded but I am not sure how to use it properly

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

I can easily fix the problem using @Transient annotation on id but this does not seem the right approach

请您帮我解决这个问题

更新

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

Thanks to @Prasanna I created the following but still same problem

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类

Noted: I have also updated my User class with

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

推荐答案

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

Apparently when I use GeneratedValue in a composite key many problems arise

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

Removing the GeneratedValue annotation solved my problem

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

Can anyone explain if this is enough for mysql auto increment option to work properly

这篇关于在组合键中休眠@GeneratedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆