Use two entity managers for one entity - java

The situation is the following: I have two databases Db1 and Db2, for which I have two EntityManagers em1 and em2 defined. Furthermore, I have the entity Person(int id, String name, Pet pet) mapped to the table persons(id, name) in the database Db1 and the entity Pet(int id, String name, Person owner) mapped to the table pets(id, name, person_id) in the database db2. The relation between Person and Pet is a #OneToOne realtion. At some point in the program, I would like to do something like this:
PersonDAO personDAO = db1DAOFactory.getPersonDAO();
Person person = personDAO.find(100);
System.out.println(person.getPet().getName());
There is no possibility to merge both databases. How can I tell Hibernate to use for the pet field the EntityManager em2? With Hibernate I use only annotations, no xml configuration.
Thank you very much!

Sorry but I think you can't. The question is answered by nakosspy:
Hibernate will create an SQL query for your HQL or Criteria query and this SQL will be sent through jdbc to the database. This means that hibernate does not support for what you are trying to do.
However, you can achieve the same result in some cases. Some databases give you the option to create an alias for a table that resides in a different database. So you will be able to write an SQL query that joins the two tables and execute it on the database.
We are doing that with DB2. If you can do that, it depends on your database.
I guess, that it would impossible if you have two different databases (example DB2 and MySQL) but if both databases are of the same vendor, then maybe it's achievable.
You should try to find more info in you database server's documentation.
If you want to see his original post check this link.

This is dependent on database features. If you are using Oracle database, You could achieve this by creating db link in database then map these entity through creating a view or a synonym in oracle database. Check your database features. Hope this helps.

Related

How use Hibernate Envers with Teiid

I have a problem with the integration of hibernate envers on a application that use Teiid. This one doesn't support ddl like 'create table TABLE_NAME_AUD' or sequence generation.
I tried to split entity manager and tranction manager and delegate create table to another entity manager, but the transations on entities created by virtual table doesn't work. I also tried to create tables on DB, but the insert statement generated by hibernate dosn't work because the name of column of reventity is a keyword of teiid syntax and I can't change the name of column.
So, there's a way to implements hibernate envers with Teiid propertly?
Thanks!

Query generated by Hibernate

I have my service up and running but there's still a few things i can't figure out.
I have a query that is similar to the following
#Query("SELECT t FROM Tablename t")
Then hibernate will generate the following query
Hibernate: select tname.column1 as a, tname.column2 as b, tname.column3 as c, tname.column4 as d from tablename tname
The problem is that Tablename is case sensitive when i query against a mysql database. Is there a way in hibernate to execute the query exactly the way it is spelled out in the annotation? Also, is it possible to stop hibernate from breaking camelcased columns into two works. For example if i had a column named columnOne hibernate will want to generate a column with the name column_one.
I know this more than likely has something to do with hibernate's naming strategy but i haven't been able to find a solution.
Try adding the following in your application.properties file.
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Documentation about naming strategies are here

Custom Validation Using Hibernate Validator

We have an Application that uses a NoSQL DB as its data source for whatever reason.
We need to persist say E.g an Employee Object that has a Department id and an Address with a zip code. If I persist it in an RDBMS with an invalid department id and zipcode. the DB will throw a constraint exception.
But if I use a NOSQL DB I cannot do this Validation. Is there is any way I can use Hibernate validator and check in DB whether a zip code or department id exist in the DB. Before saving it in DB
Here's an example on the JBoss developer forums that describes how to access the Hibernate Session in a custom validator. It's a bit dated, so you may have to make some tweaks.

How to select schema at runtime using Hibernate?

I already have an existing code base, where schema(like db_1, db_2..) are created at run time.
We are currently using JdbcTemplate, using that its quite easy to append schema in the native SQL queries some thing like :-
sql = " Select * from "+schema+".user";
jdbcTemplate.query(sql, new UserMapper());
Now I want to know is how to provide schema to hibernate at runtime like I did with the jdbcTemplate?
What connection url should I provide in hibernate.cfg.xml so that it doesn't connects to a single schema rather whole database?
Any suggestions will be helpfull.
P.S: I am new to hibernate (So I might have missed something stupid)
I know of two options:
Use native SQL query binding results to JPA entities. Details here.
Use Hibernate multi-tenancy. Details here and here.
Although I haven't tried either.

Using #Entity Class When Table Doesn't Exist

I have several database tables that my Spring MVC/JPA application refers to using the #Entity and #Table Annotations. I've run into the issue where if my application switches between database connections, some tables that exist on database 1 may not exist in database 2 (as we are following the SDLC cycle and promoting table additions/changes after they get the "OK"), thus resulting in an SQL Exception when the application server starts.
Does spring offer a way to mark specific #Entity Classes as "Optional" or "Transactional" so there are no database Exceptions returned because of nonexistant tables?
In my opinion, there is no option to do that.
You can add automatic update of schema in Hibernate, but you mentioned that you are doing this manually.
Hibernate is validating the schema, when he establishes connection. You use #Entity, so he looks for that table and throws an error if there is no with the name specified.

Categories

Resources