I just saw that the 4th candidate got released for Hibernate 5. What's new in 5 compared to earlier versions?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Some exciting features has been added/enhanced in Hibernate 5.x. Have a quick look.
1. Hibernate Search
For more details on Hibernate Search view this.
2. Hibernate Validator
Hibernate Validator comes with a handful of built-in validation rules like Email, Length, NotBlank etc.
For more details on Hibernate Validator view this.
3. Improved Java 8 Support
Java 8 date/time data types (JSR 310) are supported and can be validated via @Past and @Future. Also Optional and JavaFX types are supported via an improved ValidatedValueUnwrapper.
4. Hibernate OGM
Just released the first stable version.
5. Bootstrapping API
New bootstrapping API - better determinism, better integration
A few other things:
Check Hibernate ORM Roadmap for more details.
There's a long list of things that have been changed in Hibernate 5:
New bootstrap API so we can bootstrap a JPA environment programmatically without the need of a
persistence.xml
file.Starting in 5.0 Hibernate Spatial is part of the Hibernate project so we can handle GIS data too.
The Java 8 Date and Time types are supported in domain model mappings. The mapping between the standard SQL Date/Time types and the supported Java 8 Date/Time class types looks as follows;
java.time.LocalDate
java.time.LocalTime
,java.time.OffsetTime
java.time.Instant
,java.time.LocalDateTime
,java.time.OffsetDateTime
andjava.time.ZonedDateTime
The bytecode enhancement mechanism was redesigned from scratch, and Hibernate features both a Maven and a Gradle plugin. There are three main aspects which we can enhance with bytecode instrumentation:
Lazy initialization: Fields can be declared as
LAZY
and they will be fetched only when being accessed for the first time.Dirty checking: Entities are enhanced so that they can keep track of all the properties that get changed after being loaded in a Persistence Context.
Bidirectional associations: It's possible to synchronize both sides of a bidirectional association automatically, even if the developer only updates a single side.
Hibernate's native APIs (
Session
, etc) have been updated to use generic types. No need to cast when fetching entities.Hibernate 5.0 extends this to a broader set of types (e.g.
UUID
).Second-level cache by reference. This feature enables direct storage of entity references into the second level cache for immutable entities.
Starting with Hibernate 5.0, we have a completely new User Guide that was written from scratch.
Hibernate 5.1 adds the following features:
Hibernate 5.2 adds support for:
Query.stream()
Session
extendsEntityManager
so you can get access to all JPA methods right from aSession
Timestamp
andTime
hibernate.connection.provider_disables_autocommit
resource-local transaction optimization.There are 5 New features in Hibernate 5
1. Support classes of the Date and Time API as BasicTypes The new Date and Time API was one of the most anticipated changes in Java 8. The old java.util.Date has a lot of issues which got finally fixed.
Unfortunately, JPA 2.1 and Hibernate 4 don’t provide direct support for it. But that’s not a huge issue. It just takes a few lines of code to implement an AttributeConverter that maps a LocalDate.
But obviously, the explicit support as a BasicType is still a lot better. Hibernate implemented that in version 5.0. Since then you don’t need any additional annotations or converter to persist the classes of the Date and Time API. You can use them in the same way as any other supported attribute types.
2. Get query results as a Stream Introducing a new method to give you your query result as a Stream doesn’t sound like a big thing. But the stream method of Hibernate’s Query interface provides an additional benefit that makes it especially interesting for huge result sets. It fetches the result set in multiple batches and uses Hibernate’s ScrollableResults implementation to scroll through it. This approach is a great fit if you use a Stream to process the result set records one by one and helps you to implement your use case efficiently.
You can use the new method since Hibernate 5.2 to get your query results. The following code snippet shows a simple example that selects all Book entities from the database and processes them as a Stream.
3. Fetch multiple entities by their primary key
Fetching multiple entities by their ID is a very common use case. Most developers either implement it with a loop that calls the find method of the EntityManager for each primary key or with a JPQL query that checks all primary key values in an IN clause. The first option requires Hibernate to perform a database query for each primary key. That can create huge performance issues. The second one allows you to fetch all entities with one query and is obviously the better option.
Hibernate 5.1 introduced a third option that avoids the issues of the first and is easier to use than the second one. The new MultiIdentifierLoadAccess interface provides a comfortable option to load multiple entities with one query. You just need to call the byMultipleIds method on the Hibernate Session to get a MultiIdentifierLoadAccess interface and provide a list of primary key values to the multiLoad method. Hibernate’s implementation also provides an additional advantage: It splits huge lists of primary key values into multiple batches. This is sometimes required because some databases limit the number of elements in an IN clause.
4. Join unassociated entities in a query
You can easily join mapped associations between entities in JPQL queries. The mapping already provides the required join conditions, and you don’t need to provide them in your query.
But what about entities that map associated database tables but have no mapped association?
And that’s not a rhetorical question.
Most entity models don’t map all the possible associations. They only map the ones that seem to provide value in the domain model and not the ones where 2 database tables (seemingly by accident) store the same foreign key. It also happens quite often that a to-many association with lots of records on the many side doesn’t get mapped with Hibernate. The risk that someone calls the getter of the association and fetches several hundred or thousand entities is just too high.
That’s totally fine as long as you just look at the domain model. You probably don’t need these associations in the model. But that doesn’t mean that you don’t need them in one of your JPQL queries.
If that’s the case, you have 3 options:
1 Model the association between the entities or
2 Use a cross join in your JPQL query which might create performance issues or
3 Use Hibernate’s proprietary JOIN clause. I prefer option 3. It’s the easiest and most natural one.
Since Hibernate 5.1, you can use an SQL-like syntax to join entities without a modeled association.
5. @Repeatable annotations
Repeatable annotations are one of the smaller changes in Java 8. It allows you to annotate a class, attribute or interface with the same annotation multiple times. A typical JPA example in which you want to do that is defining multiple named queries for an entity.
So far, you had to annotate your entity with a @NamedQueries annotation which contained an array of @NamedQuery annotations. The annoying thing about that is that the @NamedQueries annotation is just a container. It doesn’t provide any value on its own. Since Hibernate 5.2, you don’t have to do that anymore. At least not as long as you use Hibernate’s version of the org.hibernate.annotations.NamedQuery annotation. @NamedQuery and a lot of other Hibernate annotations are now repeatable and can be assigned multiple times. As you can see in the following code snippet, that makes the code easier to read and is much more comfortable to use.
Creadit/Source : https://www.thoughts-on-java.org/5-new-features-hibernate-5-every-developer-know/