rperez
rperez

Reputation: 8520

Update table entry with unique constraints - Hibernate

I am new to Hibernate and have the following situation:

class A

class Course extends A (course name is unique)

class Students (holds a set of courses)

My Goal is to be able to add new students to the database that attend existing courses. I do not even need to update the existing course, but don't want to fail the save operation.

I am using session saveOrUpdate (cascade=ALL) and while all saveOrUpdates occur before flush - all is OK. when I rerun my main, I get an error:

SQL Error: 1062, SQLState: 23000

Duplicate entry 'History' for key 'COURSE_NAME'

Bellow is a snippet of the code. any help appreciated.

class A:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="TYPE")
@Table(name="A")
public class A
{
    private long aId;

    private String info;

    public A(String info)
    {
        this.info = info;
    }

    @Id
    @GeneratedValue
    @Column(name="A_ID")
    public long getAId()
    {
        return aId;
    }

    public void setAId(long aId)
    {
        this.aId = aId;
    }

    //@Column(name="INFO", nullable=false, insertable=false, updatable=false)
    @Column(name="INFO")
    public String getInfo()
    {
        return info;
    }

    public void setInfo(String info)
    {
        this.info = info;
    }
}

class course:

@Entity
@DiscriminatorValue("COURSE")
@Table(name="COURSE", uniqueConstraints = {@UniqueConstraint(columnNames={"COURSE_NAME"})})
public class Course extends A{

    private String courseName;

    public Course() {
        super("initial");
    }

    public Course(String courseName, String info) 
    {
        super(info);
        this.courseName = courseName;
    }

    @Column(name="COURSE_NAME")
    public String getCourseName() {
        return this.courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

}

And class Student that holds a list of Coreses:

@Entity
@Table(name = "STUDENT")
public class Student
{

    private long        studentId;

    private String      studentName;
    private String      additionalInfo;

    private Set<Course> courses = new HashSet<Course>(0);

    @Column(name = "ADDITIONAL_INFO")
    public String getAdditionalInfo()
    {
        return additionalInfo;
    }

    public void setAdditionalInfo(String additionalInfo)...

    public Student()
    {
    }

    public Student(String studentName, String info, Set<Course> courses)
    {
        this.studentName = studentName;
        this.courses = courses;
        this.additionalInfo = info;
    }

    @Id
    @GeneratedValue
    @Column(name = "STUDENT_ID", unique=true)
    public long getStudentId()
    {
        return this.studentId;
    }

    public void setStudentId(long studentId)...

    @Column(name = "STUDENT_NAME", nullable = false, length = 100)
    public String getStudentName()
    {
        return this.studentName;
    }

    public void setStudentName(String studentName)...

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "aId")
    public Set<Course> getCourses()
    {
        return this.courses;
    }

    public void setCourses(Set<Course> courses)
    {
        this.courses = courses;
    }
}

Upvotes: 0

Views: 2673

Answers (1)

Satadru Biswas
Satadru Biswas

Reputation: 1595

Its not exactly possible to tell without seeing the code. However, I suspect that you are creating a new object with course name "History" and adding it to the new student object. What you should be doing is fetch the course object with courseName = "History" from the database and then set this in the new student object.

Upvotes: 1

Related Questions