无法使用ActiveJDBC将对象插入数据库

时间:2013-06-13 17:17:47

标签: java hsqldb activejdbc

我正在尝试将ActiveJDBC与HSQLDB一起使用:

Base.open("org.hsqldb.jdbc.JDBCDriver", "jdbc:hsqldb:file:queen-db;shutdown=true;hsqldb.write_delay=false;", "sa", "");

    User e = new User();
    e.set("id", 1);
    e.set("nickname", "Superman");
    e.set("first_name", "John");
    e.set("last_name", "Doe");
    e.saveIt();

    User e1 = new User();
    e1.set("id", 2);
    e1.set("nickname", "Superman2");
    e1.set("first_name", "John2");
    e1.set("last_name", "Doe2");
    e1.saveIt();

    List<User> users = User.findAll();

    for (User user : users) {
        System.out.println(user.get("id") + " " + user.get("nickname") + " " + user.get("first_name") + " " + user.get("last_name"));
    }

但没有输出,并且DB中没有信息。 这是我的FlyWay迁移:

CREATE TABLE users (
    id INT NOT NULL,
    nickname VARCHAR(100) NOT NULL,
    first_name VARCHAR(100) NOT NULL,
    last_name VARCHAR(100) NOT NULL
);

怎么了?

ADD :设置了Log4J,收到了消息:

0    [main] INFO  hsqldb.db.HSQLDB3F3E800675.ENGINE  - checkpointClose start
35   [main] INFO  hsqldb.db.HSQLDB3F3E800675.ENGINE  - checkpointClose end

更新:这是日志输出

0    [main] INFO  hsqldb.db.HSQLDB3F42F00869.ENGINE  - checkpointClose start
37   [main] INFO  hsqldb.db.HSQLDB3F42F00869.ENGINE  - checkpointClose end
68   [main] INFO  org.javalite.activejdbc.ConnectionsAccess  - Attaching connection: org.hsqldb.jdbc.JDBCConnection@41a78257
68   [main] INFO  org.javalite.activejdbc.ConnectionsAccess  - Opened connection:org.hsqldb.jdbc.JDBCConnection@41a78257 named: default on thread: Thread[main,5,main]
97   [main] INFO  org.javalite.activejdbc.Configuration  - Load models from: jar:file:/Users/dmitrysavchenko/projects/hive-queen/target/queen-jar-with-dependencies.jar!/activejdbc_models.properties
116  [main] INFO  org.javalite.activejdbc.Registry  - Registered model: class org.hive.models.User
285  [main] INFO  org.javalite.activejdbc.Registry  - Fetched metadata for table: users
302  [main] INFO  org.javalite.activejdbc.DB  - Query: "UPDATE users SET first_name= ?, nickname= ?, last_name= ? where id = ?", with parameters: <John>, <Superman>, <Doe>, <1>, took: 11 milliseconds
305  [main] INFO  org.javalite.activejdbc.DB  - Query: "UPDATE users SET first_name= ?, nickname= ?, last_name= ? where id = ?", with parameters: <John2>, <Superman2>, <Doe2>, <2>, took: 0 milliseconds
315  [main] INFO  org.javalite.activejdbc.LazyList  - Query: "SELECT * FROM users", took: 4 milliseconds

看起来执行了错误的操作。怎么了?

1 个答案:

答案 0 :(得分:2)

我的问题出在ID属性上。默认的ActiveJDBC行为如下:如果ID属性为null,则创建记录,如果有任何值,则更新它。在我的情况下,我刚刚使用了以下代码:

User e = new User();
e.set("id", 1);
e.set("nickname", "Superman");
e.set("first_name", "John");
e.set("last_name", "Doe");
e.insert();

重要! insert旧版本不可用,因此请使用1.4.7或更高版本