I am stuck with the following problem:
everytime i try to create a bloco, eclipselink tries to insert a condominio record (which should be null ) , when i run the same code in a simple java project (without spring) it works fine, but in the web project it keeps trying to insert in the "parent" table.
any reason why eclipselink is trying to force a bidirectional relationship?
Entities
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;
============================== Create method
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();
}
}
}
========================= Controller Method
@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");
}
=====================
message Request processing failed; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'nomecond' cannot be null Error Code: 1048 Call: INSERT INTO condominio (cnpjcond, nomecond) VALUES (?, ?) bind => [2 parameters bound] Query: 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)
Here is que right method, i didnt set the condominio in bloco, so when i tried to create a new bloco it didnt have a condominio.
I new to this kind of stuff i make those kind of mistakes sometimes, but thanks for the help.
==================================================================
@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);
bloco.setCondominio(c);
jpa.create(bloco);
return ("redirect:bloco.htm");
}