Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA, one to many and serialization

 
Jarrod Marshall
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jarrod Marshall
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic