I am pretty new to EclipseLink.
Iwould like to know :
is it possible to create entities from 2 differents databases ? if yes, how ? (example please)
let's say I have Database1 and Database2, is it possible to create composite unit whose one field of entity 1 from database 1 is an entity 2 from database 2. if yes, how (example please)
Thank you very much
Assuming two different schemas on the same server, you should be able to do this using the #SecondaryTable annotation which allows you to map one entity to 2 or more tables. The annotation allows you to specify to the schema or catalog containing the secondary table.
https://docs.oracle.com/javaee/5/api/index.html?javax/persistence/SecondaryTable.html
Would look something like:
#Entity
#Table(name = "main_table")
#SecondaryTable(name="secondary_table", schema="secondary_schema")
public class MyEntity{
#Column(name = "my_field", table="secondary_table")
private String fieldFromSecondaryTable;
}
If you are talking about two different servers then you can look at doing something at the database level which would allow you to create a DB view and then map an entity to this view. This would work for read operations but not sure about writing...
In SQL Server, for example, you would be looking at creating a Linked Server:
Selecting data from two different servers in SQL Server
Related
In my database I have
a Members table which contains basic member details (MemberId [primary key - auto generated number], MemberName, IsActive).
a MembersDetails table which contains more detailed information about the member (Address, Phone, Birthday ...). MembersDetails has MemberId field as a foreign key to the Members table.
There's a reason (part of the app logic) that the 2 tables are separated and are not all in one table.
I've created a view that gets a full member details (a join of the 2 tables), and Entity Framework created an object that represents the view.
I have 2 questions:
Is there a better way of flattening 2 joined tables into an object other than creating a view in the database?
I would like to create an object of the view type (a full user details), initialize it's properties and insert it to the database (which will put the info it needs in the Members table, the the generated id, and than insert to the MemberDetails table). Is there a way to do that?
Ad 1.
I think a database view will be a good choice for performance reasons.
But you can investigate an inheritance provided with the Entity Framework.
It allows you join two separated tables in one model object containing all properties (from "derived" and "base" table). Note, it will be OK for one to one relations (but not for one to many).
Implementing Inheritance with the Entity Framework 6 in an ASP.NET MVC 5 Application
Ad 2.
The Entity Framework inheritance will help you with this issue well. Alternatively, if you can use a database view, just create stored procedures for inserting and updating data included in a view, then map the stored procedures for specified actions on the view model generated by Entity Framework.
I am a beginner to using JPA 2.0 and databases in general and I was just confused about a few concepts.
So I have a total of 3 tables. One is the UserTable, which contains all the information about my user. It has a primary key field called user_Id. My other two tables are ExercisesTable and FoodIntakeTable, and they each have a foreign key field called user_Id to reference the user_Id in my UserTable. I want a one-to-many relationship from my user_Id table to each of the two tables so I can find pull out exercise information or food information for a user.
Pretty much like this:
FoodIntakeTable <-> UserTable <-> ExercisesTable
I need a bidirectional mapping from UserTable to FoodIntakeTable and also a bidirectional mapping from UserTable to ExercisesTable from the field user_Id.
The problem is, when I try to write my code like this in my Usertable class:
#OneToMany(mappedBy="ExercisesTable.userId")
#OneToMany(mappedBy="FoodIntakeTable.userId")
public long userId;
It's illegal because I can't have two #OneToMany annotations on the same field. I think it's supposed to be legal in a normal relational database and I'm just confused about how you translate this into JPA. I'm very new to the whole concept of databases and entities in general, so any help would be appreciated.
In JPA you can directly reference entity objects instead of the ids that they are mapped by. Try something like this:
You should have an entity type for each of your tables, say Exercise for ExercisesTable, FoodIntake for FoodIntakeTable, and User for your UserTable.
Then your User entity is the owning side of the relationships, having one field per relationship like this:
#OneToMany(mappedBy=...)
private List<Exercise> exercises;
#OneToMany(mappedBy=...)
private List<FoodIntake> foodIntakes;
I have a legacy database with a particular table -- I will call it ItemTable -- that can have billions of rows of data. To overcome database restrictions, we have decided to split the table into "silos" whenever the number of rows reaches 100,000,000. So, ItemTable will exist, then a procedure will run in the middle of the night to check the number of rows. If numberOfRows is > 100,000,000 then silo1_ItemTable will be created. Any Items added to the database from now on will be added to silo1_ItemTable (until it grows to big, then silo2_ItemTable will exist...)
ItemTable and silo1_ItemTable can be mapped to the same Item entity because the table structures are identical, but I am not sure how to set this mapping up at runtime, or how to specify the table name for my queries. All inserts should be added to the latest siloX_ItemTable, and all Reads should be from a specified siloX_ItemTable.
I have a separate siloTracker table that will give me the table name to insert/read the data from, but I am not sure how I can use this with entity framework...
Thoughts?
You could try to use the Entity Inheritance to get this. So you have a base class which has all the fields mapped to ItemTable and then you have descendant classes that inherit from ItemTable entity and is mapped to the silo tables in the db. Every time you create a new silo you create a new entity mapped to that silo table.
[Table("ItemTable")]
public class Item
{
//All the fields in the table goes here
}
[Table("silo1_ItemTable")]
public class Silo1Item : Item
{
}
[Table("silo2_ItemTable")]
public class Silo2Item : Item
{
}
You can find more information on this here
Other option is to create a view that creates a union of all those table and map your entity to that view.
As mentioned in my comment, to solve this problem I am using the SQLQuery method that is exposed by DBSet. Since all my item tables have the exact same schema, I can use the SQLQuery to define my own query and I can pass in the name of the table to the query. Tested on my system and it is working well.
See this link for an explanation of running raw queries with entity framework:
EF raw query documentation
If anyone has a better way to solve my question, please leave a comment.
[UPDATE]
I agree that stored procedures are also a great option, but for some reason my management is very resistant to make any changes to our database. It is easier for me (and our customers) to put the sql in code and acknowledge the fact that there is raw sql. At least I can hide it from the other layers rather easily.
[/UPDATE]
Possible solution for this problem may be using context initialization with DbCompiledModel param:
var builder = new DbModelBuilder(DbModelBuilderVersion.V6_0);
builder.Configurations.Add(new EntityTypeConfiguration<EntityName>());
builder.Entity<EntityName>().ToTable("TableNameDefinedInRuntime");
var dynamicContext = new MyDbContext(builder.Build(context.Database.Connection).Compile());
For some reason in EF6 it fails on second table request, but mapping inside context looks correct on the moment of execution.
I have these three tables inside my sql server but when mapping these tables using EF I will lose the table representing the M-M relation -the accountsitemapping table- and in this case I will not be able to know which accounts are linked to which sites.
To workaround this problem I just added a new column to the AccountSiteMapping table and I re-mapped the tables again then I can access the AccountSiteMapping table.
But is there a way to be able to solve this problem without the need to modify my table design ?
By default entity framework will hide all mapping tables in many-to-many relationships. You will have to do your queries this way:
For example, finding SiteDefinitions associated with a given org_ID:
db.SiteDefinitions.Where(a => a.AccountDefinitions.Any(b => b.ORG_ID == org_ID));
I am trying to understand what would be the better way to design 2 entities which has many-to-many relationship? In database there will be a connecting table between these two entities. But do i have to follow the same approach while creating my entities?
For example: User to User group
A user may belong to many group and a group may contain many user.
In relational database I will have 3 table like User, User2Group, Group
So when I am creating my JPA entities, should I have 3 entities for 3 table or just 2 entities by providing the proper annotation(#ManytoMany).
Since I am new to JPA, I am trying to understand good and bad side from following point of view:
Performance
Code maintenance
Thanks, you input will be greatly appreciated.
No, you don't need to map the join table as an entity. Just use the ManyToMany annotation:
public class User {
...
#ManyToMany
private Set<Group> groups;
}
public class Group {
...
#ManyToMany(mappedBy = "groups")
private Set<User> users;
}
You would only need to map the join table as an entity if it was not a pure join table, i.e. if it had additional information like, for example, the date when the user entered in the group.
The mapping has little incidence on the performance. What is important is how you use and query the entities, and how the database is designed (indices, etc.)