• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

One-To-Many bidirectional

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First thing first, why do you need to go bidirectional? despite its not recommended.
 
Faraz Ali
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still Open
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Faraz Ali
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
It's exactly the same and completely different as this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic