问题描述
我有2个对象实体(用户和电话),他们应该有多对多的关系。 User.java
/ /所有列
@ManyToMany(cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
@JoinTable(name =USER_PHONE,
joinColumns = @JoinColumn(name = user_id,referencedColumnName =id),
inverseJoinColumns = @JoinColumn(name =phone_id,referencedColumnName =id))
private List< Phone>手机;
Phone.java
//所有列
@ManyToMany(cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
@JoinTable(name =USER_PHONE,
joinColumns = @JoinColumn(name =user_id,referencedColumnName =id))
private List<使用者名称>用户列表;
现在,我在USER表中添加2个ID为1和2的用户。
然后,我添加一个ID为1的电话,并将它们映射到用户ID(1& 2)。
我的USER_PHONE表格如下所示:
从USER_PHONE中选择* ;
+ ---------- + --------- +
| phone_id | user_id |
+ ---------- + --------- +
| 1 | 1 |
| 1 | 2 |
+ ---------- + --------- +
现在,我想删除一个ID为2的用户。
当我尝试这样做时,出现错误
javax.persistence.PersistenceException:org.hibernate.exception.ConstraintViolationException:无法删除或更新父行:外键约束失败(`dbname`.`USER_PHONE`,CONSTRAINT`FKC6A847DAFA96A429` FOREIGN KEY(` user_id`)参考````````
我的删除脚本:
String query =DELETE User where id =?1;
try {
Query q = entityManager.createQuery(query);
q.setParameter(1,id);
q.executeUpdate();
System.out.println(System.currentTimeMillis()+DELETE:userId+ id +==> deleted);
} catch(Exception e){
e.printStackTrace();
返回false;
}
任何想法我哪里错了?
非常感谢:)
尝试使用 entityManager.createNativeQuery()
。您不能使用 createQuery()
,因为表应该作为Java代码中的一个实体出现。此外,您需要使用确切的SQL格式。
String query =DELETE FROM USER_PHONE WHERE user_id =?1;
$ pre $ {
Query q = entityManager.createNativeQuery(query);
q.setParameter(1,id);
q.executeUpdate();
System.out.println(System.currentTimeMillis()+DELETE User_Phone:userId+ id +==> deleted);
} catch(Exception e){
e.printStackTrace();
返回false;
$ / code> c $ c>(使用 createNativeQuery()
),然后从 User
(使用 createQuery ()
)
I have 2 object entities (User and Phone) and they are supposed to have many-to-many relations.
User.java
//all columns
@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
@JoinTable(name = "USER_PHONE",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "phone_id", referencedColumnName = "id"))
private List<Phone> phones;
Phone.java
//all columns
@ManyToMany(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
@JoinTable(name = "USER_PHONE",
joinColumns = @JoinColumn(name = "phone_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private List<User> userList;
Now, I add 2 users with IDs 1 and 2 in my USER table.Then, I add a single phone with id 1 and map them to both the user IDs(1&2) .
My USER_PHONE table looks as below:
Select * from USER_PHONE;
+----------+---------+
| phone_id | user_id |
+----------+---------+
| 1 | 1 |
| 1 | 2 |
+----------+---------+
Now, I wish to remove a user with ID 2.When I try to do this, I get an error
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`dbname`.`USER_PHONE`, CONSTRAINT `FKC6A847DAFA96A429` FOREIGN KEY (`user_id`) REFERENCES `USER` (`ID`))
My delete script:
String query = "DELETE User where id=?1";
try{
Query q = entityManager.createQuery(query);
q.setParameter(1,id);
q.executeUpdate();
System.out.println(System.currentTimeMillis() + " DELETE: userId " + id + " ==> deleted");
} catch(Exception e){
e.printStackTrace();
return false;
}
Any idea where am I going wrong ?Thanks a lot :)
Try using entityManager.createNativeQuery()
. You cannot use createQuery()
because the table should be present as an entity in your Java code. Also, you need to use the exact SQL format.
String query = "DELETE FROM USER_PHONE WHERE user_id=?1";
try{
Query q = entityManager.createNativeQuery(query);
q.setParameter(1,id);
q.executeUpdate();
System.out.println(System.currentTimeMillis() + " DELETE User_Phone: userId " + id + " ==> deleted");
} catch(Exception e){
e.printStackTrace();
return false;
}`
First delete the row from USER_PHONE
(using createNativeQuery()
), and then from User
(using createQuery()
)
这篇关于无法删除或更新父行ConstraintViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!