• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA, one-to-many list is always null  RSS feed

 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, i am trying to make a simple database using JPA, and i just can't figure out what i'm doing wrong. I have two entites, Account and Person, where one Person can have many accounts, and one account can only have one person. My problem is that the list i use in Person to store the account is never initialized. I know it is initialized when fed to the database, and when i query the database i get data returned as expected, except the ArrayList<Account> account, which is always null. Can anyone please help me out?

Person entity:


Account entity:


EntityManager:


I can add more code if necessary, but hopefully it is enough. If i now add a person:

And query this person, i get all relevant data - except the accounts list, which is null. Any idea why?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Gibbins wrote:And query this person...

As the query is the problem, perhaps you should show us this code. And also, the schema for the tables.

I assume there is data in the tables that relate multiple accounts to the person?

And I'm bit confused by your explanation. Generally, the object is created by the query. Why are you creating a new one by hand before the query?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nit: a list of accounts should probably be named accounts, rather than account.
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As the query is the problem, perhaps you should show us this code. And also, the schema for the tables.

A query is done like this:


As for the schema, im not sure how to post it, isen't it inferred from my initial post?


I assume there is data in the tables that relate multiple accounts to the person?

Not in the example i posted, but in the real db there is, yes. But i don't see how that matter? Shouldn't the list be initialized but empty if there is no account?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it work properly when there is data to populate the list?
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Does it work properly when there is data to populate the list?


No, the list is always null
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Gibbins wrote:As for the schema, im not sure how to post it, isen't it inferred from my initial post?

Not really. That's your mapping of the schema. Given that there's a problem, it's possible that there's something awry with the mapping. Seeing the actual schema from the point of view of the DB can help us see if it's a mapping problem.
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Tom Gibbins wrote:As for the schema, im not sure how to post it, isen't it inferred from my initial post?

Not really. That's your mapping of the schema. Given that there's a problem, it's possible that there's something awry with the mapping. Seeing the actual schema from the point of view of the DB can help us see if it's a mapping problem.


Okay, fair point. But i'm not sure how to view that..
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh by the way, the persistence.xml is like this:


I don't know much about how this file works really, could the problem be here?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Gibbins wrote:But i'm not sure how to view that..

If you are working with databases you should have a DB manipulating tool on hand. Some good paid ones are DbVisualizer or DataGrip. Squirrel SQL is still be available and is free (donate-ware), albeit a bit more primitive. I'm sure there are many more to choose from.

These tools are essential to not only viewing database schemas, but can help you manipulate the data and the schemas.
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out, if i change


to


it works. Not sure i understand why, but "List" was the problem
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a fix, that's a kludge.

After you get the collection back, what is its type? (use the getClass() method)
 
Tom Gibbins
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:That's not a fix, that's a kludge.

After you get the collection back, what is its type? (use the getClass() method)


Collection class: org.apache.openjpa.util.java$util$ArrayList$proxy

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like it's an ArrayList implementation and should be assignable to List.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!