What is @Synchronize in Hibernate

According to Hibernate documentaion ,

There is no difference between a view and a base table for hibernate mapping. This is transparent at the database level, although some DBMSs do not support views properly, especially with updates. Sometimes you want to use a view, but you cannot create it in the database (i.e. with a legacy schema). In this case, you can map immutable and read-only object to a given subselect SQL statement using @ Org.hibernate.annotations.Subselect:

@Entity
@Subselect("select item.name, max(bid.amount), count(*) "
        + "from item "
        + "join bid on bid.item_id = item.id "
        + "group by item.name")
@Synchronize( {"item", "bid"} ) //tables impacted
public class Summary {
    @Id
    public String getId() { return id; }
    ...
}

      

Declare tables to synchronize this object so that automatic flushing occurs correctly and queries on derived object do not return stale data. Available both as an attribute and a nested display element.

I don't understand how to do this, for the Synchronize annotation statement. What's the problem with auto flush? What is the derived object here and why are we going to get stale data? How annotation sync fixes this issue.

Can someone please help me in understanding this.

+3


source to share


1 answer


Hibernation objects maintain a persistent state in memory. And any change to these objects automatically becomes permanent for the database. But the database is not updated every time you change the object field. This is done during cleanup: Hibernate decides that it should make the changes in memory permanent and thus executes the appropriate insert, update, and delete instructions to keep the state of the database consistent with the state in memory.

When does this happen?

  • before the transaction
  • when you explicitly call flush()

    in session
  • when executing a query with modified objects

Let's focus on the third point here. Suppose you have an Order object mapped to a table of orders. Let's say you have uploaded an order by ID and changed its quantity. Modification in memory only. Now you run the following query:

select sum(o.amount) from Order o

      



Obviously, the result of this query depends on the new amount of the changed object. Hibernate detects this and flushes the changes to the Order object before executing the query to ensure that the query returns the correct result. If the request was

select c from Customer c where c.name = 'John'

      

Hibernation would not have cleared the changes to the Order object before executing the query, because its result does not depend on the new value of the order amount: the table of orders does not participate in this query.

Now to your question, since Hibernate has no annotation @Table

to know which table the summary is being mapped to, it doesn't know when it should be automatically flushed or not, before the query associated with the Summary object is executed. Synchronize annotations let you tell Hibernate that this object gets data from the item and the rate table, so if the query is against the Summary object, make sure the changes to the rate and item tables are cleaned up before running the query.

+13


source







All Articles