• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA: How to display OneToMany data in servlet

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to class : Student and Address , with a OneToMany relationship, so every student can have one or multiple address,

Now I try to display a given student details (its Student + its Address details) in a JSP page but only the Student information are displayed and not its address.

here is the JSP and the sevlet page:





the JSP page:



The Student class:



And the Address class:





EDIT:

here is the problem; the address part is not loaded.



1: - So now How can I print the addresses in the servlet

So now how can I check first if addresses are being loaded before student object is passed to the jsp?
A friend tell me that I can printing the addresses in the servlet but I dont know how to do that.

2: - is the Jsp code ok to display the an student's address?

(am new in the JEE world)
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Learn about logging now. Right now. Do not pass GO, do not collect $200, until you know how to use logging to help debug your applications. Log4J is a popular choice, or you can use the builtin java.util.logging.

 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would connect to the database first and check that the student record you are retrieving has addresses linked to it.
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:I would connect to the database first and check that the student record you are retrieving has addresses linked to it.



Yes each student have already one address,
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, either by logging or running your code in debug, confirm the size of the address list for the student entity straight after you have retrieved it.

If the list is empty, I suspect lazy loading may be the issue. In which case, you could try changing your fetch type to EAGER.
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:Now, either by logging or running your code in debug, confirm the size of the address list for the student entity straight after you have retrieved it.



Well as a newbi I don't know how to run the code in debug...

but I changed the fetch type to EAGER but nothing.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you solve this issue Asme?
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:Did you solve this issue Asme?



Well not yet unfortunately, am still waiting for someone to help me, you've an solution?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you added logging to your application? Sitting around and waiting for other people to do your work isn't going to help you progress much.
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Have you added logging to your application?



Well I don't know what you mean by added logging ...

Bear Bibeault wrote:Sitting around and waiting for other people to do your work isn't going to help you progress much.



So what I am supposed to do for you to help me?

sorry, my english is not that good,
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Asme Williams wrote:Well I don't know what you mean by added logging ...


Logging is a way to display debug output in a controlled fashion.

See java.util.logging or Log4J.

So what I am supposed to do for you to help me?,


ShowSomeEffort. Finding out if the missing data is really there or not is the first priority.
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So what I am supposed to do for you to help me?,


ShowSomeEffort. Finding out if the missing data is really there or not is the first priority.

Well yes, it is here,
if I put this code on the JSP page: <h1>${student.addresses}</h1>
well It give me the address details but in that format: {[DavidStreet, DavidCity, DavidCountry]}



 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so it looks like the street, city and country are in an array or list? If so, think about that. Is that a good way to represent the data?

Your EL expressions try to treat those values as properties -- which is what I would expect. Why isn't the data arranged accordingly?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. So it's looking like the issue is poor choices for data modeling. This does not mean that you should not look into learning how to use logging. It is an essential development tool.
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Your EL expressions try to treat those values as properties -- which is what I would expect. Why isn't the data arranged accordingly?



Well I don't know and that's my problem; I mean I'd like to be able to display every Address attribut alone...
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:P.S. So it's looking like the issue is poor choices for data modeling.



O what you suggest me?
and in case this may help here is a link of the source on Dropbox
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer this: why are those values in a list? The first name isn't in a list, why would city be in a list?
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Answer this: why are those values in a list? The first name isn't in a list, why would city be in a list?



Well ok:
It was a simple CRUD web app, where I can add Student; So I decided to add another Address class and use a OneToMany relationship so that every student can have one or many Addresses, that's why I used a list of addresses....

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a one to many, but the list should be of address elements that contain the properties of an address, not the individual elements of one address.
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You can use a one to many, but the list should be of address elements that contain the properties of an address, not the individual elements of one address.



ok So what I must do?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to figure out if the data is loading correctly. At first glance, it looks like the list should be off addresses, but you need to figure out why the list contains city, state and country instead. You JSP output does not seem to jive with the code -- but I haven't taken a good and close look at it.
 
Asme Williams
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You need to figure out if the data is loading correctly. At first glance, it looks like the list should be off addresses, but you need to figure out why the list contains city, state and country instead. You JSP output does not seem to jive with the code -- but I haven't taken a good and close look at it.



Well thank you very much, I really appreciate your help but, I think I'll just give up ... It's too complicated, I've try to search for a JPA CRUD web app tutorial with a OneToMany relationship but dont find nothing....

Thank you again.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Asme

Giving up is not going to get you anywhere when working with software.

Can you post your full JSP file please?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest I think that you are taking a rather big bite all at once, which is making it seem overwhelming.

I'd concentrate on getting the DAO fully created and tested to produce the data in a reasonable format. All this worrying about in the JSP is just a distraction until the data layer is operating as expected.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the data is being returned correctly (assuming each student has exactly one address). However, I agree, testing this first is a very good idea and should take priority over any problems in the presentation tier.

Asme, assuming the data is being returned correctly, I would revisit your for each tags in the JSP as they don't look correct. It would be good to see the entire file though.
 
reply
    Bookmark Topic Watch Topic
  • New Topic