What is the Bean Scope of Hibernate Persistence Context? Singleton or Request? and how to implement Unit of Work with Hibernate? - spring

I am coming from the .NetCore Background and learning Hibernate now for a new Project.
Previously I have worked with Entity Framework Core (EF Core) and Hibernate looks quite similar too.
I have some questions though:
In .NetCore, one could mark make EF Core DbContext either as a Transient (Prototype in Spring) or Scoped (Request in Spring).
What is the default Bean Scope for the Hibernate Session or Persistence Context? and how do I change it to Request Scope?

Related

Does spring-data by default always use the same persistence context within the same request?

An example is if my spring controller has two Autowired services, and both services have a PersistenceContext also controled by spring (and i'm doing nothing more), will both share the same context in every request by default?
No, they will use different ones. A persistence context (EntityManager) is defined to be a thread-bound concept in JPA. Thus each request will see a fresh instance of EntityManager for each new request.
For a singleton Spring component that gets an EntityManager injected, Spring will autowire you a proxy instance so that it can easily exchange the backing instance. This a core Spring container feature and doesn't need anything in Spring Data JPA (see the documentation here).

Spring vs hibernate object creation

I recently learnt that in hibernate, we need a no-arg constructor in an entity because hibernate instantiates its entities via reflection:
Hibernate implementation. Are we paying the reflection penalty?
I got curious that whether it is the same case with Spring and found that Spring beans do not require a no-argument constructor mandatorily.
This brings me to the question that how does spring creates its objects if not by reflection - to which I think that Spring is a container and instantiates beans and injects dependencies on startup and it must be able to load the application beans via some classloader and hence it does not need reflection.
Then, I get back to the starting point with the question that hibernate also has my application class definition available then why does it need reflection to create its entities?
can somebody please confirm or correct my understanding and provide me an answer?

Getting Spring object instantiation right

I'm new to Spring and a little confused about how it works. I get that I can use the application context to instantiate beans and have them populated. However, is the idea that I should be able to just write Bean b = new Bean() and then have Spring to somehow automagically populate that Bean?
I'm experimenting with Spring in a web application, and as far as I can see I need to inject the ApplicationContext into, say, the servlets to be able to instantiate other beans (services, daos etc.) from there. It's a bit cumbersome, but probably works.
However, is Spring meant to be able to hook into any object instantiation which happens on classes defined as beans in applicationContext.xml?
Spring is an Inversion of Control container. A bean is an object whose life cycle is managed by Spring. If you want Spring to populate an object, it needs to go through Spring, ie. it needs to be bean.
is Spring meant to be able to hook into any object instantiation
which happens on classes defined as beans in applicationContext.xml?
Spring doesn't hook into anything. You configure your beans and the relationships between them with Spring and Spring handles creating the instances and linking them up.
For domain objects, Spring provides a solution via the #Configurable annotation: http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#aop-atconfigurable
It requires compile- or load-time-weaving and, thus, introduces some additional complexity but having the convenience of using the standard new Bean() syntax plus Spring's autowiring is worth it in my opinion.
Alternatively, you could define your domain objects as beans with prototype scope and use some factory to create them using the Spring ApplicationContext.getBean() method. With a scope of prototype a new instance will be returned every time and since you go through the ApplicationContext, Spring will do all the dependency injection magic as usual.
As for services and other beans with singleton scope, you would typically NOT retrieve them by first injecting the ApplicationContext and using it but instead you would inject them via either a constructor, setter or annotation-based strategy. The documentation covers that in detail: http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#beans-factory-collaborators

Issue in understanding the Spring Bean Scopes Vs Spring web flow Session Scopes

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.. :)

Stateful beans and Stateless beans in Spring context

I am reading spring through its official documentation and at one place I came to a line that uses prototype scope for all stateful beans while singleton for stateless beans.
I know there is something as stateful as well as stateless beans in EJB but this is not what they have mentioned in the documents.
Can anyone explain to me what exactly this means of stateful as well stateless beans in Spring
Thanks in advance
From spring perspective
stateless beans: beans that are singleton and are initialized only once. The only state they have is a shared state. These beans are created while the ApplicationContext is being initialized. The SAME bean instance will be returned/injected during the lifetime of this ApplicationContext.
stateful beans: beans that can carry state (instance variables). These are created EVERY time an object is required (like using the "new" operator in java).
These are not EJB statfull/stateless session beans.
There is no point in storing specific information like client data, per request data in Singleton bean as they are created only once by Spring IOC container. That's why singleton beans are stateless. They are shared resources. They can only be used for storing global information.
When a request is made for creating a prototype bean, a new request is created every time. So, they can be used to store some specific information for each request. So they are stateful.
It totally depends on the implementation. See tomee for example http://tomee.apache.org/statelesscontainer-config.html . You'll have to check in your server docs

Resources