Spring data commons 2.7.3 doesn't update when using save() - spring-boot

I have recently upgraded my project to java 17 and spring boot 2.7.4 and spring cloud 2021.0.4. I am using mavenBom for spring cloud dependency management. It has brought in spring-data-commons-2.7.3.
With the upgrade, the CRUD repository's save() doesn't seem to be updating the existing objects. I am getting a duplicate key exception. (org.springframework.dao.DuplicateKeyException: Document with the given id already exists).
I have already tried enabling transaction management, but to no avail.
Could you please help.

auto-upsert is not supported by spring-data-couchbase v 4.4.3.
If you want to use upsert() on an entiy class that has an #Version property, you'll need to explicitly use template.upsertById() or introduce your own repository.upsert() method. Or override the repository.save() method.

Related

Hibernate-Search 6 and Elasticsearch integration

I am working ina Hibernate Search application and I am trying to use version 6 (which right now is in beta). I followed all the instructions and documentation in https://docs.jboss.org/hibernate/search/6.0/reference/en-US/html_single/#getting-started-configuration
After trying to start my application I get next error:
I have looked in Google and seems to be I am the first person in the world who is facing this issue.
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.hibernate.search.mapper.orm.mapping.impl.HibernateOrmIndexedTypeContext.<init>(HibernateOrmIndexedTypeContext.java:35)
The following method did not exist:
org.hibernate.metamodel.spi.MetamodelImplementor.entity(Ljava/lang/Class;)Lorg/hibernate/metamodel/model/domain/spi/EntityTypeDescriptor;
It was loaded from the following location:
file:/home/myUser/.m2/repository/org/hibernate/hibernate-core/5.3.11.Final/hibernate-core-5.3.11.Final.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.hibernate.metamodel.spi.MetamodelImplementor
Any idea of how to solve it?, In my application I am working with Spring boot and jpa.
As indicated here in the documentation, Hibernate Search 6.0.0.Beta2 requires Hibernate ORM 5.4.7.Final or later. You are using Hibernate ORM 5.3.11.Final.
I think Spring Boot 2.1 ships with ORM 5.3, so that's probably what you're using.
The best course of action would be to upgrade to Spring Boot 2.2.1.RELEASE, which ships with Hibernate ORM 5.4.
If that's not possible, you can still stay with Spring Boot 2.1 and override the version of Hibernate ORM directly in your POM: from what I've seen, it works rather well in Spring Boot 2.1.
Just set this property in your POM:
<properties>
<!-- Override the version of Hibernate ORM in Spring Boot -->
<hibernate.version>5.4.7.Final</hibernate.version>
</properties>
Note that technically, any version after Hibernate ORM 5.4.4.Final should work, but sticking to the version Hibernate Search was tested with is usually safer.

How to edit Hibernate settings in a Spring-Boot project?

Essentially what I'm trying to do is to add this property change to hibernate so I can enable instantiation of composite/embeddable objects when all of its attribute values are null:
hibernate.create_empty_composites.enabled
I am aware that the usual way to edit Hibernate is in the application.properties file like so:
################################################################################
# JPA MANAGEMENT #
################################################################################
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true...
...
spring.jpa.properties.hibernate.create_empty_composites.enabled=true
But the spring.jpa.properties.hibernate.create_empty_composites.enabled=true isn't working. I'm not sure if Spring just doesn't recognize certain properties or if it's just the wrong place to put it.
What I'd like to know is if there is another way to edit the Hibernate properties directly or if there is another fix.
Analysis
The base assumption. More likely you are using Spring Boot 1.5.*.
Spring Boot 1.5.* uses Hibernate 5.0.*. GitHub proof.
Hibernate supports the hibernate.create_empty_composites.enabled setting since the 5.1 version.
GitHub proof.
JIRA proof (?): [HHH-7610] Option for injecting empty (non-null) embedded when all columns are NULL - Hibernate JIRA.
Release notes proof: ORM 5.1 feature release.
Solution
Please consider upgrading the Hibernate dependency in your pom.xml to a more recent version (5.1 and higher).
After that, it should work just fine:
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.
— Spring Boot 1.5.* reference, 77. Data Access, 77.5 Configure JPA properties.

JpaRepository and CrudRepository error

Why happen such error?
I am using Spring Data JPA 1.3.3 , Spring 4.2.8 and Spring MVC 4.2.8.
UPDATE:
Error as code block
Error:Error:line (8)java: name clash: save(java.lang.Iterable) in org.springframework.data.jpa.repository.JpaRepository and save(java.lang.Iterable) in org.springframework.data.repository.CrudRepository have the same erasure, yet neither overrides the other
All my libraries added as IntelliJ Idea's Module. Only JSTL is downloaded by Maven.
It seems you have dependency problem. An older version of Spring Data Common with Spring Data JPA might be the problem.
For spring data jpa, It's not really good idea to write native query in repo. Unless your demands are just to hard to write query methods.
For your case, you just need simple query methods like this
List<User> findByFirstName(String firstName);
Return list of User because many Users might have the same userName
Take a look at spring data doc Query methods
Hope it help :)
For those who face the Ignite Spring Data and Spring JPA dependency issue - you need to downgrade org.springframework.data:spring-data-jpa version to 1.11.23.RELEASE. Thanks to #chirdeep-tomar comment!
In my project compatibility issues of IgniteRepository and CrudRepository with deleteAll() method were solved with the following Maven configuration:
'org.springframework:spring-core:5.2.3.RELEASE'
'org.springframework.data:spring-data-jpa:1.11.23.RELEASE'
'org.apache.ignite:ignite-spring-data:2.9.0'
'org.apache.ignite:ignite-core:2.9.0'
'org.apache.ignite:ignite-spring:2.9.0'

Spring-data-rest 2.5.6 -> Setter no longer called

When I upgrade from Spring Boot 1.4.1 to 1.4.3 Spring-data-rest is upgraded from 2.5.3 to 2.5.6.
After that a #OneToMany property in a SDR-managed Resource is no longer set via its setter (I assume now via direct field access).
What has to be done that the setter is called again? I already tried #JsonProperty/#JsonIgnore - with no luck.
Try to use a property access in entities.
Look example #2 in my repo: https://github.com/Cepr0/restdemo

EHCache with Hibernate Integration Throwing CacheException

I am facing issue in integrating Hibernate And EHCache. I am using Hibernate 3.2.7 GA, Hibernate Annotation 3.2.1 ga,hibernate-commons-annotations 3.2.0.Final, net.sf.ehcache 2.2.0 and Spring version 3.2.4.RELEASE. When I ran my test case I am getting
net.sf.ehcache.CacheException: Error configuring from /content/cache/ehcache.xml. Initial cause was Error configuring from input stream. Initial cause was null:19: Element does not allow attribute "maxEntriesLocalHeap".
In Hibernate config file i have put
true
Please help me to solve the issue. I am new to EHCache.
Fairly confident the 2.2 version of Ehcache doesn't support that maxEntriesLocalHeap. Any reason you're using such an old version?
You either need to use a newer one, or modify your ehcache.xml to comply to the 2.2 XSD

Resources