2 Replies Latest reply on May 22, 2008 1:57 PM by Bhaskar P

    ManyToMany throws exception

    Bhaskar P Newbie

      Hi there!


      I have a ManyToMany relation between ClassA and ClassB, defined in the following way:



      ClassA
      
      @Id @GeneratedValue
      @Column(name="id")
      private Integer id;
      
      @Column(name="prod_name")
      private String pName;
      @ManyToMany(fetch = FetchType.EAGER,cascade = CascadeType.PERSIST,
                targetEntity=com.myTest.ClassB.class)
      @JoinTable(name="a_b_relation",
              joinColumns={@JoinColumn(name="a_id")},
              inverseJoinColumns={@JoinColumn(name="b_id")})
      @Fetch(value=FetchMode.SELECT)
      private List<ClassB> bList = new ArrayList<ClassB>(0);
      
      .... constructor and getter/setter




      ClassB
      @Id @GeneratedValue
      @Column(name="id")
      private Integer id;
      @Column(name="prod_price")
      private String prodPrice;
      
      ... constructor and getter/setter



      when I try to persist ClassA I am getting following exception


      Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails 
      at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1237)
      at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:936)
      at org.jboss.resource.adapter.jdbc.WrappedStatement.executeBatch(WrappedStatement.java:519)
      at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
      at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
      ...


      Any Help!!


      Thanks
      Pankaj