rdevender
asked on
how do i create a one to many (hibernate) relationship using jpa
I'm trying to persist data into 2 tables which has one to many relationship
Child table has foreign key relationship with the parent table
I'm getting the following exception
avax.persistence.RollbackE xception: Error while commiting the
transaction
at
org.hibernate.ejb.Transact ionImpl.co mmit(Trans actionImpl .java:71)
at
com.csg.cs.pb.usa.hdt.mode l.test.Que ryTest.tes tQuery2(Qu eryTest.ja va:78)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at
sun.reflect.NativeMethodAc cessorImpl .invoke(Na tiveMethod AccessorIm pl.jav
a:39)
at
sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessor
Impl.java:25)
at java.lang.reflect.Method.i nvoke(Meth od.java:58 5)
at junit.framework.TestCase.r unTest(Tes tCase.java :168)
at junit.framework.TestCase.r unBare(Tes tCase.java :134)
at junit.framework.TestResult $1.protect (TestResul t.java:110 )
at junit.framework.TestResult .runProtec ted(TestRe sult.java: 128)
at junit.framework.TestResult .run(TestR esult.java :113)
at junit.framework.TestCase.r un(TestCas e.java:124 )
at junit.framework.TestSuite. runTest(Te stSuite.ja va:232)
at junit.framework.TestSuite. run(TestSu ite.java:2 27)
at
org.junit.internal.runners .JUnit38Cl assRunner. run(JUnit3 8ClassRunn er.jav
a:79)
at
org.eclipse.jdt.internal.j unit4.runn er.JUnit4T estReferen ce.run(JUn it4Tes
tReference.java:38)
at
org.eclipse.jdt.internal.j unit.runne r.TestExec ution.run( TestExecut ion.ja
va:38)
at
org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.r unTests(Re moteTe
stRunner.java:460)
at
org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.r unTests(Re moteTe
stRunner.java:673)
at
org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.r un(RemoteT estRun
ner.java:386)
at
org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.m ain(Remote TestRu
nner.java:196)
Caused by: org.hibernate.exception.Co nstraintVi olationExc eption: Could
not execute JDBC batch update
at
org.hibernate.exception.SQ LStateConv erter.conv ert(SQLSta teConverte r.java
:94)
at
org.hibernate.exception.JD BCExceptio nHelper.co nvert(JDBC ExceptionH elper.
java:66)
at
org.hibernate.jdbc.Abstrac tBatcher.e xecuteBatc h(Abstract Batcher.ja va:275
)
at
org.hibernate.engine.Actio nQueue.exe cuteAction s(ActionQu eue.java:2 66)
at
org.hibernate.engine.Actio nQueue.exe cuteAction s(ActionQu eue.java:1 67)
at
org.hibernate.event.def.Ab stractFlus hingEventL istener.pe rformExecu tions(
AbstractFlushingEventListe ner.java:3 21)
at
org.hibernate.event.def.De faultFlush EventListe ner.onFlus h(DefaultF lushEv
entListener.java:50)
at org.hibernate.impl.Session Impl.flush (SessionIm pl.java:10 27)
at
org.hibernate.impl.Session Impl.manag edFlush(Se ssionImpl. java:365)
at
org.hibernate.transaction. JDBCTransa ction.comm it(JDBCTra nsaction.j ava:13
7)
at
org.hibernate.ejb.Transact ionImpl.co mmit(Trans actionImpl .java:54)
... 20 more
Caused by: java.sql.BatchUpdateExcept ion: ORA-01400: cannot insert NULL
into ("PBDT"."PROJECT"."ORGANIZ ATION_ID")
at
oracle.jdbc.dbaccess.DBErr or.throwBa tchUpdateE xception(D BError.jav a:459)
at
oracle.jdbc.driver.OracleP reparedSta tement.exe cuteBatch( OraclePrep aredSt
atement.java:4133)
at
org.hibernate.jdbc.Batchin gBatcher.d oExecuteBa tch(Batchi ngBatcher. java:7
0)
at
org.hibernate.jdbc.Abstrac tBatcher.e xecuteBatc h(Abstract Batcher.ja va:268
Child table has foreign key relationship with the parent table
I'm getting the following exception
avax.persistence.RollbackE
transaction
at
org.hibernate.ejb.Transact
at
com.csg.cs.pb.usa.hdt.mode
at sun.reflect.NativeMethodAc
at
sun.reflect.NativeMethodAc
a:39)
at
sun.reflect.DelegatingMeth
Impl.java:25)
at java.lang.reflect.Method.i
at junit.framework.TestCase.r
at junit.framework.TestCase.r
at junit.framework.TestResult
at junit.framework.TestResult
at junit.framework.TestResult
at junit.framework.TestCase.r
at junit.framework.TestSuite.
at junit.framework.TestSuite.
at
org.junit.internal.runners
a:79)
at
org.eclipse.jdt.internal.j
tReference.java:38)
at
org.eclipse.jdt.internal.j
va:38)
at
org.eclipse.jdt.internal.j
stRunner.java:460)
at
org.eclipse.jdt.internal.j
stRunner.java:673)
at
org.eclipse.jdt.internal.j
ner.java:386)
at
org.eclipse.jdt.internal.j
nner.java:196)
Caused by: org.hibernate.exception.Co
not execute JDBC batch update
at
org.hibernate.exception.SQ
:94)
at
org.hibernate.exception.JD
java:66)
at
org.hibernate.jdbc.Abstrac
)
at
org.hibernate.engine.Actio
at
org.hibernate.engine.Actio
at
org.hibernate.event.def.Ab
AbstractFlushingEventListe
at
org.hibernate.event.def.De
entListener.java:50)
at org.hibernate.impl.Session
at
org.hibernate.impl.Session
at
org.hibernate.transaction.
7)
at
org.hibernate.ejb.Transact
... 20 more
Caused by: java.sql.BatchUpdateExcept
into ("PBDT"."PROJECT"."ORGANIZ
at
oracle.jdbc.dbaccess.DBErr
at
oracle.jdbc.driver.OracleP
atement.java:4133)
at
org.hibernate.jdbc.Batchin
0)
at
org.hibernate.jdbc.Abstrac
package test.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "ORGANIZATION")
@SequenceGenerator(name = "orgSeq", sequenceName = "HDT_DEAL_SEQ")
public class Organization implements Serializable {
@Id
@Column(name="ORGANIZATION_ID")
@GeneratedValue(generator = "orgSeq", strategy =
GenerationType.SEQUENCE)
private Integer id;
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "organization")
private List<Project> projectCollection;
private static final long serialVersionUID = 1L;
public Organization() {
super();
}
public Organization(String string) {
this.name=string;
}
public Integer getId() {
return this.id;
}
public void setId(Integer organizationId) {
this.id = organizationId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public List<Project> getProjectCollection() {
return this.projectCollection;
}
/**
* add a new project
* @param project
*/
public void addProject(Project project){
if(projectCollection == null) projectCollection = new
ArrayList<Project> ();
//liability.setPayer(this);
projectCollection.add(project);
}
public void setProjectCollection(List<Project>
projectCollection) {
this.projectCollection = projectCollection;
}
}
package test.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "PROJECT")
@SequenceGenerator(name = "projSeq", sequenceName = "HDT_SEQ")
public class Project implements Serializable {
@Id
@Column(name="PROJECT_ID")
@GeneratedValue(generator = "projSeq", strategy =
GenerationType.SEQUENCE)
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name="ORGANIZATION_ID")
private Organization organization;
private static final long serialVersionUID = 1L;
public Project() {
super();
}
public Project(String string) {
this.name=string;
}
public Integer getId() {
return this.id;
}
public void setId(Integer projectId) {
this.id = projectId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Organization getOrganization() {
return this.organization;
}
public void setOrganizationId(Organization organization) {
this.organization = organization;
}
}
//This is the test method
public void testQuery2() {
this.em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
Organization organization = new Organization(
" The Apache Software");
List<Project> projectCollection = new ArrayList<Project>();
Project project = new Project("Streaming LOB support (for
OpenJPA)");
organization.addProject(project);
// projectCollection.add(project);
// project = new Project("Maven Dependency Visualization");
//projectCollection.add(project);
//organization.setProjectCollection(projectCollection);
// organization.getProjectCollection().addAll(projectCollection);
//organization.getProjectCollection.(projectCollection);
em.persist(organization);
em.getTransaction().commit();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try using composite-id,
section (5.1.7. composite-id) of
http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html
There is a property key-many-to-one for mapping another object as a part
of composite key.
section (5.1.7. composite-id) of
http://docs.jboss.org/hibernate/stable/core/reference/en/html/mapping.html
There is a property key-many-to-one for mapping another object as a part
of composite key.
ASKER
I have another issue..
Child table has compound primary key and parent foreign key is part of the primary key.
Can you help me out in this case
Thanks in advance