As I understand so far stateless sessions are preferred when using batch processes since It will just detach objects doing the process So that persistent context and cache would be free so batch processes are the ideal case for that, It is commonly known as plain JDBC Query Engine with queries translated to SQL queries immediately. Referenced from : https://stackoverflow.com/a/14174403/1460591
On the other hand, I learned that native queries do the same the one difference I see is that the stateless session can map the result to an entity, Native queries don't do that until mapper is explicitly provided.
So is there another difference and from a performance point of view which is better when doing batch processes?
If by batch processing you mean modifying entities in the database server via the SQL query itself (e.g. UPDATE things SET daily_quota=15
) then the native SQL is faster. However, in this case, you aren't loading any entities so this doesn't really seem to jive with your question.
If by batch processing you mean modifying entities in your program (e.g. load all Thing
instances, modify the dailyQuota
attribute to 15
and write an update then you will want a stateless session.
Using a native query to retrieve the objects doesn't give you any mechanism to modify the object. You still need to merge it back to the persistence context and flush those changes. Once you do this (assuming you don't have a stateless session) then it will use the classic change-detecting & cache-keeping flush mechanism.
A stateless session on the other hand gives you a way to modify entities in your program without forcing the ORM layer to go through the slow change detection process.