for inserting value to the table using entity framework I have used this:
db.FileWavs.AddObject(wav);
db.SaveChanges();
To insert foreign language text I used N in query in front of values:
INSERT INTO FileWav (Description) values( N'थिच्नुहोस' )
But How can I used it in above code of entity framework.
You don't need to do anything in entity framework, just make your field nvarchar() in database and you are done, then you can pass any UTF-8 without doing anything to entity framework.
Related
Following is my table schema
CREATE TABLE Animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
In mysql I can directly insert multiple records in one insert sql query. Like following.
INSERT INTO animals (name) VALUES('dog'),('cat'),('penguin'),('lax'),('whale'),('ostrich');
However, how can I achieve the same thing in spring data jpa.
Right now I am using CrudRepository's Iterable save(Iterable entities); and I am eneded up with 6 insert statements
insert into Animals (name) values (?)
insert into Animals (name) values (?)
insert into Animals (name) values (?)
insert into Animals (name) values (?)
insert into Animals (name) values (?)
insert into Animals (name) values (?)
How do I restrict to One insert query ? Any answer will be helpful regarding spring data jpa, hql or jpql .
Assuming that you are using hibernate you need to tweak a couple of settings. You need to enable batching by putting a value in the hibernate.jdbc.batch_size property. However that might not cut it as depending on the number of different statements you also might need to order them to be able to batch them. For this you need to set the hibernate.order_inserts and hibernate.order_updates to true.
If you are using versions or timestamps to limit concurrent modification you might also need to enable hibernate.jdbc.batch_versioned_data
So all in all you probably need to add something like this to your configuration or persistence.xml
properties.put("hibernate.jdbc.batch_size", "25");
properties.put("hibernate.order_inserts", "true");
properties.put("hibernate.order_updates", "true");
properties.put("hibernate.jdbc.batch_versioned_data", "true");
A blog post explaining a bit more can be found here.
However if you are using MySQL it might not even work (yet) as MySQL requires an additional setting on the datasource to enable the usage of the addBatch for JDBC. You need to set the rewriteBatchedStatements to true to enable this feature for MySQL. Depending on your datasource you can add this to the connection string or as a seperate property.
I am reading a tutorial about Yii framework. It uses a term that I don't know its meaning. it is "Active Record model" . It uses in database section tutorial.
Would you give me more explanation about this term ?
From the Yii's documentation:
Active Record (AR) is a popular Object-Relational Mapping (ORM) technique. Each AR class represents a database table (or view) whose attributes are represented as the AR class properties, and an AR instance represents a row in that table. Common CRUD operations are implemented as AR methods. As a result, we can access our data in a more object-oriented way.
For example, we can use the following code to insert a new row to the tbl_post table:
$post=new Post;
$post->title='sample post';
$post->content='post body content';
$post->save();
In the following we describe how to set up AR and use it to perform CRUD operations. We will show how to use AR to deal with database relationships in the next section. For simplicity, we use the following database table for our examples in this section. Note that if you are using MySQL database, you should replace AUTOINCREMENT with AUTO_INCREMENT in the following SQL.
CREATE TABLE tbl_post (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
title VARCHAR(128) NOT NULL,
content TEXT NOT NULL,
create_time INTEGER NOT NULL
);
There is more on here.
I've Table uses Trigger and sequence to set its PK column.
The Hibernate mapping strategy for its Pk is assigned..
This yields in session.save(obj) returns object with id=0
How to make it returns the correct assigned PK value.
session.getIdentifier() doesn't work!
assigned means: Nobody generates the ID, the ID is set explicitely in the entity before persisting it.
What you want to do is impossible. Hibernate would have to insert an entity without knowing its ID, then the database would generate the ID, and Hibernate would have to reload the entity from the database to know its ID. But how would it reload the entity without knowing its ID?
The native generator does the same thing, and it works because the database provides a getLastGeneratedId() method which allows getting the IOD that the database has generated. But you can't do that with Oracle and a trigger.
Remove the trigger from the database, use the sequence generator, and everything will be fine.
I am using Entity Framework for CRUD operations. When I tried to insert data in a table which has an identity column, it throws an exception
Identity_Insert is set to off
I don't know how to turn that on. Do I need to reconfigure entity data model for that?
Sounds like the EF is including an explicit value for the PK in the INSERT. It should not do this. Make sure StoreGeneratedPattern is set to identity on the PK in the SSDL. This article might help.
I need to insert records into a table that has no primary key using LINQ to SQL. The table is poorly designed; I have NO control over the table structure. The table is comprised of a few varchar fields, a text field, and a timestamp. It is used as an audit trail for other entities.
What is the best way to accomplish the inserts? Could I extend the Linq partial class for this table and add a "fake" key? I'm open to any hack, however kludgey.
LINQ to SQL isn't meant for this task, so don't use it. Just warp the insert into a stored procedure and add the procedure to your data model. If you can't do that, write a normal function with a bit of in-line SQL.
Open your DBML file in the designer, and give the mapping a key, whether your database has one or not. This will solve your problem. Just beware, however, that you can't count on the column being used for identity or anything else if there isn't a genuine key in the database.
I was able to work around this using a composite key.
I had a similar problem with a table containing only two columns: username, role.
This table obviously does not require an identity column. So, I created a composite key with username and role. This enabled me to use LINQ for adding and deleting entries.
You might use the DataContext.ExecuteCommand method to run your own custom insert statement.
Or, you might add a primary key to a column, this will allow the objects to be tracked for inserts/updates/deletes by the datacontext. This will work even if the column isn't really an enforced primary key in the database (how would linq know?). If you're only doing inserts and never re-use a primary key value in the same datacontext, you'll be fine.