I'm not able to update a table with Hibernate.
The table is created with the following statement and stored in a PostgreSQL Database.
CREATE TABLE staat
(
sta_id serial NOT NULL, -- ID des Staates
sta_bezeichnung character varying(50) NOT NULL, -- Langbezeichnung
sta_lkz character varying(10) NOT NULL, -- Laenderkennzeichen
sta_vorwahl character varying(10), -- Telefon Landesvorwahl
sta_eu boolean DEFAULT false, -- Ist der Staaat ein EU-Mitglied?
CONSTRAINT staat_pkey PRIMARY KEY (sta_id),
CONSTRAINT staat_sta_bezeichnung_key UNIQUE (sta_bezeichnung)
)
WITH (
OIDS=FALSE
);
Rights are set correct, because select, insert and update are possible with a SQL Manager. The following update statement also works with the SQL Manager.
But now the problem: when I want to update the table with my application, it generates PSQLException with the following output:
WARNUNG: SQL Error: 0, SQLState: 22004 and ERROR: query string argument of EXECUTE is null
The source code:
Query q = s.createQuery("Update Staat set sta_bezeichnung = 'BlaBla' where sta_id = 1");
int status = q.executeUpdate();
I think the problem has something to do with NOT NULL columns, because tables without NOT NULL columns can be updated with the same source code...
Does anyone has an idea of what is wrong or what I have to do???
Edit: tried with SQL (q.executeSQLUpdate) and HQL
Transaction tr = s.beginTransaction();
staat = (Staat)s.get(Staat.class, new Integer(0));
staat.setStaBezeichnung("BlaBla");
s.update(staat);
tr.commit();
Generates followin error: ERROR: query string argument of EXECUTE is null and Could not synchronize database state with session
Edit2: update works fine without hibernate
Please check your Hibernate mapping file for Staat maybe you have not configured a not-null constraint for some attribute/field, which is not-null in database.
It looks like you are trying to use SQL query where HQL query is expected. Try
Query q = s.createSQLQuery(....);
Or better yet, use mapped classes with HQL. But I don't know your mapped classes, so can't speculate on specifics.
Related
I have problem inserting values in my SQL server database on Azure, I am getting the following error:
Failed to execute query. Error: String or binary data would be truncated in table 'dummy_app.dbo.user_info', column 'uid'. Truncated value: 'u'.
The statement has been terminated.
I don't understand where I am wrong, I just created the server, and I am trying to experiment but cant fix this.
if not exists (select * from sysobjects where name='user_info' and xtype='U')
create table user_info (
uid varchar unique,
name varchar,
email varchar
)
go;
INSERT INTO dbo.user_info(uid, name, email) VALUES('uids', 'name', 'email') go;
Creating the table works fine, the only thing that doesn't work is the second command INSERT
I suspect that the reason is that you haven't defined a lenght for varchar and it defaults to 1 as length. Therefore your value gets truncated.
Set a varchar length to something like varchar(200) and you should be good to go.
This looks like the fact that the CREATE portion of your procedure for the table doesn't include a length of varchar, so you'd have to specify a length such as varchar(50) since the default is 1. Refer to the official MS docs in the link, in the remarks.
docs.miscrosoft.com
Also, here is the syntax for the CREATE TABLE in Azure which might be helpful as well.
Syntax of Azure CREATE TABLE
Does anyone know which of the DB2 special registers are allowed in CREATE TABLE statement for DB2 temporal tables or in general in CREATE TABLE statement?
I am trying to CREATE TABLE COLUMNS WITH CURRENT PACKAGESET or CLIENT_APPNAME, they are not being identified by DB2. I tried almost all combinations of key words (marked in bold).
Create table Statement
CREATE TABLE EMPLOYEE
(EMP_NR INT NOT NULL
,FIRST_NAME CHAR(20) NOT NULL
,LAST_NAME CHAR(20) NOT NULL
,TSROWBEGIN TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW BEGIN
,TSROWEND TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS ROW END
,TSPGMSTART TIMESTAMP(12) NOT NULL GENERATED ALWAYS AS TRANSACTION START ID
**,IDTERMANV CHAR(8) GENERATED DEFAULT WITH CURRENT PACKAGESET
,IDTERM VARCHAR(128) GENERATED DEFAULT WITH CLIENT_APPLNAME**
,STDB2ACTION CHAR(1) GENERATED ALWAYS AS ( DATA CHANGE OPERATION )
,PERIOD SYSTEM_TIME(TSROWBEGIN, TSROWEND)
);
It results in
ILLEGAL USE OF KEYWORD CURRENT. TOKEN WAS EXPECTED. SQLCODE=-199, SQLSTATE=42601, DRIVER=3.68.61
or
ILLEGAL USE OF KEYWORD CLIENT_APPLNAME. TOKEN WAS EXPECTED. SQLCODE=-199, SQLSTATE=42601, DRIVER=3.68.61
Any suggestions on how to create column with default value of program name which is doing CUD operation on the table?
You can use the special registers for current date / time / timestamp and for user information (user, session user, system user). Take a look at the CREATE TABLE reference.
The same reference also has a section about what cannot be used, in case you try to use a function or put the CREATE statement into a procedure. Among other things, the GENERATED value cannot be based on the following:
Special registers and built-in functions that depend on the value of a special register.
create table JSON_TAB (JSON_VAL CLOB);
-- tried to add the constraint like this CONSTRAINT JTE_CK check (JSON_VAL is json) butt says it expects null so I didn't create it for now.
I am trying to execute the statement
SELECT JT.Ime, JT.Broj, JT.Pozicija
FROM JSON_TAB JTE,
JSON_TABLE (JTE.JSON_VAL, '$.players[*]'
COLUMNS (Ime VARCHAR2(20) PATH '$.name',
Broj NUMBER PATH '$.number',
Pozicija VARCHAR2 PATH '$.position')) JT ;
and receiving an error SQL command not properly ended.
JSON_VAL is a CLOB!
Is it a problem with syntax, data type or something else?
I took the example from youtube tutorial and entered it manually.
ORACLE version is 12.1.0.2.0.
I am migrating some tables and stored procedures to in-memory optimized tables and natively compiled stored procedures and am getting stuck on a null comparison.
Here is my code:
CREATE TABLE [dbo].[MyInMemTable]
(
[MyId] int NOT NULL IDENTITY(1,1),
[MyData] varchar(900) COLLATE Latin1_General_100_BIN2 not null
CONSTRAINT [PK_MyInMemTable] PRIMARY KEY NONCLUSTERED ([MyId])
) WITH (MEMORY_OPTIMIZED = ON, DURABILITY=SCHEMA_ONLY)
GO
CREATE PROCEDURE [dbo].[sp_InsertIntoMyInMemTable](#MyData varchar(900))
WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
AS
BEGIN ATOMIC WITH (TRANSACTION ISOLATION LEVEL = SNAPSHOT, LANGUAGE = N'English')
IF #MyData IS NOT NULL
BEGIN
INSERT INTO dbo.[MyInMemTable] (MyData) VALUES (#MyData)
SELECT SCOPE_IDENTITY()
END
ELSE
SELECT 0
END
I get the following error:
Msg 12327, Level 16, State 101, Procedure sp_InsertIntoMyInMemTable, Line 306
Comparison, sorting, and manipulation of character strings that do not use a *_BIN2 collation is not supported with natively compiled stored procedures.
How do I specify a collation on the parameter, or is there an alternative way to get this null comparison to work?
please see the comments from #Eric J, above.
You can add " Collate {Collation Value} " pretty much anywhere you can specify a field with collation it seems. Did you try " IF #MyData Collate {Collation} IS NOT NULL"?
I just put this as answer to help people finding it.
Updating with pmbAustin's suggestion
pmbAustin, thanks. I think that will work. I've created a function "dbo.CK_WinteamSportExists" that returns a 0 or 1. However I'm now getting a baffling error message. When I tested my solution I get the following:
create table #check
(
SportID int
,WinTeamID int
,LoseTeamID int
,check
(
(dbo.CK_WinteamSportExists (WinTeamID , SportID) = 1)
)
)
Error Message:
Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.CK_WinteamSportExists", or the name is ambiguous.
I tried using a fully qualified name "mydatabase.dbo.CK_WinteamSportExists", no dice.
It appears that I've got the syntax wrong in the check, but for the life of me I can't find the error.
Original Question
I am trying to enforce referential integrity on a table variable. I'm working in SQL Server 2012.
declare #GameID_Table table (
GameID int
,SportID int
,WinTeamID int
,LoseTeamID int
)
Combinations of WinTeamID/SportID and LoseTeamID/SportID in #GameID_Table must be constrained by the existence of a matching row in a separate table Link_TeamSport defined as:
create table Link_TeamSport
(
TeamID int,
SportID int
primary key (TeamID, SportID)
)
Normally I'd do this with a composite foreign key, however these are not allowed in table variables.
I also tried creating user-defined functions that check for the existence of a matching row in Link_TeamSport, but UDFs are not allowed in table variables either.
Here is an example of working code that illustrates my constraint condition. The code below returns a result of 1 if a matching row is found and 0 if not. However, I cannot figure out how to incorporate this concept into the check constraint within #GameID_Table.
declare #winteam int
declare #sportid int
set #winteam = 1001
set #sportid = 4001
select count(*) from
(
select lts.TeamID, lts.sportid from Link_TeamSport lts
where
(#winteam = lts.TeamID and #sportid = lts.sportid)
) a
Use a #TempTable instead. #TableVariables have a lot of restrictions that #TempTables don't, and also don't perform as well if you're going to have more than just a few rows in them.