gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:21:11 25 4
gpt4 key购买 nike

我正在尝试将 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"));
}

但是没有输出,数据库中也没有信息。这是我的 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

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

最佳答案

我的问题出在 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 或更高版本

关于java - 无法使用 ActiveJDBC 将对象插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17093188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com