I'm working in a project where I'm supposed to read data from a database, called EBS, send it to the front-end and use it alongside other data to persist in another database. Let call it CPP.
The EBS must be read-only. Nothing should be persisted there. And they want all the specific queries to be stored in a xml file. For now, it's stored in the orm.xml.
I tried this solution, but it ended up creating a new table in the EBS and nothing was returned from CPP.
My questions are:
1 - How do a specify those native queries in orm.xml should be run in the EBS datasource? The datasource of CPP is in the application.properties file.
2 - Is it possible to JPA not create table with the #Entity annotation? Every time I use that, JPA ends up creating another table in the EBS. If not, is it possible to map the query directly to a POJO?
I would appreciate a solution based on JpaRepository. Thanks in advance.
There can be multiple persistence-unit in persistence.xml file but the name of those persistence units should be different .
If you don't want the entities to be created automatically then use application-managed entity manager
refter JPA application-managed entity manager
Related
I am making a web app with Spring Boot. I am planning on using Spring Data JPA (Hibernate) connected to MySQL. One question I have is that I have already written out the SQL statements to create my database schema. I have my primary keys, foreign keys, relationships all set already. All the tutorials I've seen assume you don't have tables or anything in the database already. If I create entities, how can I ensure they match up to my already existing data model?
I don't mind recreating the entities by hand, since there's not that many classes and manually reverse-engineering won't take too much time. (I have seen a way to reverse engineering using Eclipse but I prefer to use IntelliJ, also I feel that manually writing the Entities may be easier than doing the reverse process.) But I want to make sure that the entity objects I create will match the tables I already have and won't cause trouble. I'm not sure what the best way to go about doing this is. Will Hibernate overwrite my existing schema when I run for the first time?
You can make hibernate to validate your mapping.
Set this property :
hibernate.hbm2ddl.auto=validate
I have two databases defined in my web application. One for fashion and one for sports. Currently Im accessing the sports database. Now that I want to change it to fashion database means how can I do that ? I'm using spring with hibernate. Is there any in-built support for this either in spring or hibernate ?
EDIT 1:
I will have a web page where I can choose which database to use and then the changes should get reflected automatically. Is there any way to do it ?
FYI : Both the databases will have the same design. Only the data will be different
Hibernate specific configuration may differ slightly, but with JPA you need to have two EntityManagerFactories configured, one for each database. Then you would use either one depending on which database you want to access.
You can't do this with a single EntityManagerFactory (or the pure Hibernate equivalent).
I think this is an answer to your question. You can simply add a second PersistenceUnit and create a second Factory for your Entity Managers for it.
I hope this helps!
Kind regards
Mysql only has one database. It has many schemas (which it calls databases). You can query a table in another schema by prefixing it: schema_name.table_name .
I trying to use EclipseLink JPA provider with H2 DBMS in Eclipse IDE.
When I create a new JPA project and fill the data (URL="jdbc:h2:~/test",Username="user",password="") connection type : resource local (I'm using the embedded mode)
I get 2 odd things :
when I try to create Entities from table I get a blank table list
I get an error message near #Entity notation saying that "catalog User can't be resolved for entity e1" or "Schema User can't be resolved for entity e1" depending on keeping the default value for catalog or changing it upon project creation where User is the database user name
The database already contains tables using the h2 console in Firefox
What is the cause of these problems and how can I solve it and do you have any page or book that can help with persistence.xml file (other than the oracle official web site)?
EclipseLink doesn't create the database tables for you, unless you explicitely tell it to do so.
The way to do that is described in the documentation
EclipseLink can be used to automatically generate the tables and database schema for a persistence unit. This is done through the "eclipselink.ddl-generation" persistence unit property, set to either "create-tables" or "drop-and-create-tables". The tables and constraints will be generated for all of the classes defined in that persistence unit.
The trick was to set the right catalog when creating the JPA Project and then every thing worked splendidly
I am using Hibernate's multi-tenancy feature via JPA, with a database per tenant strategy. One of my requirements is to be able to run a query against a table that exists in each database but obviously with different data. Is this possible?
Thanks in advance for your time.
Nope. this is not possible because when hibernate runs queries it is already initialized with a connection. MT support in Hibernate is basically done a little "outside of Hibernate" itself. It's kind of feeding hibernate with a proper connection and when it's fed :) it's bound to that connection.
If you need cross-tenant queries you might want to reconsider multitenancy or change JPA provider to the one that support "shared schema approach" e.g. EclipseLink. With shared shema approach you have two choices:
run native query agains table containing mt-aware entities
create additional entity - dont mark it as multitenant - map it to the table containing mt-ware entities and run JPQL query in standard manner
I use JPA annotations (Hibernate implementation) to initialize my DB schema. And i follow the article DYNAMIC DATASOURCE ROUTING to implement the dynamic datasource routing class.
However, i have two databases (mapped 2 data sources). I set the first data source as defaultTargetDataSource. then start my application. When my application try to access 2nd data source, it tell me the table doesn't exist. It seems AbstractRoutingDataSource only create the table for the default data source but other data sources.
Is there any idea to create schema in all databases ?
PS.I'm using AbstractRoutingDataSource to implement my own DB shards.
I guess that you are using the hibenate configuration:
spring:
jpa:
hibernate:
ddl-auto: update
to reflect the entity changes to the database schema. This works fine as long as we use a single data source that is configured to be connected at startup.
However, if you have multiple data sources it is not possible to use this feature. The general approach with AbstractRoutingDataSource is to not have a data source at startup but select it at runtime.
If you select a primary data source, then it will be only applied to the primary one as hibernates applies this feature at startup, but the remaining databases will not be migrated.
To reflect the changes to all of your databases you can use a database migration tool such as Flyway or Liquibase.
Flyway is using SQL and pretty easy to configure and use to use.