• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

HQL insert syntax to insert "nested object"

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm giving my first steps with Hibernate, I guess this must be a basic HQL syntax question.

I've been able to insert new rows using HQL in a table when all the related object properties are not objects like this:


But I don't know how I could insert a row when the related object has a property which is an object.
These are the properties the object I'm trying to insert using an HQL insert sentence:


 
Saloon Keeper
Posts: 28469
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Standard disclaimer: I do not recommend using Legacy Hibernate anymore. Use Hibernate JPA. They are very similar, but Hibernate JPA uses EntityManager instead of Session and JPQL instead of HQL. And JPA is part of the JEE standard, which Legacy Hibernate is not.

For either version of Hibernate, however, the process is the same. Annotate your Entity objects with relationship definitions. Like so:


Empleados should then have have a @ManyToOne property that corresponds to the Primary Key of their Departmento table Entity object.

When you save or update Departmento, in the same transaction you should also save or update its Empeados.

That is a crude outline of the process and I recommend you get a good book on Hibernate to help you. But I suppose the thing you must know is that HQL/JPQL is not used to save or update. The "Q" stands for Query and that's all that it does.

As far as queries go, though, you can retrieve the Empleado list automatically if you have a FetchType of "EAGER" when you fetch the Departmento. If you have FetchType of "LAZY", the Empleados will be loaded when you explicitly invoke "getEmpleados". That, also, is an over-simple explanation, though.
 
Fernando Sanchez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the answer, I'll try to follow your advice if I get time for that.

In case I wasn't clear, Empleados already had a @ManyToOne property that corresponded to the Primary Key of their Departmento table Entity object.

The reason why I was not able to execute the HQL insert sentence was because the table NuevosEmpleados from where I tried to "copy" the row did not have a FK to Departamentos table, afther adding that FK and then re-creating the project objects I managed to make this run.


Which seems to succesfully "copy one row" from NuevosEmpleados to Empleados:

INSERTADAS 1 FILAS
87654321Z                Laura             Clemence                 null 4

 
Tim Holloway
Saloon Keeper
Posts: 28469
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're missing a key point.

As I said, HQL and KPQL are query language. In an ORM such as (either version of) Hibernate, you don't do SQL-like stuff to insert or update records. Instead you use the EntityManager (or Session) save() and update() methods. There's no need for SQL-like statements, because the idea is that you are working with Objects, not SQL, and the ORM knows everything it needs to invoke the raw database functions without needing a "query language".

The executeUpdate() method would only be for something where you are doing mass table updating on a "raw SQL" basis for performance reasons without actually working with the Hibernate Entity objects themselves.
 
Fernando Sanchez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you again.

I think I'd already understood what you are explaining.

I had alredy tried instantiating, modifying, and deleting persistent objects by programming (using save, delete and update methods). And I got surprised I also had the option of inserting, updating, and deleting objects using HQL language (which I had assumed would be a mere query language). I just tried to check those three options but failed to understand how the insert sentence worked. That was the reason I asked for help although I finally got it.

It's good for me knowing from you that these options of the language must be used only for 'bulk' operations.

With regards to using pure SQL sentences from Hibernate however, I read and tried some examples using createNativeQuery method instead of createQuery (which allows pure SQL insert statements which are not permitted using  createQuery).
 
Tim Holloway
Saloon Keeper
Posts: 28469
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. The order of preference is ORM object methods (like save(), update()), HQL/JPQL, then, if neither of those options suffices, native SQL.

There are a couple of reasons for that. One is that one of the primary design goals of ORMs was to reduce/eliminate the need to know SQL, and especially the gnarly stuff like JOINs. But another is that an ORM framework can maintain caches of objects and thus operate more efficiently at scale. When you bypass those ORM functions, you also potentially bypass caching, which at best can reduce performance and at worst can result in different views of the data depending on where and how it was read/written. And that can lead to data corruption.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic