我正在尝试将ActiveJDBC与HSQLDB一起使用:
Users.sql
CREATE TABLE users (
"ID" INTEGER GENERATED BY DEFAULT AS SEQUENCE seq NOT NULL PRIMARY KEY,
FIRST_NAME VARCHAR(100) NOT NULL,
LAST_NAME VARCHAR(100) NOT NULL
);
User.java
@IdName("ID")
public class User extends Model {....}
Main.java
User u = new User();
u.setFirstName("first_name");
u.setLastName("last_name");
u.save();
当我尝试保存新行时,我有以下异常:
org.javalite.activejdbc.DBException: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: id, Query: INSERT INTO users (first_name, last_name) VALUES (?, ?), params: first_name,last_name
我认为在插入查询中小写的问题。我该如何解决这个问题?
答案 0 :(得分:1)
CREATE TABLE users (
ID INTEGER GENERATED BY DEFAULT AS SEQUENCE seq NOT NULL PRIMARY KEY,
FIRST_NAME VARCHAR(100) NOT NULL,
LAST_NAME VARCHAR(100) NOT NULL
);
答案 1 :(得分:1)
在Google搜索此HSQL异常消息后,我将此代码放在一起实际运行。它使用ActiveJDBC,使用普通查询,也可以使用检测模型:
Base.open("org.hsqldb.jdbc.JDBCDriver", "jdbc:hsqldb:file:./target/tmp/hsql-test", "sa", "");
String create = "CREATE TABLE people (\n" +
" ID INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) NOT NULL,\n" +
" FIRST_NAME VARCHAR(100) NOT NULL,\n" +
" LAST_NAME VARCHAR(100) NOT NULL\n" +
");";
Base.exec(create);
Base.exec("INSERT INTO people (first_name, last_name) VALUES (?, ?)", "John", "Doe");
System.out.println("===>" + Base.findAll("select * from people"));
Person p = new Person();
p.set("first_name", "Jane", "last_name", "Doe").saveIt();
System.out.println(Base.findAll("select * from people"));
System.out.println(Person.findAll());
Base.close();
如您所见,创建表的代码有点不同,尤其是在ID列周围。
该模型如下所示:
@IdName("ID")
public class Person extends Model {}
基本上,您的代码存在以下问题:
@IdName("ID")
,因为ActiveJDBC默认为小写id
,除非您在表中更改(为什么不呢?)请记住,有些事情可能不起作用,因为HSQL不在受支持的数据库列表中。
我希望这有帮助!