• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How do you Map String Array to JPA ?  RSS feed

 
Ranch Hand
Posts: 391
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am now refactoring my code to adapt to JPA.

But, the same old problem in JDBC is now back to haunt me.

How do I map String Array to JPA, specifically using Eclipse-Link without Hibernate ?

So, I have this class - Subject that contains sub_id and subjects which is in Array.

Do I use HashMap to do it or ?

 
Rancher
Posts: 3596
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, why does your Subject class have an array of subjects?
 
tangara goh
Ranch Hand
Posts: 391
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Again, why does your Subject class have an array of subjects?



Hi Dave,

After analysing very hard and studying various other many to many relationships table, I realise I still can't run away from array.

Please see the schema for a full understanding.

database-schema-latest.jpg
[Thumbnail for database-schema-latest.jpg]
 
Dave Tolls
Rancher
Posts: 3596
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But those tables don't show that the Java side Subject should have an array in it.

A Subject has an id, a name, and (for some reason) a code.
It might (depending on how you want to model the relationships) have a List<Tutor>, but it definitely doesn't have a List<Subject name>.
Your Tutor will likely have a List<Subject>.

As an aside, there is no need for your mapping table to duplicate the columns from the Subject table.
 
tangara goh
Ranch Hand
Posts: 391
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:But those tables don't show that the Java side Subject should have an array in it.

A Subject has an id, a name, and (for some reason) a code.
It might (depending on how you want to model the relationships) have a List<Tutor>, but it definitely doesn't have a List<Subject name>.
Your Tutor will likely have a List<Subject>.

As an aside, there is no need for your mapping table to duplicate the columns from the Subject table.



I am not using a List of SubjectName but an array because I need to use ParameterValues.  Hope you get the point.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To have a List instead of an array is the better option and faster also.

Because list works well in all the scenarios. If we talk about the size than only list can change the size dynamically not the array.

So, my advise to you dear that use list all the time.
 
tangara goh
Ranch Hand
Posts: 391
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

SunilK Chauhan wrote:To have a List instead of an array is the better option and faster also.

Because list works well in all the scenarios. If we talk about the size than only list can change the size dynamically not the array.

So, my advise to you dear that use list all the time.



I need to read the Parameter Values using Array because there is no java library that can otherwise.

If List is the way, can you show me a simple example how you can read the ParameterValues using List.

Tks.
 
Sheriff
Posts: 23640
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just get the array -- which you already know how to do -- and convert that array to a List. Use the Arrays.toList() method to do that.
 
SunilK Chauhan
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Paul,

Addition to comment that We can convert Array to List using the following formula.



Consider this just for example.
Here you will get List as data.
 
Dave Tolls
Rancher
Posts: 3596
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is still avoiding the point I made at the top, which is that a Subject class should not have an array or List of Subjects in the first place.
It makes no sense in terms of what is being modelled.
 
tangara goh
Ranch Hand
Posts: 391
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Just get the array -- which you already know how to do -- and convert that array to a List. Use the Arrays.toList() method to do that.



Hello,

I am coming back to my old problem.

As per your advice, I did the following :

List<Subject> subjMany =  Arrays.asList(subject);

But, it is not working :(

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!