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

Hibernate loads only first element of the collection

 
Jay Chmiel
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use Springs SimpleFormController to write Post objects into database:



Then i load the blog using my dao in another controller:



Then finally i print all the posts from the blog in the jsp:



The problem is that the collection of posts always contains only one post, although i can see that there are more in database. Seems that not all are loaded from db. Any suggestions will be appreciated...

 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First my architecture part I don't like.

I would not put DAOs and calls to them in a Controller. I would always have a Service class in between the two. So Controller gets a Service class injected and the Service class has a DAO injected and the service class calls the DAO method. It just leaves room for enhancements, maintenance (You always know which level has what), and also scalability. Enchancements could be more code for the use case in the service, also you can now expose your service to other remoting technologies like Web Services, RMI, HttpInvoker, etc.

Now to you real problem. Can you remote debug and check line 9 where you call blogDao.getBlog(blogId) and stop past the line and examine the blog object in your debugger. This way you can seperate Hibernate from your Web code to see what is the real root cause of the problem. Also can you post your code that is in the getBlog() method so that we can see your query you are running, but only if you find after you remote debug that the blog object returned doesn't have the correct data.

Mark
 
Hong Anderson
Ranch Hand
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding architecture, actually DAO is a kind of service, it's data access service.

Even using DAO, the implementation can be anything including Web Services, RMI, etc., because we use interface, not concrete class.
But I don't like DAO pattern, I prefer to use Repository (Domain-Driven Design).
 
Jay Chmiel
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spritzler wrote:
Can you remote debug and check line 9 where you call blogDao.getBlog(blogId) and stop past the line and examine the blog object in your debugger.


Well there is the System.out statement i used for debug. Its line 12:



It shows that size of collection is 1, so there is something wrong with it already.

Mark Spritzler wrote:
Also can you post your code that is in the getBlog() method so that we can see your query you are running, but only if you find after you remote debug that the blog object returned doesn't have the correct data.




Any ideas?
 
Hong Anderson
Ranch Hand
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you have data in database?
Can you also post your mapping file?

BTW, this code looks strange to me:


BlogDao should save blog, not save post.
It should be like this:

 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj Sathianpantarit wrote:Regarding architecture, actually DAO is a kind of service, it's data access service.

Even using DAO, the implementation can be anything including Web Services, RMI, etc., because we use interface, not concrete class.
But I don't like DAO pattern, I prefer to use Repository (Domain-Driven Design).


Sorry. a DAO/Repository is a Data Access Object, not a Use Case Service class. Service or business logic can use DAOs, but a DAO can never be considered business logic, if you try to merge the meaning of DAO with Service then you are just getting into low cohesion of your code and in OO and architecture that is not a good thing to do. And DAO and Repository is the same type of class. And yes I prefer the term Repository now too.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Chmiel wrote:
Mark Spritzler wrote:
Can you remote debug and check line 9 where you call blogDao.getBlog(blogId) and stop past the line and examine the blog object in your debugger.


Well there is the System.out statement i used for debug. Its line 12:



It shows that size of collection is 1, so there is something wrong with it already.

Mark Spritzler wrote:
Also can you post your code that is in the getBlog() method so that we can see your query you are running, but only if you find after you remote debug that the blog object returned doesn't have the correct data.




Any ideas?


OK, so you are using "get" here, that would probably explain a bit more why you are not seeing what you expect to see. There seems to be a few things going on here that makes it tougher to figure out exactly where your problem is.

1) Is this a two directional association, I notice you set the Blog to the Post object but not adding the Post object to the Blog's Post Collection. So in those terms I kind of see only one Post getting associated to the Blog, and not many Posts to the Blog.

2) I agree with Kengkaj last comment about passing a Post object to the BlogDAO.
His second code is almost correct, and it depends on the bi-directionality, you might have one more line of code to set the other side. like

newPost.setBlog(blog);

I highly recommend you learn to "remote" debug you code and step through it rather than System.out.printlns. You will find your problems a lot quicker and it is pretty easy to set up. If you are using an IDE, then it is already built in.

At this point, it is becoming more obvious, that this isn't a Spring question, but a Hibernate question, so I am going to move this thread to the more appropriate Object Relational Mapping forum where Hibernate questions are posted.

Thanks

Mark
 
Hong Anderson
Ranch Hand
Posts: 1936
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spritzler wrote:
Kengkaj Sathianpantarit wrote:Regarding architecture, actually DAO is a kind of service, it's data access service.

Even using DAO, the implementation can be anything including Web Services, RMI, etc., because we use interface, not concrete class.
But I don't like DAO pattern, I prefer to use Repository (Domain-Driven Design).


Sorry. a DAO/Repository is a Data Access Object, not a Use Case Service class. Service or business logic can use DAOs, but a DAO can never be considered business logic, if you try to merge the meaning of DAO with Service then you are just getting into low cohesion of your code and in OO and architecture that is not a good thing to do. And DAO and Repository is the same type of class. And yes I prefer the term Repository now too.

I say, DAO/Repository is a kind of service (data access service), I didn't say that they are "Use Case Service" or business logic. But I know your point and I agree with you. Our argument is caused from differently interpreting meaning of "service" in context.
Generally, Services (especially in Domain-Driven Design) should contain domain logic (for non-DDD Services usually refer to application services).
But we can use word "service" for other things as well.

[Off-topic] Just want to note that Repository is not just a term, it's not the same with DAO (you might have already known).

My point is it's not relate to Web Services, RMI, etc., Domain Service, Application Service, DAO, Repository can use Web Services, RMI, etc. in implementation. Because they are just interfaces.
 
Jay Chmiel
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj Sathianpantarit wrote:
BlogDao should save blog, not save post.
It should be like this:



Ok, so i changed the code. Now I get the stacktrace on blogDao.save(blog) line:

It laso happens when i add the line newPost.setBlog(blog); as Mark Spritzler advised.

Kengkaj Sathianpantarit wrote:
Are you sure you have data in database?
Can you also post your mapping file?


Yes, im sure that the posts are in DB and they have blog_id set properly. Here goes my mapping file:



Thanks for all of you guys for helping me.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic