This discussion is archived
1 Reply Latest reply: Jul 24, 2012 11:52 PM by r035198x RSS

EJB3, Entity wirh bidirection relationship and remote client (

951572 Newbie
Currently Being Moderated
Hi, I'm new in EJB3 and i haven't experience. I have 2 Entity Document and DocumentItems with bidirection relationship and remote client app. Document have many DocumentItems. In my sample application I want to find all document and for each add DocumentItems.

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Document implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
@OneToMany(mappedBy = "document", cascade= {CascadeType.ALL}, fetch= FetchType.EAGER)
protected List<DocumentItem> documentItems;

---------------------------------------------------------------------
geter and seter method for fields
---------------------------------------------------------------------
}

@Entity
@Inheritance(strategy= InheritanceType.JOINED)
public class DocumentItem implements Serializable {
@ManyToOne
private Document document;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
---------------------------------------------------------------------
geter and seter method for fields
---------------------------------------------------------------------
}

My code on client for testing:

public class Main {

/**
* @param args the command line arguments
*/
private static PretendentDocumentRemote pretendentDocumentRemote;

public static void main(String[] args) {
pretendentDocumentRemote = DocumentFactory.getPretendentDocumentRemote();
List<Document> findDocuments = pretendentDocumentRemote.findAllDocuments();
for (Document findDocument : findDocuments) {
DocumentItem item = new DocumentItem();
pretendentDocumentRemote.addDocumentItem(item, document);
}
}
}

When I debug and view findDocuments object on breakpoint it contains list of Document. And each element of list(<Document>) has documentItems field that contains list of DocumentItems. But when I call pretendentDocumentRemote.addDocumentItem(item, document) method, documentItems field of document object is null. Can you explain why?

P.S: I'm sorry for my English :-)
  • 1. Re: EJB3, Entity wirh bidirection relationship and remote client (
    r035198x Pro
    Currently Being Moderated
    Maybe you have a typo in your code because
    for (Document findDocument : findDocuments) {
    DocumentItem item = new DocumentItem();
    pretendentDocumentRemote.addDocumentItem(item, document);
    }
    should be
    for (Document findDocument : findDocuments) {
    DocumentItem item = new DocumentItem();
    pretendentDocumentRemote.addDocumentItem(item, findDocument);
    }
    Also, instead of getting remote data and sending it back to the server, consider writing a service that only gets the client data and manipulates the server data without sending to the client if the client doesn't need to see it.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points