28

I'm trying to associate a list of function (whom Embeddable) within my Employee Entity and H2 seems unhappy with this saying that it expected an "identifier"

Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement " CREATE TABLE EMPLOYEE_FUNCTIONS ( EMPLOYEE_EMPLOYEEID VARCHAR(255) NOT NULL, ACTIVE BOOLEAN NOT NULL, DEPARTMENTNUMBER INTEGER NOT NULL, DESCRIPTION VARCHAR(255), ORDER[*] INTEGER NOT NULL ) "; expected "identifier";

The thing is I already done that with an other project and I don't see why it doesn't work.

Employee.java

@Entity
public class Employee extends AbstractScheduleEntity<EmployeeSchedule> {
    public static final String ACOMBA_UNIQUE_FIELD = "acombaUnique";

    @Id
    @GenericGenerator(name = "sequence_id", strategy = 
    "ca.tecsar.core.sql.ServerSequenceGenerator")
    @GeneratedValue(generator = "sequence_id")
    @Column(name = "EmployeeID", unique = true, nullable = false)
    private String employeeID;
    @ElementCollection
    private List<Function> functions;

    //getter and setter
}

Function.java

@Embeddable
public class Function implements Serializable {
    private int order;
    private boolean active;
    private String description;
    private int departmentNumber;

    //getter and setter
}

I removed a few properties in Employee that wasn't necessary. What may cause this error? Is it because I have a String as identifier in my Employee? If so how can I tell to Hibernate to add Employee_EmployeeID as identifier? Thanks

Nicolas Sagala Beaucage
  • 1,229
  • 1
  • 11
  • 23

12 Answers12

71

Turns out I was being dumb and named a column "Order". Wonder why H2 wasn't happy :upside_down:

Changed the variable name to something else and it worked!

Nicolas Sagala Beaucage
  • 1,229
  • 1
  • 11
  • 23
9

I have the same problem while naming the fields: private String to and private String from , changed to ex. dayTo , dayFrom , and it worked.

  • 1
    Exactly my case, I had `from` and `until` dates, that broke my query. Renaming them to `fromDate` and `untilDate` solved the issue. – Tomas Lukac Feb 09 '20 at 13:22
4

Got same issue with Order as entity. Changed table name to "orderz" and goes on.

tarmogoyf
  • 298
  • 3
  • 17
2

I had the same problem with Spring and H2 database for tests, My entity had the field name "interval", I renamed to "inter" and resolved the problem.

So, these errors happen due to a sql reserved names in entities.

Caio
  • 116
  • 1
  • 8
2

I was facing a similar issue because of a field in my entity called "interval". The answers to this post help me identify the root cause, however changing the name of the field was not a solution for me because "interval" was perfect for my class.

The solution I used was to rename the table column by using hibernate annotation.

 @Column(name = "FREQUENCY_INTERVAL")
 private int interval;

This helped me keep the variable name as 'interval' while mapping to a column name that is acceptable by the database.

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
1

I also had an issue with an entity field called row as it is also a keyword

I specified a Column name ("SEAT_ROW") to get around it.

@Entity
data class Seat (
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id :            Long,
    @Column(name="SEAT_ROW")
    val row :           Char,
    val num :           Int,
    val price :         BigDecimal,
    val description :   String
) {
    override fun toString(): String = "Seat $row-$num $$price ($description)"
}

H2 Compatibility and Keywords Table

1

I was using order as table name in h2. Changed to something like _order and I get it!

As you can see here, order is a reserved word by h2.

0

try to put the ; before the " and test ?

user641887
  • 1,506
  • 3
  • 32
  • 50
0

in my case problem cause was incorrect syntax in insert statement problem :

insert into 'table name missing' (column names...) values(values...);

after adding table name

fix:

insert into 'table name specified' (column names...) values(values...);

some times trivial mistakes are hard to spot :)

Guram Kankava
  • 51
  • 1
  • 4
0

For your script will be compatible with reserved words, it would be: "ORDER", see here:

CREATE TABLE EMPLOYEE_FUNCTIONS ( EMPLOYEE_EMPLOYEEID VARCHAR(255) NOT NULL, ACTIVE BOOLEAN NOT NULL, DEPARTMENTNUMBER INTEGER NOT NULL, DESCRIPTION VARCHAR(255), "ORDER" INTEGER NOT NULL );

using the tip reserved words by albertocavalcante user.

RodH
  • 13
  • 4
0

Double check all your column names in your entity. Be sure you are not using reserved keywords, and if you are, add an alias using:

@Column(name = "<YOUR NOT RESERVED KEYWORD COLUMN NAME>")

To the Entity attribute you are suspecting may be a reserved keyword, i.e.

@Column(name = "_user")
private String user
  • Thanks for the answer but, how can I do if the column comes from a Oracle DB, so I can't change the name? I use H2 only for local testing purpose, and connect to Oracle DB in production env. – MSA Sep 01 '23 at 11:59
  • In my case I use: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;MODE=Oracle;NON_KEYWORDS=VALUE – MSA Sep 01 '23 at 13:42
-1

I had the somewhat same problem but instead mine was missing a semi-colon.