Could not commit JPA transaction: Transaction marked as rollbackOnly

My guess is that ServiceUser.method() is itself transactional. It shouldn’t be. Here’s the reason why. Here’s what happens when a call is made to your ServiceUser.method() method: the transactional interceptor intercepts the method call, and starts a transaction, because no transaction is already active the method is called the method calls MyService.doSth() the transactional interceptor … Read more

Does Hibernate Fully Support SQLite?

There are several SQLite dialects out there. Hibernate 3: https://github.com/kemitix/sqlite-dialect <dependency> <groupId>net.kemitix</groupId> <artifactId>sqlite-dialect</artifactId> <version>0.1.0</version> </dependency> Hibernate configuration: hibernate.dialect = org.hibernate.dialect.SQLiteDialect Hibernate 4: https://github.com/EnigmaBridge/hibernate4-sqlite-dialect <dependency> <groupId>com.enigmabridge</groupId> <artifactId>hibernate4-sqlite-dialect</artifactId> <version>0.1.2</version> </dependency> Hibernate configuration: hibernate.dialect = com.enigmabridge.hibernate.dialect.SQLiteDialect Note: I am author of this repository. Based on the gwenn repo. Hibernate 5: https://github.com/gwenn/sqlite-dialect/ Author worked with Hibernate team to integrate … Read more

Deleted object would be re-saved by cascade (remove deleted object from associations)

The solution is to do exactly what the exception message tells you: Caused by: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations) Remove the deleted object from an associations (sets, lists, or maps) that it is in. In particular, i suspect, from PlayList.PlaylistadMaps. It’s not enough to just delete the … Read more

Spring Boot + JPA2 + Hibernate – enable second level cache

To sum everything (L2 cache and query cache) up: The first thing to do is to add cache provider (I recommend using EhCache) to your classpath. Hibernate < 5.3 Add the hibernate-ehcache dependency. This library contains EhCache 2 which is now discontinued. <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>your_hibernate_version</version> </dependency> Hibernate >=5.3 In newer versions of Hibernate caches … Read more

Strange Jackson exception being thrown when serializing Hibernate object

I had a similar problem with lazy loading via the hibernate proxy object. Got around it by annotating the class having lazyloaded private properties with: @JsonIgnoreProperties({“hibernateLazyInitializer”, “handler”}) I assume you can add the properties on your proxy object that breaks the JSON serialization to that annotation. Avoid Jackson serialization on non fetched lazy objects

Refresh and fetch an entity after save (JPA/Spring Data/Hibernate)

Instead of defining EntityManager in each of your resource, you can define it once by creating a Custom JpaRepository. Reference Then use the refresh of your EntityManager in each of your repository directly. Refer the below example: CustomRepository Interface import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.NoRepositoryBean; import java.io.Serializable; @NoRepositoryBean public interface CustomRepository<T, ID extends Serializable> extends JpaRepository<T, ID> … Read more

JPA Multiple Embedded fields

The generic JPA way to do it is with @AttributeOverride. This should work in both EclipseLink and Hibernate. @Entity public class Person { @AttributeOverrides({ @AttributeOverride(name=”street”,column=@Column(name=”homeStreet”)), … }) @Embedded public Address home; @AttributeOverrides({ @AttributeOverride(name=”street”,column=@Column(name=”workStreet”)), … }) @Embedded public Address work; } @Embeddable public class Address { @Basic public String street; … } }

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