Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you Map String Array to JPA ?  RSS feed

 
tangara goh
Ranch Hand
Posts: 263
  • 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 ?

 
Dave Tolls
Rancher
Posts: 2913
35
  • 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: 263
  • 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: 2913
35
  • 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: 263
  • 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.
 
SunilK Chauhan
Ranch Hand
Posts: 60
  • 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: 263
  • 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.
 
Paul Clapham
Sheriff
Posts: 22480
43
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: 60
  • 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: 2913
35
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!