我遇到了以下问题:
每次我尝试创建一个bloco,eclipselink尝试插入一个condominio记录(应该为null),当我在一个简单的java项目中运行相同的代码(没有spring)它工作正常,但在web项目中它一直试图插入"父母"表
为什么eclipselink试图强制建立双向关系?
实体
Condominio
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idcondominio")
private Integer idcondominio;
@Basic(optional = false)
@Column(name = "nomecond")
private String nomecond;
@Column(name = "cnpjcond")
private Integer cnpjcond;
@OneToMany(mappedBy = "condominio")
private Collection<Bloco> blocoCollection;
Bloco
private static final long serialVersionUID = 1L;
@EmbeddedId
protected BlocoPK blocoPK;
@Basic(optional = false)
@Column(name = "nomebloco")
private String nomebloco;
@Column(name = "numero")
private String numero;
@JoinColumn(name = "condominio_idcondominio", referencedColumnName = "idcondominio", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Condominio condominio;
============================== 创建方法
public void create(Bloco bloco) throws PreexistingEntityException, Exception {
if (bloco.getBlocoPK() == null) {
bloco.setBlocoPK(new BlocoPK());
}
bloco.getBlocoPK().setCondominioIdcondominio(bloco.getCondominio().getIdcondominio());
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Condominio condominio = bloco.getCondominio();
if (condominio != null) {
condominio = em.getReference(condominio.getClass(), condominio.getIdcondominio());
bloco.setCondominio(condominio);
}
em.persist(bloco);
if (condominio != null) {
condominio.getBlocoCollection().add(bloco);
condominio = em.merge(condominio);
}
em.getTransaction().commit();
} catch (Exception ex) {
if (findBloco(bloco.getBlocoPK()) != null) {
throw new PreexistingEntityException("Bloco " + bloco + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
========================= 控制器方法
@RequestMapping(value = "cadbloco", method = RequestMethod.POST)
public String cadbloco(@ModelAttribute(value = "Bloco") Bloco bloco, @RequestParam("condid") int idcond ) throws Exception {
BlocoJpaController jpa = new BlocoJpaController();
Condominio c = new Condominio();
c.setIdcondominio(idcond);
jpa.create(bloco);
return ("redirect:bloco.htm");
}
=====================
消息请求处理失败;嵌套异常是javax.persistence.RollbackException:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.DatabaseException
说明服务器遇到内部错误,导致无法完成此请求。
例外
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是javax.persistence.RollbackException:Exception [EclipseLink-4002](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.DatabaseException 内部异常:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Column&#39; nomecond&#39;不能为空 错误代码:1048 调用:INSERT INTO condominio(cnpjcond,nomecond)VALUES(?,?) bind =&gt; [2个参数绑定] 查询:InsertObjectQuery(Entity.Condominio [idcondominio = null]) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) javax.servlet.http.HttpServlet.service(HttpServlet.java:648) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
答案 0 :(得分:0)
异常说“nomecond”值为null。所以在你的控制器中如果你添加了代码
c.setNomeCond(“Some value”);
这应解决问题。
答案 1 :(得分:0)
这是阙右方法,我没有在bloco中设置condominio,所以当我试图创建一个新的bloco它没有condominio。
我对这种事情不熟悉,有时会犯这样的错误,但感谢你的帮助。
=============================================== ===================
@RequestMapping(value =&#34; cadbloco&#34;,method = RequestMethod.POST) public String cadbloco(@ModelAttribute(value =&#34; Bloco&#34;)Bloco bloco,@ RequestParam(&#34; condid&#34;)int idcond)抛出异常{
BlocoJpaController jpa = new BlocoJpaController();
Condominio c = new Condominio();
c.setIdcondominio(idcond);
bloco.setCondominio(c);
jpa.create(bloco);
return ("redirect:bloco.htm");
}