Sinan Samet
Sinan Samet

Reputation: 6752

Hibernate gives could not determine type error

I have a Roles table which has an id and role name and a users table which has a role_id. I want to join those but I get the error Could not determine type for: com.aon04.backend.models.Role, at table: users, for columns: [org.hibernate.mapping.Column(role)] when I try to do that.

This is my role model:

@Entity
@Table(name = "roles")

public class Role {
    @Id
    @GeneratedValue()
    @Column(name = "id")
    private int id;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id", nullable = false)
    @JsonIgnore
    public User user;

    @Column(name = "name", nullable = false)
    private String name;

}

And this my user model:

@Entity
@Table(name = "users")
public class User implements Serializable {
    @Id
    @GeneratedValue()
    @Column(name = "id")
    private int id;

    public Role role;

    public String getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(String studentNumber) {
        this.studentNumber = studentNumber;
    }

    @Column(name = "student_number", nullable = true)
    private String studentNumber;

    @Column(name = "first_name", nullable = true)
    private String firstName;

    @Column(name = "last_name", nullable = true)
    private String lastName;

    @Column(name = "email", nullable = true, unique = true)
    private String email;

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "user")
    private FinishedExam finishedExam;

    @JsonIgnore
    @Column(name = "password", nullable = true)
    private String password;


    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @Column(name = "updated_at")
    private LocalDateTime updatedAt;


    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "event_users",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "event_id", referencedColumnName = "id")
    )

    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = createdAt;
    }

    public int getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

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

    public String getLastName() {
        return lastName;
    }

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

    public String getEmail() {
        return email;
    }

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

    public String getPassword() {
        return password;
    }

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


    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }
}

I can't figure out what I am doing wrong. My desired output is when I retrieve the JSON of User I want the user data with the role as name displayed.

Upvotes: 0

Views: 47

Answers (2)

Ajit
Ajit

Reputation: 19

I can see you have user many-to-one relationship from roles to User. Does User can have multiple roles? And you need to add association from user to role in user table. It could be @ManyToMany when User can have many roles

Upvotes: 1

HBo
HBo

Reputation: 633

There is an inconsistancy in your model: a Role has a User with a ManyToOne relationship, so your User should have a collection of Role You didn't map your rolein User, do it by adding a bidirectional relationship:

@OneToMany(mappedBy = "user")
private List<Role> role = new ArrayList<>();

Upvotes: 1

Related Questions