As per Hibernate documentaion,
There is no difference between a view and a base table for a Hibernate mapping. This is transparent at the database level, although some DBMS do not support views properly, especially with updates. Sometimes you want to use a view, but you cannot create one in the database (i.e. with a legacy schema). In this case, you can map an immutable and read-only entity to a given SQL subselect expression 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 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.
I am not clear on the statement given for Synchronize annotation here. What is the issue with auto-flush? What is the derived entity here and why we will get stale data? How Synchronize annotation is fixing this issue.
Can someone please help me in understanding this.
@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default.
@Type annotation is for hibernate i.e. to tell what type of data do you want to store in database. Let's take a simple example: @Type(type="yes_no") private boolean isActive; Here return type is boolean but the value which gets stored in the database will be in Y or N format instead of true / false .
The @Table annotation allows you to specify the details of the table that will be used to persist the entity in the database. The @Table annotation provides four attributes, allowing you to override the name of the table, its catalogue, and its schema, and enforce unique constraints on columns in the table.
You can use Hibernate's @Subselect annotation to map an entity to an SQL query. In the following code snippet, I use this annotation to select the id, the title and the number of reviews of a Book and map them to the BookSummary entity.
Hibernate entities hold persistent state in memory. And any change to these entities is automatically made persistent to the database. But the database is not updated each time you change a field of an entity. It's done at flush time: Hibernate decides that it has to make the in-memory changes persistent and thus executes the appropriate insert, update and delete statements so that the database state matches with the in-memory state.
When does that happen?
flush()
on the sessionLet's focus on the third item here. Suppose you have an Order entity, mapped to the order table. Suppose you have loaded an Order by ID and modified its amount. The modification is only in memory. Now you execute the following query:
select sum(o.amount) from Order o
Obviously, the result of this query depends on the new amount of the modified entity. Hibernate detects this and flushes the changes to the Order entity before executing the query, to ensure the query returns the right result. If the query had been
select c from Customer c where c.name = 'John'
Hibernate would not have flushed the changes to the Order entity before executing the query, because its result doesn't depend on the new value of the order amount: the order table is not involved in this query.
Now to your question: since Hibernate doesn't have a @Table
annotation to know which table the Summary is mapped to, it doesn't know when it should auto-flush or not before a query involving the Summary entity is executed. The Synchronize annotation allows telling Hibernate: this entity gets data from the item and the bid table, so if a query is executed against the Summary entity, make sure changes to the bid and item tables are flushed before executing the query.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With