• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Get children for any level in tree structure but without theirs children using Hibernate?

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Folder entity in Hibernate, like so:



I'm trying to write a finder method or custom query which will do what I wrote in the subject.

So if I send a request going like folders/{parent_folder_id}, let's say value being 1, I should get objects 4 and 5, but without their children, so not including 6,7,8 and 9.

Ideally, hibernate query would be preferred. If not, any sql language is also fine. I'll try to tumble it up to hibernate.

This is what I got, I still get children...




example.png
[Thumbnail for example.png]
 
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use EAGER fetch. Use LAZY. Lazy fetches only read in related objects if you explicitly ask for them - and if the parent object is connected. Eager fetches pull in everything at once.

Eager fetching should be used with great care, since they can easily attempt to pull the entire database into RAM at one time.
 
Mike Gosling
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried with Lazy fetch, no luck. I'm new to Hibernate and ORMs. Getting error that query is not well formed.

 
Tim Holloway
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you doing native queries?
 
Mike Gosling
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, I still haven't looked up about native queries yet, I think I saw it somewhere in TA's project and I just copy-paste it..Not very nice of me
 
Tim Holloway
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally you'd only do a native query when JPQL couldn't express what you needed to do. JPQL is generally much simpler, more portable, and easier for the JPA framework to optimize.
 
Mike Gosling
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim, I'll look it up. For now, I was I able to get what I need in MySQL Workbench. But there is no way to tell that to Hibernate.

I have these query I it gets me what I need:



But when I type this:

in Hibernate, I get error saying:



In model I just placed Lazy instead of Eager and set @JsonIgnore on parent.

 
Tim Holloway
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JPQL, the query would be something like this:
 
Mike Gosling
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got what I need, that is, name of the folder and its Id. I did it with following query:

   @Query(value = "Select folder_id, name, parent From folders f Where f.parent = ?1", nativeQuery = true)



If I were to left out nativeQuery = true, I got:

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: folders is not mapped [Select folder_id, name, parent From folders f Where f.parent = ?1]





 
Tim Holloway
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you're fetching fields. Normally in JPA, you'd fetch the Entity (record). Look closely at my JPQL example.

As a general rule, you want the whole record (although you'd usually Lazy-fetch its relatives) when using JPA and only do selective field fetching if your really only wanted a few fields from a very complex Entity.
 
Mike Gosling
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Tim, you are right. But I cannot make your idea work.

In my repository if I do like this (what you suggested):



I get error 500, saying:

If I ommit nativeQuery = true:



I get

Now, I don't see a way how to get around this, and to get Entity directly as a record? I'll be sending these as JSON to andorid and will have to eventually create Folder object there anyway...

Wooo, my 100th post LOL Thank you Code Ranch very much, planning on at least couple of hundred posts more
 
Tim Holloway
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because that is not a Native Query. JPQL and SQL have similar, but not identical syntax.

Incidentally, when a DAO or other repository provides an entity retrieval method, the naming convention is not "getXXX", it's "findXXX". It avoids confusion with true "getter" methods, since a finder method does active work (a search), but a get method normally retrieves information local to the object that you do the "get" on.
 
Mike Gosling
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim once again. I'll get my hands on some quality book on Spring and Hibernate, so I can learn all that sh** Tnx
 
Tim Holloway
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you were using any Spring code there, but Spring is certainly worth knowing.

More important than learning "Hibernate", though, is learning JPA - the Java Persistence Architecture.

Hibernate comes in 2 flavors - legacy Hibernate and Hibernate JPA. JPA was designed in large part by hibernate.org, but it does have some differences from the original Hibernate.

JPA, on the other hand, is now a JEE standard, and it's not only available from Hibernate, but from a variety of other sources, including Apache OpenJPA.
 
brevity is the soul of wit - shakepeare. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic