Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Thursday, December 9, 2010

How to map to a database view in Hibernate

There is no difference between a view and a base table for a Hibernate mapping. This is transparent at the database level.


<class name="Summary">
<subselect>
select item.name, max(bid.amount), count(*)
from item
join bid on bid.item_id = item.id
group by item.name
</subselect>
<synchronize table="item"/>
<synchronize table="bid"/>
<id name="name"/>
...
</class>

Declare the tables to synchronize this entity with, ensuring that auto-flush happens correctly and that queries against the derived entity do not return stale data. The is available both as an attribute and a nested mapping element.

Tuesday, November 30, 2010

Differences In Hibernate

Difference between Save and Persist

persist() makes a transient instance persistent. However, it does not guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.

save() does guarantee to return an identifier. If an INSERT has to be executed to get the identifier ( e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is problematic in a long-running conversation with an extended Session/persistence context

Differences between Load and Get

load() vs. get() :-

load()

get()

Only use the load() method if you are sure that the object exists.

If you are not sure that the object exists, then use one of the get() methods.

load() method will throw an exception if the unique id is not found in the database.

get() method will return null if the unique id is not found in the database.

load() just returns a proxy by default and database won’t be hit until the proxy is first invoked.

get() will hit the database immediately.

Difference between Update and Merge

Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session