I'm using Oracle DB as RDBMS, and I want to access, via my JSF2 application, to two database schema.
So, I think I must use two <persistence-unit> in my persistence.xml ?
If accessing two database schemas means just that some of the entities should be in different schema, that can be easily done with Table annotation:
#Entity
#Table(schema="someotherschemathandefault")
public class EntityInOtherSchema {
...
}
If those schemas need different credentials to access (or different datasources to be used), then defining two persistence units is way to go.
Related
I have 5 domains and i need to separate each domain with different database,so how can I do that separation using Entity Framework.
Domain driven design has no opinion on your persistence mechanism such as how you design or structure your database or databases. So from a DDD perspective, the answer is "however you like"!
But, if you would like to separate your domains into different databases (even though you don't have to) your options are:
Keep everything across all your domains in one DbContext and pass interfaces into your classes that give access to only particular domain classes, such as IOrdersRepository that accesses Orders from the DbContext, and an IInvoicesRepository that accesses only Invoices from the same DbContext.
Add multiple DbContexts to your app for each domain, like SalesDbContext, BillingDbContext, MarketingDbContext and give each DbContext its own connection string in your Startup.cs file.
Split your app into smaller projects (like microservices) like a Sales.Api with its own DbContext, and a Billing.Api with its own DbContext.
There may be other options I haven't thought of too, seeing as DDD doesn't prescribe anything about how you store your data.
JPA: is there a way to map some entities to a schema of another database instance? e.g.,
#Entity
public class Foo {
}
#Entity
#Table(schema="schema1")
public class Bar {
}
The Bar entity is mapped to the schema1 of the same database instance. Is there a way in JPA to map it to a schema in a remote database instance? It is useful for sharing entities among multiple applications.
Can the "catalog" be used for this purpose?
What do you mean by 'remote database'?
If you use #Table(schema = "myschema", name = "bar"), Hibernate will qualify all queries with the schema name (e.g. SELECT e FROM Bar will ultimately translate to SELECT * FROM myschema.bar). If the database user you're using to connect to the DB has access to myschema.bar (whatever such a DB object is), then the query will work; if not, then the query will fail.
If you mean 'a remote DB that is a separate server', then, of course, you can only connect to the DB using one JDBC connection per persistence context. If that's your scenario, perhaps you should consult the docs of the RDBMS for ways to connect two DB instances (in Oracle, for example, you could use database links and synonyms).
Make sure that you understand the implications, though, as such a solution introduces its own class of problems (including the fact that you suddenly have implicit distributed transactions in your system).
As a side note, I'm not sure how such an approach is 'useful for sharing entities among multiple applications' or why one would even think 'sharing entities among multiple applications' is somehow useful, but I'd seriously think through the idea of integrating multiple application via shared/linked DBs. It usually introduces more problems than it solves.
If I understand well what you mean, you should use two (or more) different persistence context
I'm currenly designing an application where I need to use two different database schemas (on the same instance): one as the application base, the other one to customize the application and the fields for every customer.
Since I read something about Repository pattern and as I've understood is possible to use two different contexts without efficiency loose, I'm now asking if I can use a single database transaction between two schemas with Entity Framework, as I'm actually doing directly on the database (SQL Server 2008-2012).
Sorry for my English an Thanks in advance!
If your connection strings are the same (which in your case will be as you have different schemas only for different contexts) then you are ok with this approach.
Basically you will have two different contexts that will be connected via the same connection string to the database and which will represent two different schemas.
using (var scope = new TransactionScope()) {
using (var contextSO = new ContextSchemaOne()) {
// Add, remove, change entities from context schema one
ContextSchemaOne.SaveChanges;
}
using (var contextST = new ContextSchemaTwo()) {
// Add, remove, change entities from context schema two
ContextSchemaTwo.SaveChanges;
}
scope.Complete();
}
I wasn't very successful in the past with this approach, and we switched to one context per database.
Further reading: Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?
Maybe it's better to read something about unit of work before taking a decision about this.
You will have to do something like this: Preparing for multiple EF contexts on a unit of work - TransactionScope
Can we create entities classes from Database views using JPA Tools the way we do for database tables?
Manuj
You use the same syntax as you would for creating entity classes for a table. The only differences are: 1) typically views are read only, thus your entity will likely only be used as read only, and 2) Like all entities you need to have a primary key, thus your view will need to either have one column that has unique identity values, or a combination of columns that can be used as a unique identity.
The views are also available in the list of tables when you specify the connection and the schema.
Im currently working in a team that uses EF as the ORM of choice.
We have a common project that contains many EDMX files.
The reason for this is to keep the EDMX files small and manageable while also allowing them to focus on a conceptual set of tables on the database.
Eg
Orders.edmx
Users.edmx
Trades.edmx
These all point to a different set of tables on the same db.
I now need to add the user table to the Trade.edmx file. Since the user table is already in the user.edmx file, this creates the same User type twice under a different namespace which means I would need 2 UserRepository objects.
Common.data.trade.User
Common.data.users.User
Is there a way of avoiding 2 repository objects for the same table?
Any suggestions would be greatly appreciated
If you are using POCO generator you can update template for Trades.edmx to not generate new User class and its context template to use User class from Users namespace. EF matches POCO classes with entities in designer only by the class name (namespace is omitted) so it will work.
The disadvantage is that you have User entity in two mapping files and you must update it in both files or your application throw exception at runtime.
The reason for this problem is your architecture - at the beginning you wanted separated models but know you want to combine entities from different models. Those are contradicting requirements. Either use separated model where Trade knows only userID without any navigation property (like if it is defined in another database) or move all entities to single EDMX to support your new requirements.