2

I'm trying to use ActiveJDBC with 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"));
    }

but having no output, and there are no information in DB. Here is my FlyWay migration:

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

What's wrong?

ADD: Setted up Log4J, got messages:

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

UPDATE: here is log output

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

Looks like a wrong operation performed. What's wrong?

4

1 回答 1

2

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

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 或更高版本

于 2013-06-14T14:27:33.053 回答