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.RollbackException: Error while commiting the
transaction
       at
org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
       at
com.csg.cs.pb.usa.hdt.model.test.QueryTest.testQuery2(QueryTest.java:78)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:39)
       at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at junit.framework.TestCase.runTest(TestCase.java:168)
       at junit.framework.TestCase.runBare(TestCase.java:134)
       at junit.framework.TestResult$1.protect(TestResult.java:110)
       at junit.framework.TestResult.runProtected(TestResult.java:128)
       at junit.framework.TestResult.run(TestResult.java:113)
       at junit.framework.TestCase.run(TestCase.java:124)
       at junit.framework.TestSuite.runTest(TestSuite.java:232)
       at junit.framework.TestSuite.run(TestSuite.java:227)
       at
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.jav
a:79)
       at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4Tes
tReference.java:38)
       at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.ja
va:38)
       at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:460)
       at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:673)
       at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRun
ner.java:386)
       at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRu
nner.java:196)
Caused by: org.hibernate.exception.ConstraintViolationException: Could
not execute JDBC batch update
       at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java
:94)
       at
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.
java:66)
       at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275
)
       at
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
       at
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
       at
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(
AbstractFlushingEventListener.java:321)
       at
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEv
entListener.java:50)
       at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
       at
org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
       at
org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:13
7)
       at
org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
       ... 20 more
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL
into ("PBDT"."PROJECT"."ORGANIZATION_ID")

       at
oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:459)
       at
oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedSt
atement.java:4133)
       at
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:7
0)
       at
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268

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();

Open in new window

rdevenderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ajay-SinghCommented:
You should set organization object on the project in order to link it,

  this.em = entityManagerFactory.createEntityManager();
   em.getTransaction().begin();
   Organization organization = new Organization(
       " The Apache Software");
   Project project = new Project("Streaming LOB support (for
OpenJPA)");
   project.setOrganizationId(organization);
   organization.addProject(project);

   em.getTransaction().commit();
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rdevenderAuthor Commented:
that worked..thanks alot.
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
0
Ajay-SinghCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.