I am using Postgres as database and JPA 2.0 as provider.
I had been using #GeneratedValue(strategy = GenerationType.IDENTITY) which generates its own sequence for that column and also executes this query ( or similar )
ALTER TABLE "comment" ALTER COLUMN id SET DEFAULT nextval('comment_id_seq'::regclass);
Now I am using
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GEN_COMMENT")
#SequenceGenerator(name = "GEN_COMMENT", sequenceName = "SEQ_COMMENT")
which also generates it's own sequence but doesn't execute that query.
My point is that when I had been using IDENTITY then database could automatically insert the column value from sequence which is helpful when there are other accesses into database except via application.
So my question is if there is any way how to force JPA to set default value of that column to sequence without redefinition of #Column(columnDefinition="...")?
Thanks a lot.
This is database specific, the Postgres has defined type serial as generation type IDENTITY which automatically creates this database structure.
Related
I am facing an issue with Spring MVC and Hibernate 4.2.1.
With Oracle DB connection, when save() method is performed I obtain a Long value that describes the ID of the saved record. Using a Sql Server instance, otherwise, the save() returns always a Long with 0 value.
I've printed the query in the console and I've noticed that the ID field is missing (when the operation is performed on Oracle, the ID field is present).
Following, the snippet of the entity class with the highlight on ID column:
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "seq")
#SequenceGenerator(name="seq", sequenceName="SEQ_PEZZI")
#Basic(optional = false)
#Column(name = "id")
private Long id;
The field type in the database is bigint.
I've also tried to change the GenerationType with IDENTITY without success (the type SEQUENCE seems not supported in SQL Server...)
given below is the create statement for the table I created using flink.
CREATE TABLE event_kafkaTable (
columnA string,
columnB string,
timeofevent string,
eventTime AS TO_TIMESTAMP(TimestampConverterUtil(timeofevent)),
WATERMARK FOR eventTime AS eventTime - INTERVAL '5' SECOND
) WITH (
'connector' = 'kafka',
'topic' = 'event_name',
'properties.bootstrap.servers'='127.0.0.1:9092',
'properties.group.id' = 'action_hitGroup',
'format'= 'json',
'scan.startup.mode'='earliest-offset',
'json.fail-on-missing-field'='false',
'json.ignore-parse-errors'='true'
)
The table above, listens to Kafka and stores data from the topic in Kafka named event_name. Now, I want to ALTER this table, by adding a new column. Following were the ALTER commands I tried running from my flink job:
1. ALTER TABLE event_kafkaTable ADD COLUMN test6 string;
2. ALTER TABLE event_kafkaTable ADD test6 string;
Both these commands threw an Flink SQL Parser exception.
The Flink's official website, https://ci.apache.org/projects/flink/flink-docs-release-1.11/dev/table/sql/alter.html, has not listed the syntax to add or drop a column from a table. Can you please let me know, what is the syntax to add or drop a column to a table using Flink's Table API.
This is not supported yet in the (default) SQL DDL syntax, but you can use the AddColumns and DropColumns Table API methods to perform those operations.
This documentation page has examples on how to use them for each supported language.
I have a 12 years old mdb database and I was asked to add a new column to a table and set to a default value of "1".
My knowledge of asp/mdb is close to zero. I also have no Access or similar softwares.
I tried with:
ALTER TABLE Members ADD COLUMN Privacy Double Default 1
but generates error:
Error: An action query cannot be used as a row source.
Then I tried with:
ALTER TABLE Members MODIFY COLUMN Privacy VARCHAR(4) NOT NULL DEFAULT 'Yes';
but this also triggers another error:
Microsoft JET Database Engine error '80040e14'
How can I set an existing column to a default value?
Should I use an offline tool? If so which one?
Note: I also used an online tool to create the new column but it has no option to set a default value.
So I can either create the new column with the tool and set a default value with SQL, or do the creation of the column with the default value still with SQL.
I solved using AxBase from SourceForge.net.
I could properly open the *.mdb database and run the above SQL commands and they worked perfectly:
ALTER TABLE Members ALTER COLUMN Privacy SET DEFAULT 1
hi your access database is may be in office 2003 or office xp.
You can try to enable sql syntax and then try to run your query.
Tools -> Options -> Tables/Queries -> (At the bottom right:) Sql Server Compatible Syntax - turn option on for this database.
ALTER TABLE Members ADD COLUMN Privacy number Default 1
I need to make an update to in Oracle's database using SSIS. I am using the custom database task and using a query like:
UPDATE Table SET column1 = ? where KEY = ?
The key is taken from SQL Server table and is a type of nvarchar(3), the key in oracle database is of type varchar2(3). First it was complaining that the key is 4 character so I change the query to
UPDATE Table SET column1 = ? where KEY = TRIM(CAST(? AS VARCHAR(3)))
It is working for keys which has the 3 characters, but there are also the 2 characters long one. I've tried trim it, convert it. But I cannot make it work for 2 characters keys.
Oracle chararcter set for char is AL32UTF8 and for NCHAR - AL16UTF16.
I've resolved the problem by creating the derived column with KEY_LEN as LEN(key) before the update step. Then I've used it in update as the third param:
UPDATE Table SET column1 = ? where KEY = SUBSTR(?,0,?)
I just tried inserting value in to a database and that work. Now I insert again and I get an error for identical primary key.
I can't find any option to alter it to be auto-increment.
I'm updating the table via Linq-To-Sql.
User u = new User(email.Text, HttpContext.Current.Request.UserHostAddress,
CalculateMD5Hash(password.Text));
db.Users.InsertOnSubmit(g);
db.SubmitChanges();
I didn't fill in the user_id and it worked fine the first time. It became zero.
Trying to add a second user, it wants to make the ID 0 again.
I could query the database and ask for the highest ID, but that's going to far if you know about auto-increment.
How can I turn this on? All I can find are scripts for table creation. I'd like to keep my existing table and simply edit it.
How is your Linq-to-SQL model defined?? Check the properties of the user_id column - what are they set to??
In your Linq-to-SQL model, be sure to have Auto Generated Value set to true, Auto-Sync set to OnInsert, and the server data type should also match your settings (INT IDENTITY),
In SQL Server Management Studio, you need to define the user_id column to be of type INT IDENTITY - in the visual table designer, you need to set this property here:
It is zero because you have a integer for a primary key column type. To use auto-increment, set tables identity column to the ID (selected in the table properties)
Would probably be easier to edit the database using VS if you have a version that will work for, otherwise if you have to edit it in management studio see this article:
http://blogs.msdn.com/b/sqlexpress/archive/2006/11/22/connecting-to-sql-express-user-instances-in-management-studio.aspx
Or you can increment the user_id manually and pass it to the insert function if you cannot alter the property/table field description