aspose file tools*
The moose likes Object Relational Mapping and the fly likes JPA: How to display OneToMany data in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA: How to display OneToMany data in servlet" Watch "JPA: How to display OneToMany data in servlet" New topic
Author

JPA: How to display OneToMany data in servlet

Asme Williams
Greenhorn

Joined: Feb 26, 2013
Posts: 16
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)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

I would connect to the database first and check that the student record you are retrieving has addresses linked to it.
Asme Williams
Greenhorn

Joined: Feb 26, 2013
Posts: 16
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

Joined: Nov 09, 2011
Posts: 1031
    
    5

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

Joined: Feb 26, 2013
Posts: 16
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

Joined: Nov 09, 2011
Posts: 1031
    
    5

Did you solve this issue Asme?
Asme Williams
Greenhorn

Joined: Feb 26, 2013
Posts: 16
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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

Joined: Feb 26, 2013
Posts: 16
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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

Joined: Feb 26, 2013
Posts: 16
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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

Joined: Feb 26, 2013
Posts: 16

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

Joined: Feb 26, 2013
Posts: 16
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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

Joined: Feb 26, 2013
Posts: 16
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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

Joined: Feb 26, 2013
Posts: 16
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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

Joined: Feb 26, 2013
Posts: 16
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

Joined: Nov 09, 2011
Posts: 1031
    
    5

Asme

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

Can you post your full JSP file please?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61768
    
  67

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

Joined: Nov 09, 2011
Posts: 1031
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA: How to display OneToMany data in servlet