0

I'm working in a web app with Hibernate 5 and SpringMVC, when I worked just with simple tables (without relationships between any tables) everything work okay, update, save, delete..., but if I try to add some relationships between tables, and when I mapped, I got this error :

@OneToOne or @ManyToOne on 'univ_orientation.dz.Entity.cursusGlobale.Student_idStudent' references an unknown entity: int

I had searched to solve this issue, I found out that its a configuration problem, Hibernate doesn't recognize class cursusGlobale as an entity.

My question is why Hibernate doesn't recognize class cursusGlobale as an entity when I work with relationships, but it does if I work just with simple tables without relationships between tables?

  package univ_orientation.dz.Entity;

 import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;

@Entity
@Table(name="cursusGlobale")
public class cursusGlobale {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column(name="idcursusGlobale")
private int idcursusGlobale;

@Column(name="nbrRedoubler")
private int nbrRedoubler;

@Column(name="nbrDette") 
private int nbrDette;


@Column(name="idStudent")
private int idStudent;

@OneToOne( cascade= CascadeType.ALL)
@JoinColumn(name="Student_idStudent")
private int Student_idStudent;



public cursusGlobale() {

}


public cursusGlobale(int nbrRedoubler, int nbrDette, int 
 student_idStudent) {
    super();
    this.nbrRedoubler = nbrRedoubler;
    this.nbrDette = nbrDette;
    idStudent = student_idStudent;
  }



public int getIdcursusGlobale() {
    return idcursusGlobale;
}

public void setIdcursusGlobale(int idcursusGlobale) {
    this.idcursusGlobale = idcursusGlobale;
}

public int getNbrRedoubler() {
    return nbrRedoubler;
}

public void setNbrRedoubler(int nbrRedoubler) {
    this.nbrRedoubler = nbrRedoubler;
}

public int getNbrDette() {
    return nbrDette;
}

public void setNbrDette(int nbrDette) {
    this.nbrDette = nbrDette;
}


public int getIdStudent() {
    return idStudent;
}


public void setIdStudent(int idStudent) {
    this.idStudent = idStudent;
}


}
  • 1
    Please provide the code of your entities so we can try to reproduce the error. – Luiggi Mendoza May 23 '19 at 15:01
  • I had found the error, its a configuration problem, My question is why Hibernate doesn't recognize class cursusGlobale as an entity when I work with relationships, but it does if I work just with simple tables without relationships between tables? – NadjiThabet May 23 '19 at 16:04
  • Maybe you class wasn't marked as `@Entity`. Maybe something else. In order to help you analyze that situation, we need to see a glimpse of your code. – Luiggi Mendoza May 23 '19 at 16:58
  • no, all my models has marked as @Entity, I have edit my post to show you my cursusGlobale class that caused the problem – NadjiThabet May 23 '19 at 17:44

1 Answers1

0

You have not create a relationship. You are using a java native type int instead of another class Student.

@OneToOne( cascade= CascadeType.ALL)
@JoinColumn(name="Student_idStudent")
private int Student_idStudent;

As the error message clearly says: @OneToOne or @ManyToOne on 'univ_orientation.dz.Entity.cursusGlobale.Student_idStudent' references an unknown entity: int, Student_idStudent is defined as an int, not as an Entity. You will need something more along the lines of

@OneToOne( cascade= CascadeType.ALL)
@JoinColumn(name="Student_idStudent")
private Student student;

Even though it is a class it will be persisted as a Foreign Key to the Student table in the database. This is part of the abstraction provided by JPA.

Reference: JPA Hibernate One-to-One relationship

K.Nicholas
  • 10,956
  • 4
  • 46
  • 66