Search code examples
mysqlhibernateselecthqlsql-insert

HQL auto increment INSERT - SELECT : "Operand should contain 1 column(s)"


I want to ask. i'm using Hibernate

how-to-write-hql-insert-query because of hibernate can't use insert - value, i should use insert - select. but i don't know how to put auto increment value.

i try to make new DB from SQL AUTO INCREMENT Field

CREATE TABLE Persons (
Personid AUTOINCREMENT PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int

);

I try to put a code like this, but it show me an Error

mysql> insert into Persons (Personid,LastName,FirstName,Age)select ('2','Morgen','Lars','12');

or

mysql> insert into Persons (LastName,FirstName,Age)select ('Morgen','Lars','12');

ERROR 1241 (21000): Operand should contain 1 column(s)


Solution

  • The select clause of an INSERT INTO ... SELECT should not be placed in parentheses:

    INSERT INTO Persons (Personid, LastName, FirstName, Age)
    SELECT '2', 'Morgen', 'Lars', '12';
    

    While your problem borders on being a typo, it is understandable since INSERT with values clause does use tuples:

    INSERT INTO Persons (Personid, LastName, FirstName, Age)
    VALUES
        ('2', 'Morgen', 'Lars', '12');