3

I have two classes: Employee and Award, with a many-to-many relationship.

When trying to delete I get a constraint violation error. I went through all the posts but none were helpful.

This is the code:

@Entity
@Table(name="TB_AWARD")
public class Award implements Serializable{


    @Id @GeneratedValue
    @Column(name="AWARD_ID")
    private long awardId;

    @ManyToMany(mappedBy="awards")
    @NotFound(action=NotFoundAction.IGNORE)
    private Collection<Employee> employee = new ArrayList<Employee>();

    @Column(name="AWARD_TYPE")
    private String awardType;

    @Column(name="AWARD_DATE")
    private Date awardDate;

    @Column(name="AWARD_DETAILS")
    @Lob
    private String awardDetails;

    @Column(name="REMARK")
    private String remark;

@Entity
@Table(name="TB_EMPLOYEE")
public class Employee implements Serializable {

    @Id @GeneratedValue
    @Column(name="EMPLOYEE_ID")
    private long employeeID;

        @ManyToMany(cascade= CascadeType.ALL)
    private Collection<Award> awards;
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Chetan Pulate
  • 503
  • 1
  • 7
  • 21

2 Answers2

1

You're mapping it wrong, try it like this:

@Entity
@Table(name="TB_AWARD")
public class Award implements Serializable{


    @Id @GeneratedValue
    @Column(name="AWARD_ID")
    private long awardId;

    @ManyToMany(targetEntity=Employee.class, cascade = CascadeType.ALL)
    @JoinTable(name = "AWARD_EMPLOYEE", joinColumns = { @JoinColumn(name ="AWARD_ID")},
    inverseJoinColumns = { @JoinColumn(name = "EMPLOYEE_ID") })
    @NotFound(action=NotFoundAction.IGNORE)
    private Collection<Employee> employees = new ArrayList<Employee>();

    @Column(name="AWARD_TYPE")
    private String awardType;

    @Column(name="AWARD_DATE")
    private Date awardDate;

    @Column(name="AWARD_DETAILS")
    @Lob
    private String awardDetails;

    @Column(name="REMARK")
    private String remark;

@Entity
@Table(name="TB_EMPLOYEE")
public class Employee implements Serializable {

    @Id @GeneratedValue
    @Column(name="EMPLOYEE_ID")
    private long employeeID;

    @ManyToMany(
    cascade = CascadeType.ALL,
    mappedBy = "employees",
    targetEntity = Award.class
    )
    private Collection<Award> awards; //create the getter for this guy

Always look at the examples in the reference before, it will make your life so much easier. ;)

Tiago Farias
  • 3,397
  • 1
  • 27
  • 30
0

You get it from the database and not from hibernate. When hibernate deletes, it first nullifies the reference. Remove the database constraint. You should also turn on hibernate sql logging or see sql in database profiler to see what hibernate does. You will see there the sql that causes the constraint violation.

Tiago Farias
  • 3,397
  • 1
  • 27
  • 30
Avihai Marchiano
  • 3,837
  • 3
  • 38
  • 55
  • buddy i am a begginner in hibernate this is the error i get com.microsoft.sqlserver.jdbc.SQLServerException: The DELETE statement conflicted with the REFERENCE constraint "FK5759580C246A5898". The conflict occurred in database "HRIS_DATABASE", table "dbo.TB_EMPLOYEE_TB_AWARD", column 'awards_AWARD_ID'. – Chetan Pulate Jul 26 '12 at 19:37
  • this is not hibernate exception. see the error prefix . it the database exception. . you have FK that dosnt allow null. when you delete. hibernate first set null and than delete. for first remove the FK and see if it work. – Avihai Marchiano Jul 26 '12 at 19:41
  • 1
    The error comes from your db, sure. But I think he was asking for a workaround without modifying the db manually, instead changing some mapping annotation or something. – Tiago Farias Jul 26 '12 at 21:12