In hibernate, we have created one session.
Now I want my session to interact with two different databases.
How it can be done?
I explained interviewer that we can define two configuration files for different dialects but he was not convinced.
You can have two separate configuration files, and create two different SessionFactories with them, one for each.
This will allow you to create a session for each database. However, it is impossible to have one session access both databases.
Therefore, what you need to do is to create two sessions, and work with each individually. You may of course copy objects / fields from entities obtained from one database into new entities which are then persisted in the other database.
Related
The company I am working for stores their client data in a separate database schema for each client. They indicate that this cannot be changed at this time. Is there an efficient way to pull data and update data in all schemas without configuring a connection for each schema? Everything I can find when I search seems to be talking about using one or a couple of schemas, but I need to use many (100+) simultaneously.
In any given persistence context, each JPA entity class is mapped to a specific base table. Whether and how easily you can access multiple schemas via a single DB connection is a function of your DBMS, your JDBC driver, and perhaps your particular database, but even a combination that in general supports the kind of access you would need will still not allow you to map the same entity class to multiple distinct base tables in the same persistence context.
You might be able to use the same entity classes for different clients by associating a different persistence context with each client, but that will not allow you use the same DB connection for all of them. Thus, if using the same connection were possible for you at all, it would require different entity classes per client.
Have you considered creating a new DB user and creating SYNONYMS for each of the tables in the separate database schemas ?
You could then map JPA entitys to the SYNONYM names that you have created..
Using this approach you could still use the one DB connection but with SYNONYMS to the DB tables in the other schemas...
I'm designing an application that will use data from several databases. I'll use JPA to implement persistence and Spring as main application framework.
I need to handle transactions that will span among different datasources. Googlin' a bit I found that JtaTransactionManager could be useful to implement this.
But I'd also like to create relations (at application level) among entities belonging to different datasources. So that I can work as if the data-layer consists of a single database, without having to worry about the source which the entities "come from".
Will JtaTransactionManager let me do this, or I need some extra component or configuration in Spring?
I know no easy existent solution to your problem.
The JtaTransactionManager will take care of executing operations with different databases/datasources within the same transaction, but you won't have the data consistency guaranteed by any databases (like foreign keys between databases).
Besides, JPA does not support #ManyTo* relationships between different databases (like EntityFromDb1 has a OneToOne relationship tp EntityFromDb2), so the solution would be to work with the corresponding Ids. Afterwards it is your task to manage those relationships. Of course with some work from your behalf you could automatize the load of relationships, but there is much more that only that, like cascading, locking, queries joining both databases...
I am in the process of designing a new java application which has very strict requirements for auditing. A brief context is here:
I have a complex entity with multiple one to many nested relationships. If any of the field changes, I need to consider it as a new version of the object and all this need to be audited as well. Now I have two options:
1.) Do not do any update operation, just insert a new entity whenever anything changes. This would require me to create all the relational objects (even if they have not been changed) as I do not want to hold references to any previous version objects. My data tables becomes my auditing table as well.
OR
2.) Always do an update operation and maintain the auditing information in separate tables. That would add some more complexity in terms of implementation.
I would like to know if there is a good vs bad practice for any of these two approaches.
Thanks,
-csn
What should define your choice is your insert/update/read patterns for both the "live" data and the audits.
Most commonly these pattern are very different for both kinds.
- Conserning "live" it depends a lot on your application but I can imagine you have significants inserts; significatant updates; lot of reads. Live data also require transactionality and have lot relationship between tables for which you need to keep consistency. They might require fast and complex search. Indexes on many columns
- Audits have lot of inserts; almost no update; few reads. Read, search don't requires complex search (e.g. you only consult audits and sort them by date) and indexes on many columns.
So with increased load and data size you will probably need to split the data and optimize tables for your use cases.
Is it possible to create relationships (#OneToMany , #ManyToMany) between two entities in two different persistence units?
After following this set up http://viralpatel.net/blogs/spring-roo-two-database-configuration/
i want to see if an entity created with one persistent unit context can interact with another entity on another context.
If this isn't possible, do i just have to manage transactions on my own?
You cant have relationships across two different contexts/databases.
Some databases allow you to setup db-links. These create a virtual schema/db in the database which looks likes in the same db even though it is not. However, I have found these types of solutions to be slow and problematic.
Also, when dealing with 2 different db's, transaction management becomes more complicated. You can use the full 2 phase commit transaction management. Or you can use a custom transaction manager that works specifically on your 2 database instances.
I'm looking for feedback on the Data Access Object design pattern and using it when you have to access data across multiple tables. It seems like that pattern, which has a DAO for each table along with a Data Transfer Object (DTO) that represents a single row, isn't too useful for when dealing with data from multiple tables. I was thinking about creating a composite DAO and corresponding DTO that would return the result of, let's say performing a join on two tables. This way I can use SQL to grab all the data instead of first grabbing data from one using one DAO and than the second table using the second DAO, and than composing them together in Java.
Is there a better solution? And no, I'm not able to move to Hibernate or another ORM tool at the moment. Just straight JDBC for this project.
I would agree with your approach. My DAOs tend to be aligned more at the object level, rather than from a DB Table perspective. I may manage more than one object through a DAO, but they will very likely be closely related. There is no reason not to have SQL accessing two tables living in one DAO.
And for the record, I have banished the acronym DTO from my vocabulary and code.
Ideally, how you store your data in a database, and then how you access them, should be derived from the nature of the relationship among the domain entities in your domain model. That is, Relational Model should follow from Domain Model. For example, if you have two entities, say, User and Address.
Scenario #1: Address are never accessed independently, they are always an attribute of User.
In this case, Address is a Value Object and User is an Entity, and there are guides on how to store this relationship. One way is to store Address attributes of Address alongside of attributes of User, in a single table. In this case, UserDao will handle both objects.
Scenario #2: Address can be associated to a User, but also can be separate on its own, an entity.
In this case, an approach different from the first one is needed. You may have a separate DAO and table for the Address type.
My point is, that more often this important idea is ignored that Domain Model should be the core of the application, driving other layers.
For instance, if your domain model is properly define and you are well aware of the type of entities you have and the relationship among them, then your persistence (relational tables and their relationships, your DAOs, etc) will evolve as a very logical consequence of what you have in the domain model.
In other words, if you spend some time studying your model, you will be able to trace your problem in determining how to organize your DAOs to a place in the domain model. If you can clearly define the type of the objects and the nature of relationship among them in the domain model, it will, help you resolve your problem in DAL layer.