Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Combining @OneToMany and @SecondaryTable

 
James Johnsona
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 things I want to do. I know how to do them separately. I am wondering if I can solve them together.


1) I have a relationship where 1 entity is mapped to multiple entities. Cool, that can be done through the @OneToMany annotation I am familiar with. So I can get back a Set<OtherEntity>

2) I need a a column from another table in my entity. Can be done using the @SecondaryTable annotation. Again, doable.


What if I needed a @OneToMany association, from a column of another table?

I have never seen an example like this. Is this doable?

So for example, suppose we had Table Country and Table City.

It is easy to have something like:



Where country_code is the pk of Country, and country_code and city_name together make the pk of City.


But what if I wanted:



@OneToMany? @SecondaryTable?

Is this even possible in Hibernate? This needs to be able to load and then written back in the database as well in case the city list is updated.




 
Dave Tolls
Ranch Hand
Posts: 2096
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I presume city_name is not the primary key on the City table then Hibernate would have nothing to key off, even if this were possible.

I'm not sure what you're going to gain by this?
If you only want a list of city names, say for display on the front end or for dispatch somewhere, then why not write a query that returns just that for a given country?
You could build up the List from the List<City> but that might have some overhead as Hibernate will then pull all the Cities from the DB, when all you really want is the names.
 
James Johnsona
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Since I presume city_name is not the primary key on the City table then Hibernate would have nothing to key off, even if this were possible.

I'm not sure what you're going to gain by this?
If you only want a list of city names, say for display on the front end or for dispatch somewhere, then why not write a query that returns just that for a given country?
You could build up the List from the List<City> but that might have some overhead as Hibernate will then pull all the Cities from the DB, when all you really want is the names.


I don't want to go into too much detail but we need to allow the user to update this list as well and when it is updated store it back into the City table. So you are right I could write a query for the load from database to get this list of city_name Strings and set it onto the Country entity (I assume that is what you were saying), but how would hibernate then know how to store it back for an update?

Also, city_name is part of a composite key (along with country_code) for the City table. Each row in the city table can be uniquely identified through the country_code and city_name pairing. I was hoping those 2 together (which we have in the Country entity) would be sufficient to let Hibernate know how to store back this list of city_names.
 
James Johnsona
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen examples using @SecondaryTable pulling a field from another entity/table.
http://www.concretepage.com/hibernate/secondarytables_hibernate_annotation

The only difference between all the other @SecondaryTable examples I've seen and this is here we have a List instead of a single field with a @OneToMany relationship.
 
Dave Tolls
Ranch Hand
Posts: 2096
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Johnsona wrote:I've seen examples using @SecondaryTable pulling a field from another entity/table.
http://www.concretepage.com/hibernate/secondarytables_hibernate_annotation

The only difference between all the other @SecondaryTable examples I've seen and this is here we have a List instead of a single field with a @OneToMany relationship.


Exactly.
SecondaryTable is a One To One relationship.

I still don't see what you gain by trying to get a simple List<String> in the Country.

And no, I wasn't suggesting adding a transient List<String> to the Country table, I was suggesting getting the List as a separate action and sticking it in whatever View you're using.
When the user submits a new City (or changes the List) you can then simply update the database as you know the Country, so have the full PK.
 
James Johnsona
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
James Johnsona wrote:I've seen examples using @SecondaryTable pulling a field from another entity/table.
http://www.concretepage.com/hibernate/secondarytables_hibernate_annotation

The only difference between all the other @SecondaryTable examples I've seen and this is here we have a List instead of a single field with a @OneToMany relationship.


Exactly.
SecondaryTable is a One To One relationship.

I still don't see what you gain by trying to get a simple List<String> in the Country.

And no, I wasn't suggesting adding a transient List<String> to the Country table, I was suggesting getting the List as a separate action and sticking it in whatever View you're using.
When the user submits a new City (or changes the List) you can then simply update the database as you know the Country, so have the full PK.


So you are saying, extract the List<String> from the database through a separate query. Ok can be done. But I don't understand what you mean by "view". I'm sorry kind of new to hibernate and that is a word that seems a little ambiguous to me.

We want to stick teh List<String> in Country, because these County objects are then passed on to another service which then gets the names from there (In that new Country object, it has a field with type List<String>).
But I really don't understand what you mean by "view". What we currently had (List<City>) it was easy for hibernate to automatically save the Cities back to the appropriate table. But how can we do that and just use List<String>? Like what annotations would we use to help map back this data to the database. I was hoping a combination of @OneToMany and @SecondaryTable would work but it seems like it doesn't...
 
Dave Tolls
Ranch Hand
Posts: 2096
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using "view" in terms of MVC, so the front end. "Consumer" might have been a better choice of term since that includes eg a web service.

You essentially have two models here.
The one for your db related stuff and the one you are going to transmit over your service.
So use two, if you really need the List<City> to be a List<String>, and translate between them.

There isn't a way for you (at least not in my experience, which might need taking with a pinch of salt) to map your table just via a String.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic