Hibernate by default on application start up generates and caches the CRUD statements for all the mapped entities. These statements are not used if we call queries that we have written but when we make changes on persistent context on an entity. The problem is that these auto generated statements update or insert all the properties of our entities, even the unmodifiable ones. This is not very performant and in some cases it may not even be correct. Also, when a new row is persisted in the database, the fields which have not been initialised, are set to NULL by Hibernate. In order to disable this functionality we should set these two configurations to true. Dynamic update and dynamic insert set to true means that every time that we need to update or insert an entity in the database, Hibernate will generate a statement at runtime for only the fields we are updating. Usually, these settings go hand in hand with another hibernate configuration, @SelectBeforeUpdate. It affects performance but we will be sure that the right data are being updated.
Hibernate is offering a configuration called LazyCollectionOption.EXTRA. Extra lazy collections means we can apply trivial operations to them without having to retrieve them from the database and without having to write these trivial queries to avoid doing so. Trivial operations are considered the size(), isEmpty() and contains(). If for example we have an entity User and an extra lazy collection called Address we can call user.getAddress.size() and this will generate a COUNT query on the ADDRESS table. So, no need to write a special query or load in memory the collection. Lazy loading is set by default to @OneToMany relations but you need to set it on @ManyToOne which are by default Eagerly loaded. @OneToOne can be lazy loaded only if the relation is not optional. If the relation is optional the the proxy will have to query the database to find out if it should be null or if it should be initiated. A spring boot property that needs to be overriden in all our applica...
Comments
Post a Comment