Hello Everyone,
I got the following situation:
Two tables depends one-another (cyclic depency), in database wise that's perfect possible - in my case using
Deferrable Initially Deferred for
Postgres, meaning that
foreign constraints are checked just after transaction finishes (by default it's checked right after every single statement, even if they belong to the same transaction)
So, that works over database, but when i bring this concept to
Hibernate it does not work, an exception raises(See
Full stack trace of any exception that occurs).
What should i do guys?
Thanks in advance,
Jander Nascimento
Hibernate version:
Implementation-Title: Hibernate3
Implementation-Version: 3.2.4.sp1
Implementation-Vendor: hibernate.org
Hibernate-Version: 3.2.4.sp1
Come across: JTA Data Source
Seam version:
Implementation-Version: 2.1.1.GA
Mapping documents:
Code:
@Entity
@Table(name = "t1", schema = "temp")
public class T1 implements java.io.Serializable {
private int id;
private T2 t2;
private Set<T2> t2s = new HashSet<T2>(0);
public T1() {
}
public T1(int id, T2 t2) {
this.id = id;
this.t2 = t2;
}
public T1(int id, T2 t2, Set<T2> t2s) {
this.id = id;
this.t2 = t2;
this.t2s = t2s;
}
@Id
@Column(name = "id", unique = true, nullable = false)
@NotNull
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_t2", nullable = false)
@NotNull
public T2 getT2() {
return this.t2;
}
public void setT2(T2 t2) {
this.t2 = t2;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "t1")
public Set<T2> getT2s() {
return this.t2s;
}
public void setT2s(Set<T2> t2s) {
this.t2s = t2s;
}[/b]
}
Code:
@Entity
@Table(name = "t2", schema = "temp")
public class T2 implements java.io.Serializable {
private int id;
private T1 t1;
private int idT1;
private Set<T1> t1s = new HashSet<T1>(0);
public T2() {
}
public T2(int id, T1 t1, int idT1) {
this.id = id;
this.t1 = t1;
this.idT1 = idT1;
}
public T2(int id, T1 t1, int idT1, Set<T1> t1s) {
this.id = id;
this.t1 = t1;
this.idT1 = idT1;
this.t1s = t1s;
}
@Id
@Column(name = "id", unique = true, nullable = false)
@NotNull
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id", unique = true, nullable = false, insertable = false, updatable = false)
@NotNull
public T1 getT1() {
return this.t1;
}
public void setT1(T1 t1) {
this.t1 = t1;
}
@Column(name = "id_t1", nullable = false)
@NotNull
public int getIdT1() {
return this.idT1;
}
public void setIdT1(int idT1) {
this.idT1 = idT1;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "t2")
public Set<T1> getT1s() {
return this.t1s;
}
public void setT1s(Set<T1> t1s) {
this.t1s = t1s;
}
}
Called method for persist
Code:
...
public void callMe(){
T1 t1=new T1();
//Static ID - so it does not depends on database sequence(test purpose)
t1.setId(1);
T2 t2=new T2();
//Static ID - so it does not depends on database sequence(test purpose)
t2.setId(1);
t1.setT2(t2);
t2.setT1(t1);
entityManager.persist(t1);
entityManager.persist(t2);
entityManager.flush();
}
...
Full stack trace of any exception that occurs:
13:34:23,083 FATAL [application] javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: br.gov.exercito.silog.entitybean.classeV.T1.t2
javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: br.gov.exercito.silog.entitybean.classeV.T1.t2
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
at javax.faces.component.UICommand.broadcast(UICommand.java:383)
at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:321)
at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:296)
at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:253)
at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:466)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Name and version of the database you are using:
Postgres SQL 8.2