Murli Manohar

Greenhorn
+ Follow
since Nov 17, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Murli Manohar

Got the answer myself
have to set ( mainRef.setRefTable(em.find(RefTable.class,keyRef))

MainTable mainTable=new MainTable();
mainTable.setMainId(keyMain);
mainTable.setCol2("O");
List<MainRef> ref=new ArrayList<MainRef>();
MainRef mainRef=new MainRef();
mainRef.setMainId(keyMain);
mainRef.setRefId(keyRef);
mainRef.setMainTable(mainTable);
mainRef.setRefTable(em.find(RefTable.class,keyRef));
ref.add(mainRef);
mainTable.setMainRefList(ref);
em.persist(mainTable);
DB --
Main_table
Name Null? Type
----------------------------------------- -------- ---------------
MAIN_ID NOT NULL NUMBER
COL2 VARCHAR2(4000)
Ref_table
Name Null? Type
----------------------------------------- -------- ----------------
REF_CD NOT NULL NUMBER
REF_VAL VARCHAR2(4000)
Main_ref (Associaton table)
Name Null? Type
----------------------------------------- -------- ------------------
MAIN_ID NOT NULL NUMBER
REF_ID NOT NULL NUMBER

entities 1 Main Table

@Entity
@NamedQuery(name = "MainTable.findAll", query = "select o from MainTable o")
@Table(name = "MAIN_TABLE")
public class MainTable implements Serializable {
private String col2;
@Id
@Column(name="MAIN_ID", nullable = false)
private Long mainId;
@OneToMany(mappedBy = "mainTable" ,cascade={CascadeType.ALL})
private List<MainRef> mainRefList;

public MainTable() {
}

...getter and setters


entities 2 Ref Table
@Entity
@NamedQuery(name = "RefTable.findAll", query = "select o from RefTable o")
@Table(name = "REF_TABLE")
public class RefTable implements Serializable {
@Id
@Column(name="REF_CD", nullable = false)
private Long refCd;
@Column(name="REF_VAL")
private String refVal;
@OneToMany(mappedBy = "refTable")
private List<MainRef> mainRefList;

public RefTable() {
}
...getter setters....

entity3 MAinRef (Association)
@Entity
@NamedQuery(name = "MainRef.findAll", query = "select o from MainRef o")
@Table(name = "MAIN_REF")
@IdClass(MainRefPK.class)
public class MainRef implements Serializable {
@Id
@Column(name="MAIN_ID", nullable = false, insertable = false, updatable = false)
private Long mainId;
@Id
@Column(name="REF_ID", nullable = false, insertable = false, updatable = false)
private Long refId;
@ManyToOne
@JoinColumn(name = "REF_ID", referencedColumnName = "REF_CD")
private RefTable refTable;
@ManyToOne
@JoinColumn(name = "MAIN_ID", referencedColumnName = "MAIN_ID")
private MainTable mainTable;

public MainRef() {
}
---PK class
public class MainRefPK implements Serializable {
public Long mainId;
public Long refId;

public MainRefPK() {
}

public MainRefPK(Long mainId, Long refId) {
this.mainId = mainId;
this.refId = refId;
}

public boolean equals(Object other) {
if (other instanceof MainRefPK) {
final MainRefPK otherMainRefPK = (MainRefPK) other;
final boolean areEqual = (otherMainRefPK.mainId.equals(mainId) && otherMainRefPK.refId.equals(refId));
return areEqual;
}
return false;
}

public int hashCode() {
return super.hashCode();
}

Problem ---
When i try persisting Main_table entity i get following error.
.....
......
long key1=333L;
long key2=33L;
final Context context = getInitialContext();
SessionEJB sessionEJB = (SessionEJB)context.lookup("SessionEJB");
MainTable obj=new MainTable();
obj.setMainId(key1);
obj.setCol2("OOO");
List<MainRef> ref=new ArrayList<MainRef>();
MainRef objRef=new MainRef();
objRef.setMainId(key1);
objRef.setRefId(key2);
ref.add(objRef);
obj.setMainRefList(ref);
sessionEJB.persistEntity(obj);

.....
Call: INSERT INTO MAIN_REF (REF_ID, MAIN_ID) VALUES (?, ?)
bind => [null, null]
Query: InsertObjectQuery(project1.MainRef@1ed364c); nested exception is: oracle.oc4j.rmi.OracleRemoteException: An exception occurred during transaction completion: ; nested exception is:
javax.transaction.RollbackException: returning error in transaction: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: ORA-01400: cannot insert NULL into ("HR"."MAIN_REF"."MAIN_ID")

Any pointers !!! Thanks in advance