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