Do Jpa methods create their own transactions? - spring-data-jpa

I was wondering whether jpa methods execute within transactions or without them.
If i introduce #Transactioanl annotation, i understand that all the database queries within the scope of the annotation will execute within a single transaction.
Does that mean a #Transactional annotation overrides jpa methods' transaction settings, or is it that jpa methods, by themselves do not create a transaction?

Found the answer - Yes , JPA Methods save and saveAll do create their own transactions:
https://www.baeldung.com/spring-data-save-saveall

Related

EclipseLink Queries

Is there a way to create the equivalent behavior provided by EclipseLink’s AdditionalCriteria annotation in JPA? That is, can we create additional filtering that is added to every query for a particular entity type? We can use EclipseLink, but we don’t want to be dependent on it.

notifying entity call back methods after bulk query in jpa

How does jpa entity call back method work?
are they only notified when doing single jpa operations like em.merge() and em.create() ? or they will be notified if i use em.createQuery().executeUpdate()? (also for namedQueries)
Can i use them instead of real database triggers?
I did not find a direct statement, but from the quote below (JPA 2.1 spec,
Chapter 4.10 Bulk Update and Delete Operations), I understand that in a BULK update or delete, the listeners are not called, because the persistence context won't see the changed entities:
[In a bulk update or bulk delete] The persistence context is not synchronized with the result of the
bulk update or delete.
So the answer is: only in single JPA operations will the listeners be changed.
A listener is not the same as a database trigger: a constraint of JPA listeners is that you should not change relationships or other entities inside them (although some JPA providers may support this).

Spring Data JPA Repository CRUD Testing

I'm playing around with the Spring Data Repository and have a question on writing CRUD tests. I have written many CRUD tests against Hibernate DAOs and EJB 3 entity beans where I create and entity, flush it to the database, clear the entity manager, and read it back by ID. The entity manager is cleared so the first level cache is not hit on the read.
Using the Spring Data repository I can't find a way to clear the underlying entity manager used by my test so my read is not going back to the actual database, making my test invalid.
Is there any way to clear the entity manager in the test? Is there a way I can inject one into my test so that it is used by the repository?
Thanks!
Cory.
Try it by injecting the entitymanager like this:
#PersistenceContext
EntityManager entityManager
and make your test transactional by setting the #Transactional attribute on your test method. Then inside the method you can call the entityManager.flush() method.
Regards
If you wish the EntityManager to be cleared automatically you can set #Modifying annotation’s clearAutomatically attribute to true.
Please see here

How to get a detached object from JPA

In my application I need most objects fetched in detached mode (fetched with the find API).
I'm wondering if there is a way to ask a detached object from the JPA provider and save the extra call to detach() API.
In additional I would expect the object created in such mode to be less expensive since the JPA provider doesn't need to add it to the entity manager context.
Is there a way to achieve this with JPA APIs?
Is there a way to achieve such functionality with query results?
Specifically I'm using Eclipse Link so if there is a specific way to do it with this implementation it will be helpful as well.
You can fetch a detached entity without an extra call to detach() if you fetch it outside a transaction. If you are not using container-managed transactions, it's trivial, simply do not start a transaction.
If you are using CMT, you have to make sure the requesting object is not a transaction-enabled EJB:
if in an EJB, suspend the transaction by annotating the appropriate method with:#TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED),
or
call the EntityManager from a POJO. You dont have to call it directly, it only impotrant that the query result will end in a non-EJB object.
AFAIK, there is no performance gain to be expected, since the query result will always be put in the current persistence context, however shortlived it may be.
EDIT: There is another possibility to get detached objects which does not depend on transaction demarcations: JPA constructor expressions:
List<DTO> dtos = em.createQuery("SELECT NEW com.example.DTO( o.title, o.version) FROM Entity o").getResultList();
The constructed type must have a constructor with all the relevant attributes. The objects in the list, entities or not, will always be created detached. However there is a small overhead of instantiating a new object.

How does Entity Framework convert an EntityTransaction into a provider specific transaction?

I've got an Entity Framework 4 data mode. I'm loading data into a database using stored procedures since EF is so slow. We use the entity model to call the stored procedures. Everything has to be in one transaction.
In order to speed up the process, I need to perform some bulk copy operations. I'm using SQL Anywhere and their ADO.NET provider software for this.
When I call context.Connection.BeginTransactin(), I get an EntityTransaction back. Actually, my variable is a DbTransaction, which is the base class for all transactions. But the actual object returned is an EntityTransaction.
I can't cast an EntityTransaction into the provider specific transaction class (SAConnection in this case). If I do, I get a cast exception. Yet, somehow, when the entity context calls the stored procedures, it's enlisting the provider specific command objects it creates into the transaction represented by that EntityTransaction object.
How does the provider do this? Is it a mechanism I can use to get a provider-specific transaction object for my bulk copy operations?
Tony
I have taken a look at the EntityTransaction class and in fact an internal property called StoreTransaction exists. If found some source-code in this SO discussion: This SqlTransaction has completed; it is no longer usable. Entity Framework Code First which might help you out.
Keep in mind, that this won't work in partial trust environments, which do not allow you to access members using reflection.