Error creating bean with name 'customerRepository': Invocation of init method failed; No property updateCustomer found for type Customer - jpa

CRUD operations using Spring Boot + JPA + Hibernate + PostgreSQL
Please am new in this topic. Am trying to create a CRUD Api. Everything is ok but i cant get the UPDATE method using the JpaRepository. I dont have error when i build my project.
I try to code a specific createCustomer method into my customerRepository but still not working
enter image description here
enter image description here
But when i run it with maven i get this error:

Spring data Repository work mostly with method names. It creates query based on the method name. For example in your Repository interface you have a method findByUserId. This will work even if you have not written any query. How ?
spring-data will formulate the query to fetch the field specified after findBy (UserId in your case). But at compile time, it checks whether you are providing a valid field So that it can prepare a proper query. It does so by checking your entity class (Customer in your case) for this field.
Note : keep in mind that field in Entity is called userId but method name in Repository is called UserId.
The reason save, delete methods are working because you are extending JPARepository class(which in-turn extends other classes), which has these methods. So it works out of the box.
But when you write some method which is not there in any of the super class, then spring-data will try to formulate the query on its own as I specified earlier. So when you write a method like updateCustomer which is not in any of the super classes, then it tries to find in the entity class a field with name updateCustomer and it fails since it cannot find and hence the exception.
So if you want to declare or do something like this, you can do this by annotating such method with #Query(....) and providing the right query inside it.
#Query(update customer set ..........)
Customer updateCustomer(Customer customer)
Hope its clear.

Related

deleteById vs delete in spring jpa

I have a question about methods deleteById and delete in spring-data.
what is the difference between these methods? When should I use delete/deleteById?
I search google for one day but i have no answer for it
The method deleteById will throw an EmptyResultDataAccessException if the supplied id does not exist, whereas the method delete will silently return if the supplied entity hasn't been persisted yet, or for whatever reason it cannot be found by the EntityManager.
Also, as #manish noted in their comment, the method deleteById actually calls the delete method internally if the findById method is able to find an entity.
As of Spring-Boot 3.x and it's respective version of spring-data-jpa,
the method deleteById will no longer throw the Runtime exception of EmptyResultDataAccessException in case the provided id did not existed in database to be deleted.
So the only difference from now on between deleteById(Id id) and delete(T entity) is that the first one will take as parameter the id and try to locate the respecting entity in database with that id to be deleted, while the second one will take as parameter directly the entity to be deleted.

Spring Data JPA 2.0.0: How to best resolve conflict between custom findById method and new version in CrudRepository?

Release 2.0.0 of Spring Data JPA replaced the findOne and exists methods with findById and existsById in CrudRepository.
We have the unfortunate situation where our entities PK attribute are called "oid", and often there is an additional natural id field named "id". To make matters worse both are Strings.
This means that our existing findById and existsById Repository methods conflict or inadvertently override those in CrudRepository.
The findById we can rename to queryById, and the existsById we can rename to something like existsLocalById, but both are workarounds and I suspect bugs will be introduced by people inadvertently using the wrong method.
Are there any other options available which would result in a cleaner design?
So a little digging into the JIRA behind the change reveals the following:
it should work if you annotate the method with #Query as that
indicates you want this thing to be a query method explicitly
Initial testing appears to show that this works as expected, e.g. In MyEntityRepository..
#Query
Optional<MyEntity> findById(String id);
#Query
boolean existsById(String id);
We'll still have to take care that the correct method is used as it could lead to some hard-to-determine bugs I suspect.

Javers and MyBatis integration help needed

I'm having issues trying to get MyBatis and Javers (with Spring) integrated and working. I've followed instructions at http://javers.org/documentation/spring-integration/ and gotten the Aspect setup, and annotated my entity class and registered it with Javers, and the MyBatis interface correctly annotated with #Repository and #JaversAuditable on the appropriate methods, but still haven't gotten it to work, even setting breakpoints in the Javers Aspect, but nothing triggers.
I've also gone about it the other way, using MyBatis plugin interceptor, as per http://www.mybatis.org/mybatis-3/configuration.html#plugins (then used http://www.mybatis.org/spring/xref-test/org/mybatis/spring/ExecutorInterceptor.html as a basic example for commits). However while it's triggering, it's not doing what I expected and is basically just an aspect around on the commit method, which takes a boolean rather than containing which entity(ies) are being commited which would let me pass them to Javers. I suppose I could add an interceptor on the update/insert MyBatis methods, and then stored that in a ThreadLocal or similar so that when commit/rollback was called I could pass it to Javers as necessary, but that's messy.
I've got no clue where to go from here, unless someone can see something I've missed with one of those 2 methods.
So in my confusion, I realized that since MyBatis generates the concrete object for the Mapper Interfaces, Spring never seems the creation of that object, simply has the final object registered as a Bean in the context. Thus, Javers never has a chance to process the Bean as it's created in order to do any proxying or what not as necessary.
So, silly me. So I ended up creating a Spring-Data #Repository layer that mostly just passes the call through to the Mapper. Although on updates I'm doing some extra bits which the DAO shim layer (as I'm calling it) works well for.

Extending Breeze Entities that don't map to database

I am trying to return and extend a type that is defined in my server data model, but is not contained in the EF DataContext. Using breeze, I am able to return it from the BreezeController, but I cannot call registerEntityTypeCtor with the entity type, because the EntityType is not contained within the metadata.
I would like to be able to extend the model (currently just showing in console as type 'Object') with computed properties for display in an Angular app... Think 'fullName'.
Is there a way to do this within Breeze?
You can create and add an EntityType directly to the MetadataStore via the MetadataStore.addEntityType method. See the NoDb sample in the Breeze zip for an example of this.
Once you have the EntityType you can then call 'registerEntityTypeCtor'. If you have 'named' the EntityType consistent with the server side type that will be serialized to the client then Breeze will automatically recognize it. If not then you can either use the EntityQuery.toType method to force a query result to be recognized as being of your new type or more generally you can create a JsonResultsAdapter to do the same thing.

JPA named queries

I have a java class that implements serializable and the class begins with :
#NamedQueries( {
#NamedQuery(....)
#NamedQuery(....)
...
..})
My question is at what stage these queries are going to be executed because i see no direct call to these queries by their name
Project is using JPA. I think IBM implementation of JPA...surely not hibernate.
Thank you
Each named query has a name and the query is executed by invoking EntityManager.createnamedQuery().
If the name of the query is based on a constant, you can search the usages of the constant in your project or if it's just a string, you can a text search in your project.
If you don't find any usages, there's a chance that those queries are not used at all, unless there's another framework that is invoking them (by doing something like convention over configuration).