Update single field using spring data jpa

You can try something like this on your repository interface: @Modifying @Query(“update EARAttachment ear set ear.status = ?1 where ear.id = ?2”) int setStatusForEARAttachment(Integer status, Long id); You can also use named params, like this: @Modifying @Query(“update EARAttachment ear set ear.status = :status where ear.id = :id”) int setStatusForEARAttachment(@Param(“status”) Integer status, @Param(“id”) Long id); The … Read more

Why use returned instance after save() on Spring Data JPA Repository?

The save(…) method of the CrudRepository interface is supposed to abstract simply storing an entity no matter what state it is in. Thus it must not expose the actual store specific implementation, even if (as in the JPA) case the store differentiates between new entities to be stored and existing ones to be updated. That’s … Read more

What RuntimeException can Spring CrudRepository throw?

Spring has built-in exception translation mechanism, so that all exceptions thrown by the JPA persistence providers are converted into Spring’s DataAccessException – for all beans annotated with @Repository (or configured). There are four main groups – NonTransientDataAccessException – these are the exceptions where a retry of the same operation would fail unless the cause of … Read more

Pagination in Spring Data JPA (limit and offset)

Below code should do it. I am using in my own project and tested for most cases. usage: Pageable pageable = new OffsetBasedPageRequest(offset, limit); return this.dataServices.findAllInclusive(pageable); and the source code: import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; import org.apache.commons.lang3.builder.ToStringBuilder; import org.springframework.data.domain.AbstractPageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import java.io.Serializable; /** * Created by Ergin **/ public class OffsetBasedPageRequest implements Pageable, … Read more

How to generate a ddl creation script with a modern Spring Boot + Data JPA and Hibernate setup?

Ah, right after I posted this question a section of the spring data docs caught my eye: 73.5 Configure JPA properties In addition all properties in spring.jpa.properties.* are passed through as normal JPA properties (with the prefix stripped) when the local EntityManagerFactory is created. So, to answer my own question: prefix the javax.persistence properties with … Read more

Spring Data optional parameter in query method

I don’t believe you’ll be able to do that with the method name approach to query definition. From the documentation (reference): Although getting a query derived from the method name is quite convenient, one might face the situation in which either the method name parser does not support the keyword one wants to use or … Read more

Spring Data JPA Update @Query not updating?

The EntityManager doesn’t flush change automatically by default. You should use the following option with your statement of query: @Modifying(clearAutomatically = true) @Query(“update RssFeedEntry feedEntry set feedEntry.read =:isRead where feedEntry.id =:entryId”) void markEntryAsRead(@Param(“entryId”) Long rssFeedEntryId, @Param(“isRead”) boolean isRead);

How to map Page to Page in spring-data-rest

You can still use the Page.map without lambda expressions: Page<ObjectEntity> entities = objectEntityRepository.findAll(pageable); Page<ObjectDto> dtoPage = entities.map(new Converter<ObjectEntity, ObjectDto>() { @Override public ObjectDto convert(ObjectEntity entity) { ObjectDto dto = new ObjectDto(); // Conversion logic return dto; } });

How to manually force a commit in a @Transactional method? [duplicate]

I had a similar use case during testing hibernate event listeners which are only called on commit. The solution was to wrap the code to be persistent into another method annotated with REQUIRES_NEW. (In another class) This way a new transaction is spawned and a flush/commit is issued once the method returns. Keep in mind … Read more