*
The moose likes EJB and other Java EE Technologies and the fly likes One-To-Many bidirectional Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "One-To-Many bidirectional" Watch "One-To-Many bidirectional" New topic
Author

One-To-Many bidirectional

Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Hi,

I am having a problem in managing one-to-many bidirectional relationship.

Tables:

1. city (cityID, name, description)
2. branch (branchID, name, description, cityID)

Table relationship is "each city has one to many branches"


Entities:





Client Code:




createCity() code:


Now, the problem is this that only city table is populated, while branch table is empty, and its not populated. Though i am setting the branch collection in city
object.

Can you guys please tell me what is wrong in my code?

Thank you,
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Hi,

I have added CascadeType.All to the branches collection, and now its working. But i have another question now. It was not inserting value of branchID.

So i call branch.setCity() for both branch objects. And at the same time, i am also adding both branch objects in branches collection in city object and calling city.setBranches(). I think i am duplicating the code. Because on branch objects i am calling setCity(), and on city object i am calling setBranches(). How to solve this? I was expecting ejb wil handle it herself.

Thanks,
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
I can also fix it by adding addBranch() method, and calling setCity() in it. But i was thinking that ejb will do it herself. Am i right?

And how to update/delete City or Branch object?

Will appreciate your help,
Thanks,
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Hi,

Just now i tried few things and now i am able to update/delete the city object. But if i want to delete a particular branch object from city.branches collection, how i will do it. I tried it but its not working.

Right now what i am doing is to first remove that branch object from the city.branches collection. Than i am calling bean's merge method. There is no error/exception, but that branch object is not deleted from database.

How to fix it?

Thanks a lot,
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
First thing first, why do you need to go bidirectional? despite its not recommended.
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Actually i wanted to test how it works.
By the way, why it is not recommended and what is its alternative?
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Faraz Ali:
By the way, why it is not recommended and what is its alternative?


Because of the complication resulted from a network object model with bidirectional references.

You make it that way, if it really needed and there is no other better substitute.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
When we attempt bidirectional relationship, we need consider both sides. If one side is modified, the change must reflect on the other. This thing was done previously in EJB 2.0 with CMPs, I suppose.
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Still Open
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
Originally posted by Faraz Ali:
Hi,

I am having a problem in managing one-to-many bidirectional relationship.

Tables:

1. city (cityID, name, description)
2. branch (branchID, name, description, cityID)

Table relationship is "each city has one to many branches"


Entities:





Client Code:




createCity() code:


Now, the problem is this that only city table is populated, while branch table is empty, and its not populated. Though i am setting the branch collection in city
object.

Can you guys please tell me what is wrong in my code?

Thank you,


Yes. There is something wrong with the code. You are declaring the bidirectionality the wrong way.

Please change -



to



since you declared the bidirectionality as @OneToMany(mappedBy="city")

If you refer to the source code, mappedBy declared the field name of the variable in Branch object where bidirectionality exists.

In your client code, use the following



Of course, the basic premise in the application code is that the City Object already exists with the primary key of 1. If you do not want to set the City object manually in your branches, you could add the following code in your City



This callback method simply assigns the City object to the collection of Branches. In the EntityManager mechanism, after persisting City, it will call this method before persisting the collection of Branches.

Regards.


Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
email: jerwin.uy@incuventure.net
Faraz Ali
Ranch Hand

Joined: May 15, 2008
Posts: 108
Thanks a lot for pointing out the issue.
But what if i want to remove a branch object from the city.branches collection. What need to be changed in the code. I tried, to remove on branch instance from the collection, and than i called merge method, but its not working. Can you tell you how to fix it?

Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: One-To-Many bidirectional