0

I have 2 entity class called Student and University and relationship between student and university is Many-to-one which means many students can study in one university. My input jsp page basically will ask the university name and student email address to register.

Here is the flow. I enter following in my input page -

university1, student1 - This will add university1 and student1 in my database.

When I enter -

university1, student2 - My intention here is to first find whether university1 is already present in my database. If it is then associate the same university to student object and try to save it. But when I am doing that hibernate is trying to add one more university object in the database and I am getting following exception -

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1' for key 'university_id'.

Here are my entity classes -

@Entity
@Table(name="students")
public class Student implements Serializable{

private Integer studentId;
private University university;


@Id
@GeneratedValue
@Column(name = "student_id")
public Integer getStudentId() {
    return studentId;
}

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "university_id")
public University getUniversity() {
    return university;
}

}

@Entity
@Table(name="university")
public class University implements Serializable {

private Integer universityId;

@Id
@GeneratedValue
@Column(name = "university_id")
public Integer getUniversityId() {
    return university;
}

public void setCompanyId(Integer companyId) {
    this.companyId = companyId;
}

@Column(name = "university_name", unique = true, nullable = false)
public String getUniversityName() {
    return university;
}

}

Here is the simple Java Code.

University university =   universityService.getUniversityByName(student.getUniversity().getUniversityName());
if(university != null) {
student.setUniversity(university);
}
getHibernateTemplate().saveOrUpdate(student);

I am using Spring MVC and database schema is getting generated by Hibernate. Looks like I am making some very basic mistake.

user977263
  • 45
  • 1
  • 8
  • Do not bother, I found the solution by myself. university_name attribute shouldn't have 'unique' attribute. I removed it and now everything is working fine. – user977263 Jul 01 '12 at 05:45
  • 1
    I doubt this is the solution. Now you probably create several universities with the same name, but don't notice it. Each student probably has its own university. The unique constraint was a good thing: it prevented two universities from having the same name. – JB Nizet Jul 01 '12 at 07:23
  • So what is the solution then? – user977263 Jul 02 '12 at 08:12
  • Try putting back the unique attribute, and see if it still works. Maybe you just had a deployment problem. Your code looks fine to me. – JB Nizet Jul 02 '12 at 08:20

1 Answers1

0

hibernate won't issue queries to identify if there are already other records with the same unique column values in the database, you have to do it yourself.

List<University> unis = session.createCriteria(University.class)
    .add(<Restrictions.eq("name", name)
    .list<University>()

University u;
if (unis.getCount() == 0)
{
    u = new University();
    ...
}
else
{
    u = unis.get(0);
}
Firo
  • 30,626
  • 4
  • 55
  • 94
  • But I am already doing that in my java code. universityService.getUniversityByName() API will find the university by university name and then I am assigning it to student if it exits. So what you suggested is already taken care of. – user977263 Jul 02 '12 at 08:12
  • then do you use a different session to search for the uni and to save the new student? – Firo Jul 02 '12 at 08:31
  • No, why shouldn't I be using a different session? – user977263 Jul 02 '12 at 19:07
  • where do you get the University which is already set on the student here `student.getUniversity().getUniversityName()`? Maybe you set some new object with id = 1 and didn't find a uni in db then you try to save it and bang? – Firo Jul 03 '12 at 05:51