QGA
QGA

Reputation: 3192

Hibernate @GeneratedValue in a composite key

Problem:

I want to map the following in hibernateenter image description here

The error I get when I implement users table is:

Could not set a field value by reflection setter of User.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;
    }

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

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

Can you help me in sorting this out please

UPDATE

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;
    }
}

Noted: I have also updated my User class with

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

Upvotes: 4

Views: 4654

Answers (2)

Yatrik Mehta
Yatrik Mehta

Reputation: 51

As per current implementation in Hibernate, its not possible to use @GeneratedValue in a composite key, choose either a composite key or a single @GeneratedValue id.

There has been a reported issue in Hibernate for Composite ID with auto-generated part - HHH-6044

Upvotes: 5

QGA
QGA

Reputation: 3192

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

Removing the GeneratedValue annotation solved my problem

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

Upvotes: 2

Related Questions