-->
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.  [ 7 posts ] 
Author Message
 Post subject: persistent parent and transient child save
PostPosted: Thu Jan 01, 2009 11:47 pm 
Beginner
Beginner

Joined: Wed May 14, 2008 2:06 pm
Posts: 21
I have parent and child with cascade all . In my case I open session, load the parent create a new child (this is transient) and add this child to parent and call save , I was hoping that hibernate would generate the PK for the newly added child , but it did not , it is generating only after flush ,and I if I want to use the PK of this child for some other operation before flush I canot , I want hibernate to generate the PK of the child with call to save on persistant parent , please tell me how to do this ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 9:54 am 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
Hi,

It all depends in your mappings files. If you can post them will be highly appreciated,

regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 4:15 pm 
Beginner
Beginner

Joined: Wed May 14, 2008 2:06 pm
Posts: 21
I am using hibernate annotations

this is the code to create new child and add to persistant parent .
Code:
      Transaction tx = null;
      Session session = InitSessionFactory.getInstance().getCurrentSession();
      try {
         tx = session.beginTransaction();
         EaAudit  eaAudit= (EaAudit)session.get(EaAudit.class, new Long(7));
         EaAuditProg  eaAuditProg= new EaAuditProg();
         eaAudit.getEaAuditProgs().add(eaAuditProg);
         
         eaAuditProg.setEaAudit(eaAudit);
         session.save(eaAudit);
         System.out.println(eaAuditProg.getSysAuditProgId());  // this returns null
         tx.commit();
      } catch (HibernateException e) {
         e.printStackTrace();
         if (tx != null && tx.isActive())
            tx.rollback();

      }




code for EaAudit and EaAuditProgram

Code:
package de.laliluna.example;

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.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
* <p>Pojo mapping TABLE PUBLIC.EA_AUDIT</p>
*
* <p>Generated at Thu Jan 01 20:13:11 EST 2009</p>
* @author Salto-db Generator v1.0.16 / EJB3
*
*/
@Entity
@Table(name = "EA_AUDIT", schema = "PUBLIC")
@SuppressWarnings("serial")
public class EaAudit implements Serializable {

   /**
    * Attribute sysAuditId.
    */
   private Long sysAuditId;
   
   /**
    * List of EaAuditProg
    */
   private List<EaAuditProg> eaAuditProgs = new ArrayList<EaAuditProg>();

   
   /**
    * @return sysAuditId
    */
   @Id
    @GeneratedValue(generator="eaauditseq",strategy=GenerationType.SEQUENCE)
    @SequenceGenerator(name="eaauditseq",sequenceName="eaauditseq", allocationSize=1)
   @Column(name = "SYS_AUDIT_ID")
      public Long getSysAuditId() {
      return sysAuditId;
   }

   /**
    * @param sysAuditId new value for sysAuditId
    */
   public void setSysAuditId(Long sysAuditId) {
      this.sysAuditId = sysAuditId;
   }
   
   /**
    * Get the list of EaAuditProg
    */
    @OneToMany(mappedBy="eaAudit",cascade=CascadeType.ALL)
    public List<EaAuditProg> getEaAuditProgs() {
       return this.eaAuditProgs;
    }
   
   /**
    * Set the list of EaAuditProg
    */
    public void setEaAuditProgs(List<EaAuditProg> eaAuditProgs) {
       this.eaAuditProgs = eaAuditProgs;
    }


}

package de.laliluna.example;

import java.util.List;
import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Embeddable;

/**
* <p>Pojo mapping TABLE PUBLIC.EA_AUDIT_PROG</p>
*
* <p>Generated at Thu Jan 01 20:13:11 EST 2009</p>
* @author Salto-db Generator v1.0.16 / EJB3
*
*/
@Entity
@Table(name = "EA_AUDIT_PROG", schema = "PUBLIC")
@SuppressWarnings("serial")
public class EaAuditProg implements Serializable {

   /**
    * Attribute sysAuditProgId.
    */
   private Long sysAuditProgId;
   
   /**
    * Attribute infProgram
    */
    private InfProgram infProgram;   

   /**
    * Attribute eaAudit
    */
    private EaAudit eaAudit;   

   
   /**
    * @return sysAuditProgId
    */
   @Id
    @GeneratedValue(generator="eaauditprog",strategy=GenerationType.SEQUENCE)
    @SequenceGenerator(name="eaauditprog",sequenceName="eaauditprog", allocationSize=1)
   @Column(name = "SYS_AUDIT_PROG_ID")
      public Long getSysAuditProgId() {
      return sysAuditProgId;
   }

   /**
    * @param sysAuditProgId new value for sysAuditProgId
    */
   public void setSysAuditProgId(Long sysAuditProgId) {
      this.sysAuditProgId = sysAuditProgId;
   }
   
   /**
    * get infProgram
    */
   @ManyToOne
   @JoinColumn(name = "SYS_PROG_ID")
   public InfProgram getInfProgram() {
      return this.infProgram;
   }
   
   /**
    * set infProgram
    */
   public void setInfProgram(InfProgram infProgram) {
      this.infProgram = infProgram;
   }

   /**
    * get eaAudit
    */
   @ManyToOne
   @JoinColumn(name = "SYS_AUDIT_ID")
   public EaAudit getEaAudit() {
      return this.eaAudit;
   }
   
   /**
    * set eaAudit
    */
   public void setEaAudit(EaAudit eaAudit) {
      this.eaAudit = eaAudit;
   }



}

ataveras wrote:
Hi,


It all depends in your mappings files. If you can post them will be highly appreciated,

regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2009 11:02 am 
Beginner
Beginner

Joined: Wed May 14, 2008 2:06 pm
Posts: 21
Please help me with this.
miroconnect wrote:
I am using hibernate annotations

this is the code to create new child and add to persistant parent .
Code:
      Transaction tx = null;
      Session session = InitSessionFactory.getInstance().getCurrentSession();
      try {
         tx = session.beginTransaction();
         EaAudit  eaAudit= (EaAudit)session.get(EaAudit.class, new Long(7));
         EaAuditProg  eaAuditProg= new EaAuditProg();
         eaAudit.getEaAuditProgs().add(eaAuditProg);
         
         eaAuditProg.setEaAudit(eaAudit);
         session.save(eaAudit);
         System.out.println(eaAuditProg.getSysAuditProgId());  // this returns null
         tx.commit();
      } catch (HibernateException e) {
         e.printStackTrace();
         if (tx != null && tx.isActive())
            tx.rollback();

      }




code for EaAudit and EaAuditProgram

Code:
package de.laliluna.example;

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.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
* <p>Pojo mapping TABLE PUBLIC.EA_AUDIT</p>
*
* <p>Generated at Thu Jan 01 20:13:11 EST 2009</p>
* @author Salto-db Generator v1.0.16 / EJB3
*
*/
@Entity
@Table(name = "EA_AUDIT", schema = "PUBLIC")
@SuppressWarnings("serial")
public class EaAudit implements Serializable {

   /**
    * Attribute sysAuditId.
    */
   private Long sysAuditId;
   
   /**
    * List of EaAuditProg
    */
   private List<EaAuditProg> eaAuditProgs = new ArrayList<EaAuditProg>();

   
   /**
    * @return sysAuditId
    */
   @Id
    @GeneratedValue(generator="eaauditseq",strategy=GenerationType.SEQUENCE)
    @SequenceGenerator(name="eaauditseq",sequenceName="eaauditseq", allocationSize=1)
   @Column(name = "SYS_AUDIT_ID")
      public Long getSysAuditId() {
      return sysAuditId;
   }

   /**
    * @param sysAuditId new value for sysAuditId
    */
   public void setSysAuditId(Long sysAuditId) {
      this.sysAuditId = sysAuditId;
   }
   
   /**
    * Get the list of EaAuditProg
    */
    @OneToMany(mappedBy="eaAudit",cascade=CascadeType.ALL)
    public List<EaAuditProg> getEaAuditProgs() {
       return this.eaAuditProgs;
    }
   
   /**
    * Set the list of EaAuditProg
    */
    public void setEaAuditProgs(List<EaAuditProg> eaAuditProgs) {
       this.eaAuditProgs = eaAuditProgs;
    }


}

package de.laliluna.example;

import java.util.List;
import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Embeddable;

/**
* <p>Pojo mapping TABLE PUBLIC.EA_AUDIT_PROG</p>
*
* <p>Generated at Thu Jan 01 20:13:11 EST 2009</p>
* @author Salto-db Generator v1.0.16 / EJB3
*
*/
@Entity
@Table(name = "EA_AUDIT_PROG", schema = "PUBLIC")
@SuppressWarnings("serial")
public class EaAuditProg implements Serializable {

   /**
    * Attribute sysAuditProgId.
    */
   private Long sysAuditProgId;
   
   /**
    * Attribute infProgram
    */
    private InfProgram infProgram;   

   /**
    * Attribute eaAudit
    */
    private EaAudit eaAudit;   

   
   /**
    * @return sysAuditProgId
    */
   @Id
    @GeneratedValue(generator="eaauditprog",strategy=GenerationType.SEQUENCE)
    @SequenceGenerator(name="eaauditprog",sequenceName="eaauditprog", allocationSize=1)
   @Column(name = "SYS_AUDIT_PROG_ID")
      public Long getSysAuditProgId() {
      return sysAuditProgId;
   }

   /**
    * @param sysAuditProgId new value for sysAuditProgId
    */
   public void setSysAuditProgId(Long sysAuditProgId) {
      this.sysAuditProgId = sysAuditProgId;
   }
   
   /**
    * get infProgram
    */
   @ManyToOne
   @JoinColumn(name = "SYS_PROG_ID")
   public InfProgram getInfProgram() {
      return this.infProgram;
   }
   
   /**
    * set infProgram
    */
   public void setInfProgram(InfProgram infProgram) {
      this.infProgram = infProgram;
   }

   /**
    * get eaAudit
    */
   @ManyToOne
   @JoinColumn(name = "SYS_AUDIT_ID")
   public EaAudit getEaAudit() {
      return this.eaAudit;
   }
   
   /**
    * set eaAudit
    */
   public void setEaAudit(EaAudit eaAudit) {
      this.eaAudit = eaAudit;
   }



}

ataveras wrote:
Hi,


It all depends in your mappings files. If you can post them will be highly appreciated,

regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2009 12:40 pm 
Beginner
Beginner

Joined: Wed May 14, 2008 2:06 pm
Posts: 21
I added the mapping files.Please help me .
ataveras wrote:
Hi,

It all depends in your mappings files. If you can post them will be highly appreciated,

regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2009 1:58 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
Hello miroconnect, i think that it's because of the generator strategy. The one that you are using it will trigger the select on the sequence after the commit of the transaction. You could check how it works looking at the source of org.hibernate.id.SequenceGenerator. Why do you need id before the transaction commit, remember that you could still use the reference of the object,

regards,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2009 11:46 pm 
Beginner
Beginner

Joined: Wed May 14, 2008 2:06 pm
Posts: 21
here is my understanding
When I use persistanceContext to get an entity instance it puts in its cache (first level cahce)and state of this entity is persistent and any changes made will effect the database only after flush is called .

In my case I am creating new instances of another entity child and adding it to collection in parent persistent entity and call saveOrUpdate becasue the parent is already persistent hibernate does nothing , but before calling saveOrupdate I called session.evict(parent) and called saveOrupdate(parent) in this case hibernate generated PK for child becasue parent in not in session .
so here is my question

i have an parent instance which is in persistent state and I update the instance with new child entity instances (new entity ofcourse means transient state) and call session.saveorUpdate(parent) , (usually any entity in transient state is made persistent by calling any method like save or update or merge etc ) but in this case parent child relation how to make children persitent without calling flush?




I tried the same example with a transient instance of EaAudit and in this case becasue the persistence context cache does not have EaAudit and calling saveorUpdate for EaAudit makes it persistent it hibernate goes to database to generate

ataveras wrote:
Hello miroconnect, i think that it's because of the generator strategy. The one that you are using it will trigger the select on the sequence after the commit of the transaction. You could check how it works looking at the source of org.hibernate.id.SequenceGenerator. Why do you need id before the transaction commit, remember that you could still use the reference of the object,

regards,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.