hi all,
im having problems in inserting data in a join table using jpa.
im using eclipselink and mysql.
i have 3 tables:
tab1(pk(id),k1)
tab2(pk(id),k2)
tab3(fk(x)->tab1(id),fk(y)->tab2(id))
sa far i have done this.
this is tab1
this is tab2
and this is the servlet
i get this:-----
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`prim`.`tab3`, CONSTRAINT `tab3_ibfk_1` FOREIGN KEY (`x`) REFERENCES `tab1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Error Code: 1452
Call: INSERT INTO tab3 (x, y) VALUES (?, ?)
bind => [2 parameters bound]
Query: DataModifyQuery(name="tab1s" sql="INSERT INTO tab3 (x, y) VALUES (?, ?)")
org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
pak1.Srv1.doGet(Srv1.java:59)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
thank you
im having problems in inserting data in a join table using jpa.
im using eclipselink and mysql.
i have 3 tables:
tab1(pk(id),k1)
tab2(pk(id),k2)
tab3(fk(x)->tab1(id),fk(y)->tab2(id))
sa far i have done this.
this is tab1
package pak1; import java.io.Serializable; import javax.persistence.*; import java.util.List; /** * The persistent class for the tab1 database table. * */ @Entity public class Tab1 implements Serializable { private static final long serialVersionUID = 1L; @Id private int id; private int k1; //bi-directional many-to-many association to Tab2 @ManyToMany(mappedBy="tab1s") private List<Tab2> tab2s; public Tab1() { } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public int getK1() { return this.k1; } public void setK1(int k1) { this.k1 = k1; } public List<Tab2> getTab2s() { return this.tab2s; } public void setTab2s(List<Tab2> tab2s) { this.tab2s = tab2s; } }
this is tab2
package pak1; import java.io.Serializable; import javax.persistence.*; import java.util.List; /** * The persistent class for the tab2 database table. * */ @Entity public class Tab2 implements Serializable { private static final long serialVersionUID = 1L; @Id private int id; private int k2; //bi-directional many-to-many association to Tab1 @ManyToMany @JoinTable( name="tab3" , joinColumns={ @JoinColumn(name="y") } , inverseJoinColumns={ @JoinColumn(name="x") } ) private List<Tab1> tab1s; public Tab2() { } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public int getK2() { return this.k2; } public void setK2(int k2) { this.k2 = k2; } public List<Tab1> getTab1s() { return this.tab1s; } public void setTab1s(List<Tab1> tab1s) { this.tab1s = tab1s; } }
and this is the servlet
package pak1; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Srv1 */ @WebServlet("/Srv1") public class Srv1 extends HttpServlet { private static final long serialVersionUID = 1L; static EntityManagerFactory emf = Persistence.createEntityManagerFactory("aaa"); static EntityManager em = emf.createEntityManager(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out=response.getWriter(); List<Tab2> l2=new ArrayList<>(); List<Tab1> l1=new ArrayList<>(); em.getTransaction().begin(); Tab1 t1=new Tab1(); t1.setK1(12); Tab1 t11=new Tab1(); t11.setK1(12); l1.add(t1); l1.add(t11); em.persist(t1); em.persist(t11); Tab2 t2=new Tab2(); t2.setK2(12); Tab2 t22=new Tab2(); t22.setK2(123); t22.setTab1s(l1); em.persist(t2); em.persist(t22); em.getTransaction().commit(); em.close(); emf.close(); out.println("fund"); } }
i get this:-----
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`prim`.`tab3`, CONSTRAINT `tab3_ibfk_1` FOREIGN KEY (`x`) REFERENCES `tab1` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Error Code: 1452
Call: INSERT INTO tab3 (x, y) VALUES (?, ?)
bind => [2 parameters bound]
Query: DataModifyQuery(name="tab1s" sql="INSERT INTO tab3 (x, y) VALUES (?, ?)")
org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
pak1.Srv1.doGet(Srv1.java:59)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
thank you