hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails

一篇文章里边有多张图片,典型的单向一对多关系

hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails

多方

hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails

当程序运行到这一句的时候必然报错

hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails

但是参考书也是这样写的

hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails

其中em是

 EntityManager em = JPA.createEntityManager();

我本就是为了省事儿,采用的hibernate,结果你又让我用JPA这不是扯淡吗???/

经过两天的无脑研究

我发现一对多的情况,如果一方来控制,那么多方不能在实体中创建外键

也就是说,要这样做

hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.hs.model; import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator; /**
*
* @author wishr
*/
@Entity
@Table(name = "t_imgs")
public class PreviewImg implements Serializable { private int id;
private String url; @Id
@GeneratedValue(generator = "_native")
@GenericGenerator(name = "_native", strategy = "native")
public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
} }

多方代码

然后在一方进行保存的时候,多方自动保存,并且,多方的表,会自动增加一个属性,就是这个外键

hibernate4一对多关联多方多写一次外键导致无法创建java.lang.NullPointerException以及Cannot add or update a child row: a foreign key constraint fails

service代码如下

public void captureAndSaveByZcjId(JTextArea jtaPreview, JTextArea jtaLog, int zcjId) {
ArticleDao articleDao = new ArticleDao();
Configuration cfg = new Configuration().configure();//实例化配置文件
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
SessionFactory sf = cfg.buildSessionFactory(sr);//获取session工厂
Session session = sf.openSession();//生成一个新的session
session.beginTransaction();//开启事务
ZcjArticle zcjArticle = this.captureById(jtaPreview, jtaLog, zcjId);//远程文章大于本地文章才去刷
Article article = new Article();
article.setOldId(zcjArticle.getId());
article.setZcjPlateId(zcjArticle.getPlateId());
article.setColumnId(1);
article.setSectionId(1);
// int id = articleDao.save(session, article);//取回刚插入的数据的id
PreviewImg pi = new PreviewImg();
pi.setUrl("不为空就行");//测试用,随便写的
PreviewimgDao pd = new PreviewimgDao();
article.getPreviewImgs().add(pi);
articleDao.save(session, article);
session.getTransaction().commit(); //提交事务
session.close();//关闭session
sf.close();//关闭session工厂
}

为啥会出现这个错误,因为在一对一配置中,我也在被控的一方,写了一个外键,但是他并没有报错,大意

上一篇:insert时报Cannot add or update a child row: a foreign key constraint fails (`yanchangzichan`.`productstatusrecord`, CONSTRAINT `p_cu` FOREIGN KEY (`cid`) REFERENCES `customer` (`cid`))错误


下一篇:MySql数据库插入或更新报错:Cannot add or update a child row: a foreign key constraint fails