How to insert the Map<String,List<other_table_type>> in orient db - orientdb

I have a table column in orient db, which has to be of form,
column: [env1 : [R1,R2,R3],
env2 : [R1,R2]]
How can i achieve this?
I tried using LinkMap, linkset, linklist... But unable to achieve..

EMDEDDEDMAP data type needs to be used for the column. Below is an example class with such a property:
CREATE CLASS Entity IF NOT EXISTS;
CREATE PROPERTY Entity.propertyAccessAssignments IF NOT EXISTS EMBEDDEDMAP EMBEDDEDSET STRING;
Query of the below form (SET will work with CREATE VERTX/EDGE also) can be used to insert data
UPDATE #216:0 SET propertyAccessAssignments={'a':['b']}
Where #216:0 is the rid of the record, standard where clause will also work

Related

How to make mybatis to map a list to Object

I have a device table which defines the device entity:
device:
id:
type:
And I have a attribute table which records the device's attributes:
device_attr:
device_id:
key:
value:
How can I write the mappers to save the device POJO into the table and how to load the attributes into the POJO? thanks.
Pivot function may meet this, but, mysql doesnt support it, and it's performance is not good.
In order to save the object you can use dynamic sql to iterate over POJO properties and generate INSERT statements similar to what is described in How to use MyBatis to iterate all fields of an object? :
<bind name="deviceProperties"
value="#org.apache.commons.beanutils.BeanUtils#describe(myDevice).entrySet()" />
<foreach index="propertyName" item="propertyValue" collection="deviceProperties">
INSERT INTO device_attr (key, value) values (
${propertyName}, ${propertyValue});
</foreach>
There is no out of the box solution to do the same for the select.
You need to do what is called table pivot but this is very database specific.
Another option is to recreate the POJO from the list of fields in java. It can be done in mapper using default methods like described in this answer.

How to correctly add a list of objects to database, working with multiple tables?

My database looks like this: Database Diagram MSSMS
So everytime i'm adding a fish to the database, i want to add the fishes's continent, waterlagen and verbanden.
Tables Continenten, Waterlagen & Verbanden are already filled with objects from an array after creating the database.
Im using List Continenten; for example to store multiple continents.
So i tryed:
Vis nieuweVis = new Fish();
nieuweVis.Naam = "Molly";
foreach(Continent c in Continenten)
nieuweVis.Continenten.Add(c)
so in the table VisContinenten
i assume that EF will automaticly fill in the FishId and ContinentId wich are foreignkeys.
I want records in that table also to be unique so i add a unique key to both the columns in VisContinenten so that fish 1 on continent 1 won't appear twice in that table.
Error i get:
An error occurred while saving entities that do not expose foreign key
properties for their relationships
Additional information: Unable to update the EntitySet 'VisContinenten' because it has a DefiningQuery and no element exists in the element to support the current operation.
Help me pls :)
Thank you

Entity Framework : map duplicate tables to single entity at runtime?

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.

EF Table per Hierarchy (TPH) not saving because can't insert value null in Discriminator column

I have a table used for multiply type of category and it contains a Discriminator column named 'ClassName' to specify the type of object to load. The ClassName column is non nullable with a default value of 'Category'
My problem is when saving a new item , I get the error :
'Cannot insert the value null into column ClassName' table Category.
I tought that ef would set the ClassName value base on the new object class.
How can I save my object with the right 'ClassName' value ?
I changed my db Structure to accept null. EF will set null if the object name match the table name and will set the discreminator name for the derived classes.
It's old question but I just met it today. In .net 4.0 code first I don't meet "disclaimer column" but when downgrade to net 3.5, it make me tired for a day.
This is my solution
Change the column in the database to ALLOW NULL (using alter table)
and update the edmx file to make it update the change(allow null)

How to manage GetDate() with Entity Framework

I have a column like this in 1 of my database tables
DateCreated, datetime, default(GetDate()), not null
I am trying to use the Entity Framework to do an insert on this table like this...
PlaygroundEntities context = new PlaygroundEntities();
Person p = new Person
{
Status = PersonStatus.Alive,
BirthDate = new DateTime(1982,3,18),
Name = "Joe Smith"
};
context.AddToPeople(p);
context.SaveChanges();
When i run this code i get the following error
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated.
So i tried setting the StoreGeneratedPattern to computed... same thing, then identity... same thing. Any ideas?
You have to manually edit the edmx xml and set your SSDL StoreGeneratedPattern attributes to identity or computed. But whenever you update your edmx via the designer your changes will get overwritten.
This is a known issue. Please see the following links for more details:
Microsoft Connect Ticket
Using a GUID as an EntityKey in Entity Framework 4
I had the same problem! For me it works like this:
Database MS SQL Server express:
[RowTime] [datetime2](7) NOT NULL
ALTER TABLE [dbo].[Table_1] ADD CONSTRAINT [DF_Table_1_RowTime] DEFAULT (getdate()) FOR [RowTime]
GO
Then I import the table from database to my Entities model.
Entities will not realise the default value!
So, you have to set the StoreGeneratedPattern of the column to Computed.
Then Entities will not put there any default value any more.
Combination of:
datetime2,
NOT NULL,
StoreGeneratedPattern=Computed
Works for me!
Changing type of DateCreated to datetime2 might solve the problem.
datetime 2007-05-08 12:35:29.123
datetime2 2007-05-08 12:35:29. 12345
Ref: http://technet.microsoft.com/en-us/library/bb677335.aspx67
Here is a working workaround:
1) Change the column to datetime2 as mentioned elsewhere. This fixes the conversion error.
2) Add a trigger that sets DateCreated to getdate();
CREATE TRIGGER [TR_AS_ChangeTime] ON [AS_ApplicationSession]
AFTER INSERT,UPDATE AS
BEGIN
SET NOCOUNT ON;
UPDATE AS_ApplicationSession
SET AS_ChangeTime = getdate()
WHERE AS_Id IN(SELECT AS_ID FROM INSERTED)
END
3) If neccessary, set
p.DateCreated = DateTime.MinValue;
just to initialize it.
4) If you need the DateCreated from the database, add
context.Refresh(System.Data.Objects.RefreshMode.StoreWins, p);
just after
context.SaveChanges();
Focusing on the fact that I do not want change the database, since it is an application problem and I expect them to solve this problem one day, my solution (that is totally possible in my case) is to create a partial class of the model to correct the problem on constructor:
public partial class Log
{
public Log()
{
this.Date = DateTime.Now;
}
}
This works for me because:
I create the model on the moment I send it to database.
I use CLOUD for these services, the datetime must be the same in both Application and Database servers!
Don't forget that the namespace needs to match the Model namespace or partial should not list properties (and should no be partial as well ;])!