Reputation: 139
I'm new to hibernate try lean hibernate relationship and have tow table, relationship is one to many
create table student(
id bigint primary key auto_increment,
name varchar(20),
student_class_id bigint foreign key references student_class(id)
)
create table student_class(
id bigint primary key auto_increment,
name varchar(20)
)
spring sessionFactory bean have configured packageToScan entity
this is entity class:
@Table(name = "students")
@Entity
public class Student {
private Integer id;
private Integer name;
private StudentClass studentClass;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public Integer getName() {
return name;
}
public void setName(Integer name) {
this.name = name;
}
@OneToOne
@JoinColumn(name = "student_class_id")
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
public StudentClass getStudentClass() {
return studentClass;
}
// setter getter
}
@Entity
@Table(name = "student_class")
public class StudentClass {
private Integer id;
private String name;
private Set<Student> students = new HashSet<Student>();
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
@OneToMany(mappedBy = "studentClass", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
public Set<Student> getStudents() {
return students;
}
/setter getter
}
sql is: select sc.name from StudentClass sc left join Student s on sc.id = s.student_class_id
and junit test fount error
[DEBUG] 2018-05-26 11:59:39.993 [main] HqlSqlBaseWalker - select << begin [level=1, statement=select]
[DEBUG] 2018-05-26 11:59:40.005 [main] FromElement - FromClause{level=1} : com.f.pojo.StudentClass (sc) -> studentcla0_
[DEBUG] 2018-05-26 11:59:40.009 [main] HqlSqlWalker - Creating entity-join FromElement [s -> com.f.pojo.Student]
[DEBUG] 2018-05-26 11:59:40.010 [main] FromElement - FromClause{level=1} : com.f.pojo.Student (s) -> student1_
[DEBUG] 2018-05-26 11:59:40.012 [main] FromReferenceNode - Resolved : sc -> studentcla0_.id
[DEBUG] 2018-05-26 11:59:40.013 [main] DotNode - getDataType() : id -> org.hibernate.type.IntegerType@26a2f7f9
[DEBUG] 2018-05-26 11:59:40.013 [main] FromReferenceNode - Resolved : sc.id -> studentcla0_.id
[DEBUG] 2018-05-26 11:59:40.013 [main] FromReferenceNode - Resolved : s -> student1_.id
[ERROR] 2018-05-26 11:59:40.016 [main] ErrorTracker - could not resolve property: student_class_id of: com.f.pojo.Student
[ERROR] 2018-05-26 11:59:40.016 [main] ErrorTracker - could not resolve property: student_class_id of: com.f.pojo.Studentcker - could not resolve property: student_class_id of: com.f.pojo.Student
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: could not resolve property: student_class_id of: com.f.pojo.Student [select sc.name from com.f.pojo.StudentClass sc left join com.f.pojo.Student s on sc.id = s.student_class_id]
I'm two entity relationship seem be right, student entity using studentClass
property references studentClass
entity
Upvotes: 0
Views: 136
Reputation: 1057
Your SQL:
create table student(
Your class:
@Table(name = "students")
These names need to match. Even a single character (s) makes a big difference.
Another problem in your SQL:
name varchar(20),
Your class:
private Integer name;
The type also needs to match. I believe you meant to use String in your class instead of Integer.
Upvotes: 1