@@ -3,12 +3,12 @@
|
||||
|
||||
This section describes how to persist (save) entities with Spring Data JPA.
|
||||
|
||||
[[jpa.entity-persistence.saving-entites]]
|
||||
[[jpa.entity-persistence.saving-entities]]
|
||||
== Saving Entities
|
||||
|
||||
Saving an entity can be performed with the `CrudRepository.save(…)` method. It persists or merges the given entity by using the underlying JPA `EntityManager`. If the entity has not yet been persisted, Spring Data JPA saves the entity with a call to the `entityManager.persist(…)` method. Otherwise, it calls the `entityManager.merge(…)` method.
|
||||
|
||||
[[jpa.entity-persistence.saving-entites.strategies]]
|
||||
[[jpa.entity-persistence.saving-entities.strategies]]
|
||||
=== Entity State-detection Strategies
|
||||
Spring Data JPA offers the following strategies to detect whether an entity is new or not:
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ class CdiConfig {
|
||||
|
||||
In the preceding example, the container has to be capable of creating JPA `EntityManagers` itself. All the configuration does is re-export the JPA `EntityManager` as a CDI bean.
|
||||
|
||||
The Spring Data JPA CDI extension picks up all available `EntityManager` instances as CDI beans and creates a proxy for a Spring Data repository whenever a bean of a repository type is requested by the container. Thus, obtaining an instance of a Spring Data repository is a matter of declaring an `@Injected` property, as shown in the following example:
|
||||
The Spring Data JPA CDI extension picks up all available `EntityManager` instances as CDI beans and creates a proxy for a Spring Data repository whenever a bean of a repository type is requested by the container. Thus, obtaining an instance of a Spring Data repository is a matter of declaring an `@Inject` property, as shown in the following example:
|
||||
|
||||
[source, java]
|
||||
----
|
||||
|
||||
@@ -37,5 +37,5 @@ A plain JPA setup requires all annotation-mapped entity classes to be listed in
|
||||
----
|
||||
====
|
||||
|
||||
NOTE: As of Spring 3.1, a package to scan can be configured on the `LocalContainerEntityManagerFactoryBean` directly to enable classpath scanning for entity classes. See the link:{springJavadocUrl}org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.html#setPackagesToScan(java.lang.String...)$$[JavaDoc] for details.
|
||||
NOTE: As of Spring 3.1, a package to scan can be configured on the `LocalContainerEntityManagerFactoryBean` directly to enable classpath scanning for entity classes. See the link:{springJavadocUrl}/org/springframework/orm/jpa/LocalContainerEntityManagerFactoryBean.html#setPackagesToScan(java.lang.String...)$$[JavaDoc] for details.
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ Generally, the query creation mechanism for JPA works as described in {spring-da
|
||||
|
||||
.Query creation from method names
|
||||
====
|
||||
[source, java]
|
||||
----
|
||||
public interface UserRepository extends Repository<User, Long> {
|
||||
|
||||
@@ -52,7 +53,7 @@ The following table describes the keywords supported for JPA and what a method c
|
||||
|`After`|`findByStartDateAfter`|`… where x.startDate > ?1`
|
||||
|`Before`|`findByStartDateBefore`|`… where x.startDate < ?1`
|
||||
|`IsNull`, `Null`|`findByAge(Is)Null`|`… where x.age is null`
|
||||
|`IsNotNull`, `NotNull`|`findByAge(Is)NotNull`|`… where x.age not null`
|
||||
|`IsNotNull`, `NotNull`|`findByAge(Is)NotNull`|`… where x.age is not null`
|
||||
|`Like`|`findByFirstnameLike`|`… where x.firstname like ?1`
|
||||
|`NotLike`|`findByFirstnameNotLike`|`… where x.firstname not like ?1`
|
||||
|`StartingWith`|`findByFirstnameStartingWith`|`… where x.firstname like ?1` (parameter bound with appended `%`)
|
||||
@@ -76,7 +77,7 @@ For example, `select distinct u from User u` will produce a complete different r
|
||||
In the first case, since you are including `User.id`, nothing will duplicated, hence you'll get the whole table, and it would be of `User` objects.
|
||||
|
||||
However, that latter query would narrow the focus to just `User.lastname` and find all unique last names for that table.
|
||||
This would also yield a `List<String>` result set instead of a `List<User`> result set.
|
||||
This would also yield a `List<String>` result set instead of a `List<User>` result set.
|
||||
|
||||
|
||||
`countDistinctByLastname(String lastname)` can also produce unexpected results.
|
||||
@@ -130,7 +131,7 @@ The query has a special name that is used to resolve it at runtime.
|
||||
|
||||
[[jpa.query-methods.named-queries.declaring-interfaces]]
|
||||
=== Declaring Interfaces
|
||||
To allow these named queries, specify the `UserRepositoryWithRewriter` as follows:
|
||||
To allow these named queries, specify the `UserRepository` as follows:
|
||||
|
||||
.Query method declaration in UserRepository
|
||||
====
|
||||
|
||||
Reference in New Issue
Block a user