I have 2 entities that needed to be persisted in two different DB's:
1) MyClassMetaData - persisted on mysql via jpa+hibernate in spring (entityManager)
2) MyClassRawData - persisted on mongoDB via spring data (mongoTemplate)
There is a one To one Relation between the two entities:
There is no meaning for only one entitiesto be persisted without the other.
there will always be a metadata AND rawdata for each save.
My service for saving these 2 entities looks like this
#Transactional
public void saveMyClass(metadata, rawdata){
// Do Something here
this.entityManager.persist(metadata);
this.mongoTemplate.save(rawData);
}
My question is: how do i make sure that if an error occurs on this save method - a rollback will take place and for both classes?
thanks
This might be what you want to achieve: http://static.springsource.org/spring-data/mongodb/docs/current/reference/html/mongo.cross.store.html
However it ties the two entities strictly together.
Related
I want to log exceptions to my database to ensure failures are recorded. I am using entity framework.
Should I setup an encapsulated logging service that records to a table which is not managed by entity framework or should I just make an ef class called Log?
Im thinking that a log is not really an entity that represents my application parts, but represents meta data which is why I ask.
Consider a separate (bounded) context for your general logging. If logs happen to reference top-level entities you can define minimal entity definitions for these as well. Logging operations are heavy-write, so by keeping a separate DbContext you minimize the spin-up time.
When it comes to auditing (I.e. persisting change tracking) then I commonly use a pattern that hooks directly into the DbContext events and records information based on when entities are updated, inserted, or deleted.
Here's a scenario I need to execute to throw the optimistic lock exception in case if something has changed underneath between the time I read from the DB and by the time I actually writes it back. I have a project that defines it's own persistence entities and unit. The EntityManager is defined with the extended persistence context. It define a bunch of persistent entities.
Now I have a dependency on some jar file and now this jar file also defines some persistence entities and it's own persistence unit(in persistence.xml file) and entityManager. The entityManager here also is defined with extended persistence context.
Both the persistence unit are pointing to the same database and same schema/library(DB2). THere are some persistent entities that are common between both the persistence units i.e. they refer to the same underlying table but since they are coming from two different jars they have a different name/package structure and a different set of keys defined as a composite primary key.
The scenario is and I need to read an entity from one of the entityManager and then write/update using the different entity manager. Since I am running this in a single transaction the persistence context propagates but since these are 2 different persistence units and each defines a different name/package structure for the same underlying table, even I read from one of them and try to save from the other, the second entity manager saves this as a new entity as if it doesn't exist in the table. So even if the data has changed for the same row in the DB between the time I read it from the first entity Manager and the time I try to write using different entity manager it doesn't throw the optimistic lock exception, it just saves it as it is.
I tried my best to explain the scenario but please don't hesitate to ask in case of query. Also, I believe if you have 2 different PU that defines persistent entity with different name for the same underlying table, you just cannot read from one em and have the other em know that since the underlying table is same, and since this row has been read by different em so let convert this entity the way that second PU defines it and when tries, the second em tries to compare the data in the table first if something has changed since the very first time the first em read from the same table and if something has changed then to throw the optimistic lock exception.
I'm using Spring Data JPA and DataNucleus as JPA persistence provider and have something like
interface BookRepository extends CrudRepository<Book, Long> {
Book findByAuthorId(Long id);
}
If I call bookRepository.findByAuthorId() and then access book.publishingHouse.manager.name is null. As opposed to calling bookRepository.findAll() when the fields are populated correctly all the way. I set datanucleus.DetachAllOnCommit=true and datanucleus.maxFetchDepth=-1 (I also tried with 10).
Any idea why?
If you don't have any additional transaction boundaries defined, the EntityManager closed when leaving the query method. That means what you get back is detached entities and what kind of load state you get back is determined by the defaults the persistence provider uses.
You basically have two options:
Have a client (service or controller class) using #Transactional to keep the EntityManager open and thus the loaded instances eligible to lazy-loading to pull data out of the store while you use the instance. If a controller or service is not enough, you might wanna look into the OpenEntityManagerInViewFilter/-Interceptor which basically keeps the EntityManager open until the view is rendered.
Define what should be fetched explicitly either using JPA 2.1 entity graphs (see the reference docs for details) or explicitly adding fetch-joins to the query by defining it manually.
I have a web application using J2EE + Spring and a MySQL database. I need one entity which will be read-only. I have one main table with products, and they are only to read. There should be no insertion of new records and no updates currently.
The entity class should only read data and pass the entities forward (other entities are tables like order, shipments etc.).
Is there any solution for this? Does anyone have the same issue? Thanks for the help.
If you don't change an object, it will never be updated.
If you are using EclipseLink you can use the #ReadOnly annotation to mark something as read-only.
Let's say i have two tables in db: Car and Part. Car owns arbitrialy number of parts.
Because i use EJB 3.0 i have entity beans for Car and Part and Car entity bean contains list of Part objects.
I want to save new Part to db and right after that fetch from db all Cars. I exepect Part i've just added to be amongst parts associated with fetched Cars. But it is not.
I do persist and find in single transaction so that's the problem i guess. How can i save something to db and right after calling persist on entityManager fetch it from db?
Try to use EntityManager's flush method between persisting Part and fetching Car entities.