Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Object Relational Mapping and the fly likes JPA, one to many and serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA, one to many and serialization" Watch "JPA, one to many and serialization" New topic

JPA, one to many and serialization

Jarrod Marshall

Joined: Aug 01, 2008
Posts: 3
Hi all,
I'm fairly new to JPA and having to work through an issue.

I've used Netbeans and JDeveloper both to generate entities from tables.

Now, these entities cross language boundaries via serialization methods exposed with Oracle's Coherence - meaning, entities are both serialized to and from C# .NET objects.

Due to this serialization, I can't have the backref/many to one child->parent relationship on the child otherwise I get a stack overflow.

I just want a unidirectional relationship mapped from table a to table b with no middle join table. Sure, the many to one exists but I don't want it (can't have it) mapped.

On the child entity, I just want the ID of the parent, not an object that references the parent. Just a field with parent ID.

Any help would be greatly appreciated.
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425

Firstly as per JPA Specification you cannot have OneToMany unidirectional without a join table so either you need to have a join table or live with ManyToOne on the other side.

Regarding serialization, I agree that bidirectional relationship forms a circular reference so Jaxb doesn't serialize the circular graphs,

One way to resolve this is using @XmlTransient variable in you child class on the variable which refers parent.

There are some other ways which even i am looking at, but haven't tried due to the limitation of server version availability at my end.

Let me know if any of these works if we want to live with bidirectional relationship.

[ August 01, 2008: Message edited by: Rahul Juneja ]

Rahul Juneja
ThoughtClicks -
Jarrod Marshall

Joined: Aug 01, 2008
Posts: 3
I appreciate the reply - not what I was hoping for.
I thought I had read that through the ORM.xml file it was possible to base a one to many on a foreign key.

The problem with the XmlTransient is that this uses proprietary, custom serialization routines. Not the out of the box java serialization.

I can avoid serializing the back ref back to the parent but when the object comes back into the java realm from the network and it attempts to deserialize it doesn't go out and get the back ref object and load it. The reason it needs contain just a foreign key is that this object will be automatically persisted via Coherence.
I agree. Here's the link:
subject: JPA, one to many and serialization
It's not a secret anymore!