wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA, one to many and serialization" Watch "JPA, one to many and serialization" New topic
Author

JPA, one to many and serialization

Jarrod Marshall
Greenhorn

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
Jarrod,

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.

http://www.theserverside.com/discussions/thread.tss?thread_id=47299

https://jaxb.dev.java.net/guide/Mapping_cyclic_references_to_XML.html

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

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

Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Jarrod Marshall
Greenhorn

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: http://aspose.com/file-tools
 
subject: JPA, one to many and serialization
 
Similar Threads
Association problem in delete
retrieving object hierarchy - many-to-one?
JPA: Joined Table ID of subclass
iBatis resultMap groupBy Question
Unable to save data into child table