persistence@glassfish.java.net

Re: ManyToOne problem bug?

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Tue, 26 Feb 2008 18:07:26 -0800

It shouldn't depend on the setter access type. The known problem with using JAXB
with JPA is that JAXB can't handle (populated) bi-directional relationships. The
solution is to nullify one side of the relationships before returning the graph
back to the client. You need to be careful to nullify those relationships on a
detached graph - otherwise the JPA provider will treat it as an update.

Regards,
-marina

Dru Devore wrote:
> I have a many to one relationship setup where the primary column of the
> parent does not participate in the relationship. I am using the
> @JoinColumn annotation and seem to have it working now, it took me about
> a week to figure out what is going on. This is is what I have discovered
> and I want to know if it is proper behavior or if it is a bug. The
> following are reference part of the classes I used, simple parent and child.
>
> Class Parent:
> @OneToMany(mappedBy="myParent")
> private List<Child> childs;
>
> public List<Child> getChilds() {
> return childs;
> }
>
> public void setChilds(List<Child> childs) {
> this.childs = childs;
> }
>
> Class Child:
> @ManyToOne
> @JoinColumn(name="placer_id", referencedColumnName="placer_id")
> private Parent myParent;
>
> public Parent getParent() {
> return myParent;
> }
>
> protected void setParent(Parent parent) {
> this.myParent = parent;
> }
>
> The problem is that in the child if I have the setParent method public
> it an exception that results in the following.
>
> Caused by: javax.xml.bind.MarshalException
> - with linked exception:
> [com.sun.istack.SAXException2: A cycle is detected in the object graph.
> This will cause infinitely deep XML
>
>
> If the method is protected or non-existent it runs with no problems. I
> can see why you possibly don't want the parent changed from the child
> but I have not seen any documen! tation on this not being allowed,
> though I have not completed reading the spec yet. I have been reading a
> hibernate 3 book, though this is being written with toplink, which shows
> a public modifier on the parent object of a ManyToOne relationship.
>
> The bad thing is that I was looking everywhere else for the problem and
> simply told NB to generate standard getters and setters for me, which
> seems to be my downfall.
>
> Is this proper behavior or a bug?
>
> ---
> Dru Devore