We are designing rest api for our existing application using spring+jpa.
want to validate the input/request payload prior to persisting. Have found #PrePersist listener method and hope we can validate entity business validations(unique etc) and id's can be resolved in prior to persist, however had few issues
EntityManager is not auto wired: trying to autowire entity manager in super Entity class like below so that entityManger object can be used in all subclasses
#PersistenceContext
protected EntityManager entityManager;
Understood that since the entity is not spring managed bean, entity manager object is not autowired.
After setting entiy object manually (as a workaround for point 1), while trying to resolve ids based on user provided values in PrePersist callback method resulted is getting same method called recursively.
any suggestion/way to implement business validation and resolving ids based on values from jason payload for rest api would greatly appreciate.
Thanks
Have implemented AOP aspect to validate the rest resources prior to persisting which is working pretty good as expected.
Related
I have a microservice created with JHipster(SpringBoot+JPA) exposing a rest api.
During a save operation over an Entity I need to manage the transaction because I must execute another update over the DB (using other Entities).
How can I do this?
Using the tradictional approach (JDBC) I got the connection and create a transaction over it, make all the queries and finally close the transaction(commit/rollback).
With JPA I have an Entity, but I find no way to specify to begin/end(manage) the transaction programmatically.
You have many alternatives, here are few ones:
define a service (a class annotated with #Service) and annotate with #Transactional the public method that implements your logic
manage transaction manually through the EntityManager injected into your service class constructor
create a custom Repository
Check the Spring docs
I was wondering, how a spring converter implementation gets an transactional scope.
Having a Converter which is used to convert a path placeholder (entity id) in an (rest) controller to the Entity itself. The Entity thus is loaded via Hibernate from the database.
For the details:
spring boot 2.3.2
A rest controller endpoint which triggers the converter to expand a path-placeholder (prior executing the rest-endpoint method body)
If in question, the controller method is not annotated #Transactional either
Question:
How (since we disabled OSIV) does this Converter get it's Transactional scope if the convert neither the method/nor the converter class is annotated using Transactional nor the convert method does custom transaction handling?
If you mean how Spring Data repositories get a transaction, the answer is all repository methods are transactional by default.
I'm using Spring boot along with Spring Data JPA.
I'm stucking in an odd scenario when saving a new entity.
Unsing method from extended CrudRepository class, all works as expected.
If I inject via #Autowired the CrudRepository interface in my service layer, the method still works, but nothing is persisted on db.
The returned object from 'save' method seems ok, since I get an always increasing ID value.
Suggestions?
Cheers
FB
check whether we are populating the data properly in your bean and check it before passing to save method of spring data jpa
I'm making an application with spring+primefaces
I created a init function in managed bean to load some useful data (the bean is #ViewScope)
#PostConstruct
public void init(){
log.debug("initing....");
currentUser = UserUtil.getCurrentUser(this.userManager);
loadData();
}
later in view I have some tags to hide columns like this:
<p:column rendered="#{dataManagedBean.currentUser.manager}"> // to test if the current user is a manager role
...
...
</p:column>
the problem is when I login with a normal user, then even logout and login with manager user, the column I want to show is not showing, because the init() method is not calling again therefore the "currentUser" remains the oldone (normal suer)
UPDATE
the problem maybe I mixed Spring DI and JSF managed beans, there is more detail
in javabean layer: I used #Entity annotation for hibernate
in dao layer: I use #Repository("DataDao") to inject the daos (from
spring)
in manager layer: I use use many spring annotations to inject like
#Service("DataManager") #Authowire #Qualifier etc
in view layer, since I have been using spring to inject, thats why I
use #Component("DataManagedBean") and #ViewScope
I have been 2 weeks with the application, and everything worked as expected. untill I found today #PostConstruct is actually never call again, the view is never destroy :S (or it's intented to work like this, a singleton...)
Can someone kindly explain me where I did wrong?
After an intensive reading about JSF scopes, specially thanks to #M. Deinum's comments and Mr #BalusC's excelent article explaining about the JSF communication topic. I understand why the user object survived event after logout/relogin.
My mistake is combining Spring DI (by using #Component annotation) and JSF annotation (by using #ViewScoped), then the #ViewScoped annotation is ignored and default spring scope is used, which is by default a singleton...
We Know the Spring Framework gives
singleton,
prototype,
request,
session,
global_session
bean Scopes.
Also we know that Spring web flow gives flowScope,viewScope,requestScope,flashScope,conversationScope.
So If i mentioned one Component, say Student, as #Component #Scope=singleton in a spring MVC project. For each request, will it create a new Student Object or Spring container will create only once?
You are confusing yourself with objects and beans.
For each request, will it create a new Student Object or Spring container will create only once?
The functioning of Spring is purely using beans. When you declare a something like a #Component, it's just an annotation that tells Spring that the part you've declared as a component is either Model or View or Controller i.e. a component of MVC. When you say something like #Scope=singleton, it tells Spring that only a single object instance can access the bean.
Let me make it more clear. Say you and I are objects and a strawberry candy is a bean. So if you have the candy. I cannot take it from you. Meaning only one of us can have that candy. It's the same thing with singleton scope.
Hope I made things simpler.. :)