You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Tom Cassimon <to...@gmail.com> on 2009/09/02 21:13:39 UTC

Problem with primary key in Persisted object list

Hi all,

I have a problem with CXF. I'm using the newest version 2.2.3.

 

I'm using Hibernate & Spring on a Tomcat Application Server and i
communicate with a Java Swing client with Apache CXF. Now everything works
like a charm. But there is one issue. I have a persisted object on the
server with a @ManyToOne relationship. 

 

            Definition in entity class Patient:

 

    @JoinTable(name = "patient_document", joinColumns = {@JoinColumn(name =
"patientId")}, inverseJoinColumns = {@JoinColumn(name = "documentId")})

    @OneToMany(fetch=FetchType.LAZY)

    private List<Document> documentList;

 

            Document entity:

 

@Entity

@Table(name = "document")

public class Document implements Serializable {

 

    private static final long serialVersionUID = 1L;

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    @Basic(optional = false)

    @Column(name = "id")

    private Long id;

            .

            .

            .

 

I recieve the document list correct on the client side. But the id field is
null. This causes problems when I send the Patient class back to the Tomcat
server for an update. Let's say I add a new document to the documentList in
the patient object and I want to insert it in the database. I execute the
following code:

 

            //Process documents

            if (p.getDocumentList() != null) {

                for (Document d : p.getDocumentList()) {

                    if (d.getId() == null)

                        em.persist(d);

                    else

                        em.merge(d);

                }

            }

//Finally process patient

            if (p.getId() == null)

                em.persist(p);

            else

                em.merge(p);

 

Now the problem is that the id field is null, so the code here "thinks" it
are all new documents. He removes the links in the database and re-inserts
all the documents. But due to another bug not cleaning up Orphans everything
ends up more times in the database, and that is off course not wanted. Do I
do something wrong here or is it an actual bug? The strange thing is. I also
have @OneToOne relationships end there the id is submitted to the client. I
don't see any reason for not submitting the id in @ManyToOne relationships.
Maybe I am doing something wrong.

 

If you need more info please ask.

 

Regards,

 

Tom