Creating an instance of a new object in sleep mode using an SQL query

I am trying to create an object in hibernate using a query, which will then be saved back to a table representing the class.

Excerpt from the hbm.xml file:

<class name="MyClass" table="MY_TABLE"> <id column="ID" name="ID"> <generator class="sequence"> <param name="sequence">MY_SEQ</param> </generator> </id> <property column="VAL" name="val" type="string"/> </class> <sql-query name="myQuery"> <return class="MyClass"/> SELECT MY_SEQ.nextval ID, 'something' VAL from DUAL </sql-query> 

Code snippet from test case:

 MyClass myClass = (MyClass) session.getNamedQuery("myQuery").list().get(0); Transaction t = session.beginTransaction(); session.save(myClass); t.commit(); 

My goal is that a new record should appear in the table MY_TABLE, but the insertion does not occur, I assume that this is due to the fact that Hibernate does not know that the instance was not saved in db.

I tried changing the read request:

 SELECT NULL ID, 'something' VAL from DUAL 

But this leads to the fact that Hibernate does not instantiate the object.

So, how can I create an instance of a new object from a query that is not associated with a constant instance of the class and use it to create a persistent instance?

+4
source share
1 answer

Update:. I tested the approach suggested below, and I couldn't get it to work for this particular scenario, Hibernate expects you to select columns for all attributes, while we definitely do not want an identifier. However, using ResultTransformer really worked:

16.1.5. Returning Unmanaged Objects

You can apply ResultTransformer to native SQL queries, allowing it to return ResultTransformer objects.

 sess.createSQLQuery("SELECT NAME, BIRTHDATE FROM CATS") .setResultTransformer(Transformers.aliasToBean(CatDTO.class)) 

The specified request:

  • SQL query string
  • result transformer

The above query will return the CatDTO list that was created and entered the NAME and BIRTHNAME values ​​in the corresponding properties or fields.

The documentation mentions returned unmanaged objects, but it also works with the entity (there is no reason why it will not work), and I could persist transition object successfully.

see also

I leave the original answer for the sake of clarity.


Perhaps the following will help:

16.1.2. Entity Requests

The above questions concerned returning scalar values, mostly returning "raw" values ​​from a ResultSet. The following shows how to get object objects from a native sql query through addEntity() .

 sess.createSQLQuery("SELECT * FROM CATS").addEntity(Cat.class); sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").addEntity(Cat.class); 

The specified request:

  • SQL query string
  • the object returned by the request

Assuming Cat is displayed as a class with column identifiers, NAME and BIRTHDATE, the above queries will return a list in which each item is a Cat Object.

If an object is mapped to a many-to-one other object, it is also required to return this when executing its own query, otherwise the database "column not found" error. Additional columns will be automatically returned when using * notation, but we prefer to be explicit, as in the following many-to-one Dog example:

 sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE, DOG_ID FROM CATS").addEntity(Cat.class); 

This will allow cat.getDog() to function properly.

But I don’t think you should set the ID if you want to save it and you want Hibernate to perform the insertion. Strike>

+5
source

Source: https://habr.com/ru/post/1315946/


All Articles