4

I'm having the following Entity. I'm trying to replace the folliwing. (See image) but I'm getting the following error: Persistent Entity should have a primary key.

(I'm new to hibernate) (Please comment if you also need the UserEntity Class) (Thanks in advance!)

enter image description here

@Entity
@Table(name = "Student", schema = "pad_ijburg", catalog = "")
public class StudentEntity extends UserEntity {
    private Integer idGroup;
    private int idUser;

    @Basic
    @Column(name = "idGroup")
    public Integer getIdGroup() {
        return idGroup;
    }

    public void setIdGroup(Integer idGroup) {
        this.idGroup = idGroup;
    }

    @Basic
    @Column
    public int getIdUser() {
        return idUser;
    }

    public void setIdUser(int idUser) {
        this.idUser = idUser;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        StudentEntity that = (StudentEntity) o;

        if (idUser != that.idUser) return false;
        if (idGroup != null ? !idGroup.equals(that.idGroup) : that.idGroup != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = idGroup != null ? idGroup.hashCode() : 0;
        result = 31 * result + idUser;
        return result;
    }
}

package org.hva.folivora.model.user;

import javax.persistence.*;

/**
 * 
 */
@Entity
@Table(name = "User", schema = "pad_ijburg")
@Inheritance(strategy = InheritanceType.JOINED)
public class UserEntity {
    private int idUser;
    private String email;
    private String firstName;
    private String lastName;
    private String password;
    private Boolean admin;

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "idUser", nullable = false)
    public int getIdUser() {
        return idUser;
    }

    public void setIdUser(int idUser) {
        this.idUser = idUser;
    }

    @Basic
    @Column(name = "email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Basic
    @Column(name = "firstName")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Basic
    @Column(name = "lastName")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Basic
    @Column(name = "password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Basic
    @Column(name = "admin")
    public Boolean getAdmin() {
        return admin;
    }

    public void setAdmin(Boolean admin) {
        this.admin = admin;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserEntity that = (UserEntity) o;

        if (idUser != that.idUser) return false;
        if (email != null ? !email.equals(that.email) : that.email != null) return false;
        if (firstName != null ? !firstName.equals(that.firstName) : that.firstName != null) return false;
        if (lastName != null ? !lastName.equals(that.lastName) : that.lastName != null) return false;
        if (password != null ? !password.equals(that.password) : that.password != null) return false;
        if (admin != null ? !admin.equals(that.admin) : that.admin != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = idUser;
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
        result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (admin != null ? admin.hashCode() : 0);
        return result;
    }
}

Edit: Also getting the following error when using generated value identiti

.StudentEntity column: idUser (should be mapped with insert="false" update="false")
3
  • 1
    Does UserEntity have an @Id annotated field? Commented Apr 20, 2016 at 20:36
  • 1
    Please post the complete error message and explain when it occurs. Commented Apr 20, 2016 at 20:55
  • @gmaslowski Yes it does. Commented Apr 20, 2016 at 20:57

1 Answer 1

1

Can you please try replacing your code

@GeneratedValue(strategy=GenerationType.AUTO)

with

@GeneratedValue(strategy=GenerationType.IDENTITY)
Sign up to request clarification or add additional context in comments.

1 Comment

I had what you had mentioned in my code and was facing the same error. replacing with the above code fixed my issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.