This mapping is wrong:
@JoinTable
is for associations not for basic properties.
Now, if you auto-generated the schema, the mapping will work.
The problem is that you have a database schema that does not match with the entity mappings. That’s because there is a hi5s.subject_tutor
in the database which is not present in your entity mappings.
Most likely that you want to use a unidirectional association with a join table. So, try this mapping instead:
@Entity (name = "Tutor")
@Table(name = "tutor")
public class Tutor implements Serializable{
public Tutor() {}
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
@Column(name="tutor_id")
private int tutor_id;
@OneToMany
@JoinTable(name="tutor_id", joinColumns=@JoinColumn(name="subject_id"))
private List<Subject> subjects = new ArrayList<>();
public Tutor(Tutor tutor){
this.Name = tutor.Name;
this.NRIC = tutor.NRIC;
this.gender = tutor.gender;
this.subjects = tutor.subjects; // subjects
.....and so on and so for...
}
public Tutor Name(String Name) {
this.Name = Name;
return this;
}
@Column(name="tutorName")
@NotNull
private String Name;
//The owner side is the side which Hibernate looks at to know which association exists - the one that dun have mapped by
public List<Subject>getSubjects(){
return subjects;
}
public void addSubject(Subject subject){
subjects.add(subject);
}
public void removeSubject(Subject subject){
subjects.remove(subject);
}
public int getTutor_id() {
return tutor_id;
}
public int setTutor_id(int tutor_id) {
return this.tutor_id = tutor_id;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
@Override
public boolean equals (Object o){
if (this == o){
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Tutor tutor = (Tutor)o;
//Subject subject = (Subject)o;
return Objects.equals(tutor_id, tutor.tutor_id)&&
Objects.equals(NRIC, tutor.NRIC) &&
Objects.equals(Name, tutor.Name) &&
Objects.equals(gender, tutor.gender) &&
Objects.equals(address1, tutor.address1) &&
Objects.equals(address2, tutor.address2) &&
Objects.equals(zipcode, tutor.zipcode) &&
Objects.equals(age, tutor.age) &&
Objects.equals(qualification, tutor.qualification) &&
Objects.equals(qualificationDetail, tutor.qualificationDetail) &&
Objects.equals(contactNo, tutor.contactNo) &&
Objects.equals(email, tutor.email) &&
Objects.equals(budget, tutor.budget) &&
Objects.equals(remark, tutor.remark);
// Objects.equals(subject, subject.getSubjMany());// should I have this ?
}
@Override
public String toString() {
return "Tutor [tutor_id=" + tutor_id + ", Name=" + Name + ", NRIC=" + NRIC + ", gender=" + gender
+ ", address1=" + address1 + ", address2=" + address2 + ", zipcode=" + zipcode + ", age=" + age
+ ", qualification=" + qualification + ", qualificationDetail=" + qualificationDetail + ", contactNo="
+ contactNo + ", email=" + email + ", budget=" + budget + ", remark=" + remark + ", subject=" + subjects
+ "]";
}
}
And the `Subject` looks like this:
```
@Entity (name = "Subject")
@Table(name = "subject")
public class Subject implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7607227309942205498L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int subject_id;
private String subject;
public Subject() {
// TODO Auto-generated constructor stub
}
public Subject(String subject){
this.subject = subject;
}
public String getSubject() {
return subject;
}
public void setSubject_id(int subject_id) {
this.subject_id = subject_id;
}
}
```