Delete Not Working with JpaRepository

Most probably such behaviour occurs when you have bidirectional relationship and you’re not synchronizing both sides WHILE having both parent and child persisted (attached to the current session). This is tricky and I’m gonna explain this with the following example. @Entity public class Parent { @Id @GeneratedValue(strategy = IDENTITY) @Column(name = “id”, unique = true, … Read more

Create filter aggregation in spring

You can try below query. Static Imports import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.filter; import static org.springframework.data.mongodb.core.aggregation.ComparisonOperators.Eq.valueOf; Code Aggregation aggregation = newAggregation( project().and(filter(“parts”) .as(“item”) .by(valueOf( “item.currentState”) .equalToValue( “Estimation Confirmed”))) .as(“parts”); ); List<outputType> results = mongoTemplate.aggregate(aggregation, inputType, outputType)

How to consume Page response using Spring RestTemplate

new TypeReference<Page<StoryResponse>>() {} The problem with this statement is that Jackson cannot instantiate an abstract type. You should give Jackson the information on how to instantiate Page with a concrete type. But its concrete type, PageImpl, has no default constructor or any @JsonCreators for that matter, so you can not use the following code either: … 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

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

Spring Data: “delete by” is supported?

Deprecated answer (Spring Data JPA <=1.6.x): @Modifying annotation to the rescue. You will need to provide your custom SQL behaviour though. public interface UserRepository extends JpaRepository<User, Long> { @Modifying @Query(“delete from User u where u.firstName = ?1”) void deleteUsersByFirstName(String firstName); } Update: In modern versions of Spring Data JPA (>=1.7.x) query derivation for delete, remove … Read more

How to use OrderBy with findAll in Spring Data

public interface StudentDAO extends JpaRepository<StudentEntity, Integer> { public List<StudentEntity> findAllByOrderByIdAsc(); } The code above should work. I’m using something similar: public List<Pilot> findTop10ByOrderByLevelDesc(); It returns 10 rows with the highest level. IMPORTANT: Since I’ve been told that it’s easy to miss the key point of this answer, here’s a little clarification: findAllByOrderByIdAsc(); // don’t miss … Read more

tech