How to split tables among databases with JPA (Eclipselink)? - jpa

Is there a way to split a database using JPA (eclipselink)? I would like to separate the system data from the client data.
I would like to have relationship between them and if possible being one Persisence Unit.

Not sure what you mean. But you can have two persistence units, one for a client database, and one for a system databases.
If you want to use them as a single persistence unit from your application, or want to have relationships between the two, you can use Composite Persistence Units in EclipseLink.
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Composite_Persistence_Units

Related

What is the difference between multiple data sources and multi tenancy?

In context of mongodb and gorm, If we need to have different databases for different clients, then are multi-tenancy (With Database mode) and multiple data source approach are 2 solutions to achieve the same thing or is there any difference between them?
Multiple Data Source Solution:
http://gorm.grails.org/latest/mongodb/manual/#multipleDataSources
Multiple Tenant Solution:
http://gorm.grails.org/latest/mongodb/manual/#multiTenancy
Well they are not meant to achieve the same purpose
tldr;
Multiple Data Source is meant to have different databases (collections if you only plan to use mongodb) for different objects while Multi Tenant will store the same object but add a discriminator to identify client specific data.
If your question is about supporting different databases for different clients the answer will be to go with multi tenant
Multiple Data Sources
Grails supports (for long) to have multiple database for the same application (it can be different db vendor or different db from same vendor). The purpose is to have specific data stored in a different db/namespace.
For example, you can decide to have a db for all core entity of your business and to have a dedicated db for all audit/logging things. When using multiple data sources you will map an object to a dedicated datasource
Multi Tenancy (with database tenant as per OP context)
In mutli tenancy (database tenant) on the other hand, grails will have a single database schema for your client to store all the objects. so data from Client A will be in another db than Client B. Grails will have some default tenant resolver (that you can still override if needed) which will manage to determine which database needs to be queried depending the context.

Multiple database in EF6

We are involved in quite a new development in which we are remaking our current web shop platform.
In the current platform we do not use EF6 neither other ORM but store procedures to access to the db, but in the new building is what we do.
We have a doubt regarding database design of the new platform. In the current platform we use several different databases depending on the content of them.
For example, we have dedicated databases to store information for products catalogs other dedicated db for handling orders.
Currently all data access is done through stored procedures, so we have no problem with the links between different databases.
The problem appears to us now when we have started to use EF6. In this case each DB is associated with a context and it is not possible to know data from one context to another
unless we implement directly in the source code these relationships using various contexts. It looks like these means we will lose the power of EF6.
The questions we have are:
Is it a bad design maintaining different databases for the same application using EF6?
in case this is a poor design and choosing for a single database, is the performance going to be optimum even driving hundreds of tables (almost 1000) with several TBytes of information?
in the other hand, in the case of opting for the design in which several bbdd appear (it would be much better in our case), what is the best way to handle them EF6?
Thank you very much for your help!
First of all EF is not written to be cross database. You can't write cross database (cross context) queries, lazy load does not work and so on.
This is a big limitation in your case.
EF could work with several schema (actually I don't use it and I don't like it but is just my opinion).
You can use your stored procedures with EF but as I understand you are thinking to stop to use them.
In my experience I wrote several applications with more than one database but the use of the different databases was very limited. In this cases I use cross database views (i.e. one database per company and some common tables with views in company databases that selects data in common tables). In your case, if the tables are sharded everywhere I don't think this is a way you can choose.
So, in my opinion you could change the approach.
If you have backups problems you could shard the huge tables (I think facts tables and tables with pictures) and create cross database views. BTW, also, cross database referential integrity is not supported in SQL Server so you need to write triggers to check it.
If you need to split different application functions (i.e. WMS, CRM and so on) you can use namespaces without bothering about how tables are stored in the DB.

Database Crawler using JPA

We have a requirement for building a database crawler. The application parses the tnsnames, connects to each database and retrieves some information like version, accounts, etc. We are trying to use JPA across the other parts of the application and to persist this data into the application's database.
So far, I only see creating an EntityManagerFactory programmatically for every database. Is there any other options?
We are using Spring, are there any benefits that Spring brings to the table in this scenario?
Thanks
JPA is clearly not the right tool for this job. JPA allows creating functional entities mapping a well-know database schema. Your tool doesn't know anything about the schemas and tables it will find. There could be 0 tables or 5000, with completely unknow names.
You need a much lower-level API to do what you want, like JDBC.
You could use JPA to store the results of your crawlings in a single schema, though.

jpa: how to use different databases with relationships between them

In a single Mysql database installation I can create different databases with cross-database relationships. My goal is, for example, to use relationships between people on database A and adresses on database B.
It seems that EclipseLink has recently added support for such cases Composite persistence unit
Unfortunately more exhaustive examples are hard to find and above all the Composite persistence unit merges persistence.xml files from different jar. In the project I'm currently working on I don't have classes and entities divided in different jars based on the database they refer to. Frankly, watching the documentation on Eclipselink, I cannot figure out the real effort to make the code compliant with what Composite persistence unit requires.
Do you have any experience with it ?
Are there alternative ways ?
Thanks
Filippo
Are you databases different databases or just different schemas on the same database?
If the are just different schemas, then you don't need composite persistence units, just set the schema in the #Table.
For different databases you can use composite persistence units, the documentation is here,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Composite_Persistence_Units
You don't need to have all of the persistence unit classes in the jars (although that is the easiest way to do it. You could just have all of the classes in the root jar, and list the classes in the persistence.xml, and on have the composite persistence.xml in the composite jars.

Enity Framework for existing enterprise database

I am working on creating a service layer for a large sql server database (2008 R2) that is currently the backend for a winforms POS application with strongly typed datasets.
I think WCF is the way to go, and at first glance it seemed EF 4 was a good choice but now I'm having my doubts. Here is what I have found:
The stored procedure mapping isn't that great. I have hundreds of stored procs that I want to reuse. Most of them wouldn't return an 'entity' so the stored procs would have to be mapped to a complex type. Many of the procs use dynamic sql or temp tables so EF can't figure out what complex type to crete. Many of the procs return multiple result sets. I've read that EF extensions have a way to map stored procs with multiple result sets, but only for entities, so that doesn't help me.
Large models are a problem. There doesn't seem to be a good way to handle large entity models. The workaround of creating smaller models isn't that desirable and splitting the model loses design support, am I missing something?
EF mappings only go so far. The stored procs that I want to reuse return projections or information from many tables into a result set. There doesn't seem to be a way to map these results into entities, am I wrong? I've read about combining results from 2 table into 1 entity, but that only works if the tables have the same primary key.
Are people using EF in large scale existing databases? If not what would you recommend?
I've used EF on large scale databases, but as you say, the support for SPs as you have got is not great. That's not specifically a failing of EF per-se - ORMs in general work on the same principle and have the same "limitation".
If you have lots of SPs and are mapping them to datasets, you'll have to do lots of work even without SPs in terms of no longer referencing datasets and referencing your domain model types through your system, so you'd need to have some way to map your SPs to your domain model and back anyway.