This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Error in Many to Many persisting EJB3 Toplink Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Error in Many to Many persisting EJB3 Toplink" Watch "Error in Many to Many persisting EJB3 Toplink" New topic
Author

Error in Many to Many persisting EJB3 Toplink

Murli Manohar
Greenhorn

Joined: Nov 17, 2008
Posts: 2
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
Murli Manohar
Greenhorn

Joined: Nov 17, 2008
Posts: 2
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);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in Many to Many persisting EJB3 Toplink
 
Similar Threads
incomplete @JoinColumns
using 2 @OnetoMany annoations in one Entity Class
Repeated column in mapping for entity ERROR PLEASE HELP..
mapping a join table with an intermediate entity
Can a class be mapped to a table and be Embeddable at the same time?