These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Mapping two classes with @OneToMany in a table
PostPosted: Wed Jul 20, 2011 3:17 am 
Newbie

Joined: Mon Jun 06, 2011 7:39 am
Posts: 3
Hi,
I'm pretty a newbie with mapping technologies, and i have a problem with mapping two tables in a single class.

My problem is as follows:

I want to implement i18n functionalities at database level for the countries, places, ... and any other geographical data that may be stored in the database.
So if an english speaking user would log into the application the coutry names, etc would be printed in his language (english: united kingdom, italian: regno unito, ....)

In the legacy db the "schema of the relationships is as follows" (example with countries):

Code:
+-------------+                     +-----------------+                        +----------------+
|country      |                     |  country_i18n   |                        | language       |
+-------------+ 1..1 --------- 0..n +-----------------+ 0..n ------------- 1..1+----------------+
| id (pk)     |                     | id (pk)         |                        | id (pk)        |
| code (uk)   |                     | name            |                        | code (uk)      |
+-------------+                     | language_id (fk)|                        | name           |
                                    | country_id (fk) |                        +----------------+
                                    +-----------------+


Now, is there a way to map all of this stuff (rows of tables country and contry_i18n) in a single table, telling hibernate to use le language code or any other unique value (which is unique) as discriminator value?

Someone told me to try solving that by mapping the countries in a hashmap like Map<enumLanguage,Country> but I can't understand how... and if it's still possible

Any suggestions, or other possible solutions would be very much appreciated!

Thank you very much in advance. :)


Top
 Profile  
 
 Post subject: Re: Mapping two classes with @OneToMany in a table
PostPosted: Thu Jul 21, 2011 3:20 am 
Newbie

Joined: Mon Jun 06, 2011 7:39 am
Posts: 3
up


Top
 Profile  
 
 Post subject: Re: Mapping two classes with @OneToMany in a table
PostPosted: Sat Jul 23, 2011 11:50 am 
Newbie

Joined: Mon Jun 06, 2011 7:39 am
Posts: 3
I tried to imagin 3 solutions but still don't know how to do that :(
1) country e countryI18n mapped in one single class
2) country as abstract class and countryI18n extending country
3) composition between country and countryi18n without the use of collections, which in the end i think it would be the best solution

Here the code examples
1) solution 1
Code:
public class CountryI18n {
  Integer id
  String code
  String name
}


Solution 2)
Code:
public abstract class Country {
   Integer id
   String code
}
public class CountryI18n extends Country {
  String name
  Language language
}

Solution 3)
Code:
public class Country {
   Integer id
   String code
   CountryI18n country
}
public class CountryI18n {
Integer id
String name
Language language
}


Thank you all :)


Top
 Profile  
 
 Post subject: Re: Mapping two classes with @OneToMany in a table
PostPosted: Tue Jul 26, 2011 10:00 am 
Newbie

Joined: Tue Jul 26, 2011 9:53 am
Posts: 1
I am a newbie and learning hibernate . I have a similar scenario for the case highlighted above. Assume an user is entering or checking data in html and the database tables are as below

Table1(PK_Table1) -- Entry is created
Table2(PK_Table2) --- Entries are already loaded
Table3(PK_Table3) --- Data is entered in a textbox. If the same entry exists do not insert , else insert
Table4(PK_Table4 , FK_Table1 , FK_Table2) -- Contains primary key of Table1 & Table2
Table5(PK_Table5, FK_Table1 , FK_Table3) -- Contains primary key of Table1 & Table3.

The precise class structures are as below
class Table1{
@OneToMany
Set<Table4> table4Set;
@OneToMany
Set <Table5> table5Set;
}

class Table2{
}

class Table3{
}

class Table4{
@ManyToOne
private Table1;
@ManyToOne
private Table2;
}

class Table5{
@ManyToOne
private Table1;
@ManyToOne
private Table3;
}

Now my question is - How to save the data in Table1 , Table3 , Table4 , Table5 at one stretch? I am able to persist data in Table1 and Table3 when I do it seperately. But The problem I am facing is in persisting the data for Table4 and Table5 alongwith Table1 and Table3.

Regards,
Aravind


Top
 Profile  
 
 Post subject: Re: Mapping two classes with @OneToMany in a table
PostPosted: Tue Jul 26, 2011 12:23 pm 
Newbie

Joined: Mon Jan 31, 2011 3:23 pm
Posts: 5
Check the cascade-parameter of the @OneToMany-Annotation. It might have, what you are looking for

_________________
http://www.winfonet.eu


Top
 Profile  
 
 Post subject: Re: Mapping two classes with @OneToMany in a table
PostPosted: Tue Jul 26, 2011 10:12 pm 
Newbie

Joined: Tue Jul 26, 2011 10:07 pm
Posts: 1
hello...




TIN - TIN CANS - CAKE TINS

_________________
TIN CANS - CAKE TINS - TIN


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.