-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: @OneToMany between parent to child composite primary key
PostPosted: Fri Nov 19, 2010 4:43 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
Hi,

I have a one to many relationship between two classes PARENT and CHILD. The relationship is between the parent primary key (autogenerated oracle sequence when persisted) to one of the member of composite primary key of the child

Parent Child
------- -------
parentId ------------>|parentId | ----> Composite primary key of child (parentId + child_dept_key)
Name -|child_dept_key|
-dept-name

And my entity mappings are defined as below.

Here while persisting, the parentId on the parent when created, should be automatically mapped to the child entity and a record in the child should be created.

Code:
@Entity
@Table(name="PARENT")
@SequenceGenerator(name = "parent-seq", sequenceName = "PARENT_SEQUENCE")
class Parent{
     @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "parent-seq")
    @Column(name="PARENT_ID")
    Long id;

    @Column(name="PARENT_NAME")
    String name;

    @OneToMany(mappedBy = "childCompositePK.parent", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    @JoinColumn(name="PARENT_ID", nullable= false, updatable=false)
    private List<Child> childList;
}

@Entity
@Table(name="CHILD")
Class Child{
   @EmbeddedId
   ChildPK childCompositePK;

   @Column(name="DEPARTMENT_NAME")
   String depantmentName;
}

@Embeddable
Class ChildPK{

    @ManyToOne
    @JoinColumn(name = "PARENT_ID",referencedColumnName="PARENT_ID", nullable= false, updatable=false)
    Parent parent;

   @Column(name="DEPARTMENT_KEY")
    String departmentKey;
}


When I try to persist the parent entity, the parent record is saving properly.
But after that while persisting the child record, the 'parent-id' value is not being passed to the child and I am getting a SQL exception for NULL value.

I have got struck at this point. Can anyone help me in this issue please.

Code:
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
   at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:513)
   at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:101)
   at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:263)
   at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:86)
   at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
   at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1414)
   ... 28 more
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(DefaultFlushEventListener.java:50)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
   at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
   ... 33 more
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into (???)

   at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
   at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10768)
   at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
   ... 40 more


Thanks in advance.

Regards,
Harish


Top
 Profile  
 
 Post subject: Re: @OneToMany between parent to child composite primary key
PostPosted: Tue Nov 23, 2010 4:52 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
bringing to the top..

Can anyone post the solution if they know please

Regards
Harish


Top
 Profile  
 
 Post subject: Re: @OneToMany between parent to child composite primary key
PostPosted: Thu Mar 10, 2011 12:26 pm 
Newbie

Joined: Thu Mar 10, 2011 12:22 pm
Posts: 1
are you passing your values to childpk? well i guess you have'nt included your childPK values and thats where its giving a problem for...
see if this solves the problem...


Top
 Profile  
 
 Post subject: Re: @OneToMany between parent to child composite primary key
PostPosted: Tue Mar 15, 2011 5:18 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
Hi,

The parentId value is an oracle sequence generated value and this can only be filled during runtime. While departmentKey value is passed explicitly during the call.

Is this what you are expecting?

Cheers,
Harish


Top
 Profile  
 
 Post subject: Re: @OneToMany between parent to child composite primary key
PostPosted: Tue Mar 15, 2011 5:56 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

Why do you specify a JoinColumn on both the OneToMany and the ManyToOne annotation?
Given that your OneToMany is using the mappedBy property shouldn't the JoinColumn only be defined on the ManyToOne annotation?


Top
 Profile  
 
 Post subject: Re: @OneToMany between parent to child composite primary key
PostPosted: Thu Mar 17, 2011 4:42 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
I have tried all the three cases. JoinColumn on parent class, JoinColumn on child class and the above case i.e on both classes. Still the same issue

-Harish


Top
 Profile  
 
 Post subject: Re: @OneToMany between parent to child composite primary key
PostPosted: Thu Mar 17, 2011 5:49 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Only put it on the Child Class.
Can you post here the code you are using to save Parent and Child entities?


Top
 Profile  
 
 Post subject: Re: @OneToMany between parent to child composite primary key
PostPosted: Wed Sep 21, 2011 6:42 am 
Newbie

Joined: Fri Nov 19, 2010 4:02 am
Posts: 11
Hi,

This has been resolved. The solution is.. we need to populate the parent object variable in the child class too in order to carry forward the primary key to the child class

Cheers,
Harish


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.