当前位置: 首页 > 知识库问答 >
问题:

在jooq中使用DAO时发生DataAccessException

越胤
2023-03-14

我想使用DAO将生成的Pojo添加到数据库中。但我收到一个异常,不知道如何解决它。

我尝试添加一个公共。队伍在每一张桌子前面。

在DAO使用中插入部队:

TroopDao dao = new TroopDao(new DefaultConfiguration().set(SQLDialect.SQLITE).set(database.getConnection()));
Troop initial = new Troop(1, 100, 120, 120.2, 11.2, 12.0, 13.0, 6245.0, 1534.0, 1364.0, 121235.3, 125.3, 51.3);
dao.insert(initial);

使用的数据库文件

CREATE TABLE IF NOT EXISTS TROOP(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    current_health INTEGER NOT NULL,
    max_health INTEGER NOT NULL,
    pos_x DOUBLE NOT NULL,
    pos_y DOUBLE NOT NULL,
    normal_speed DOUBLE NOT NULL,
    street_speed DOUBLE NOT NULL,
    difficult_terrain_speed DOUBLE NOT NULL,
    close_combat_range DOUBLE NOT NULL,
    ranged_combat_range DOUBLE NOT NULL,
    normal_view_distance DOUBLE NOT NULL,
    disadvantaged_view_distance DOUBLE NOT NULL,
    advantaged_view_distance DOUBLE NOT NULL
);

CREATE TABLE IF NOT EXISTS ARMY(
    id INTEGER,
    hq INTEGER,
    troop INTEGER,
    FOREIGN KEY(hq) REFERENCES TROOP(id),
    FOREIGN KEY(troop) REFERENCES TROOP(id),
    UNIQUE(hq, troop),
    PRIMARY KEY (id, hq, troop)
);

JOOQ一代,我还尝试了unqualifiedSchema=main:

        Configuration configuration = new Configuration()
                .withGenerator(new Generator()
                        .withDatabase(new Database()
                                .withName("org.jooq.meta.extensions.ddl.DDLDatabase")
                                .withIncludes("ARMY | TROOP")
                                .withOutputSchemaToDefault(Boolean.TRUE)
                                .withProperties(new Property()
                                                .withKey("unqualifiedSchema")
                                                .withValue("none"),
                                        new Property()
                                                .withKey("scripts")
                                                .withValue("/db/schema.sql")))
                        .withGenerate(new Generate()
                                .withPojos(Boolean.TRUE)
                                .withDeprecationOnUnknownTypes(Boolean.FALSE)
                                .withImmutableInterfaces(Boolean.TRUE)
                                .withDaos(Boolean.TRUE))
                        .withTarget(new Target()
                                .withPackageName("me.leslie.generals.server.persistence.jooq")
                                .withDirectory("Generals-Server/src/main/java")));

        GenerationTool.generate(configuration);

数据库类只是一个返回基本数据的单例:

@EqualsAndHashCode
@ToString
@Getter
public class Database {
    public static final String DEFAULT_DB_URL = "jdbc:sqlite:";
    @NonNull
    private final String url;
    private final Connection connection;

    private static Database instance;

    public static Database get() {
        if(instance == null){
            instance = new Database(DEFAULT_DB_URL);
        }
        return instance;
    }

    private Database(String url) {
        try {
            this.connection = DriverManager.getConnection(url);
        } catch (SQLException e) {
            throw new InitializationException("Could not get Database connection", e);
        }
        initialize();
        this.url = url;
    }


    private void initialize() {
        final String troopSchema = "CREATE TABLE IF NOT EXISTS TROOP(" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "current_health INTEGER NOT NULL," +
                "max_health INTEGER NOT NULL," +
                "pos_x DOUBLE NOT NULL," +
                "pos_y DOUBLE NOT NULL," +
                "normal_speed DOUBLE NOT NULL," +
                "street_speed DOUBLE NOT NULL," +
                "difficult_terrain_speed DOUBLE NOT NULL," +
                "close_combat_range DOUBLE NOT NULL," +
                "ranged_combat_range DOUBLE NOT NULL," +
                "normal_view_distance DOUBLE NOT NULL," +
                "disadvantaged_view_distance DOUBLE NOT NULL," +
                "advantaged_view_distance DOUBLE NOT NULL" +
                ");";


        final String armySchema = "CREATE TABLE IF NOT EXISTS ARMY(" +
                "id INTEGER," +
                "hq INTEGER," +
                "troop INTEGER," +
                "FOREIGN KEY(hq) REFERENCES TROOP(id)," +
                "FOREIGN KEY(troop) REFERENCES TROOP(id)," +
                "UNIQUE(hq, troop)," +
                "PRIMARY KEY (id, hq, troop)" +
                ");";

        try (PreparedStatement sql = connection.prepareStatement(troopSchema)) {
            sql.execute();


        } catch (SQLException e) {
            throw new InitializationException("Could not initialize Troops", e);
        }

        try (PreparedStatement sql = connection.prepareStatement(armySchema)) {
            sql.execute();
        } catch (SQLException e) {
            throw new InitializationException("Could not initialize Armies", e);
        }
    }
}

错误,当我使用基于文件的数据库并访问它时,所需的表似乎在那里:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jooq.tools.reflect.Reflect (file:/home/leslie/.m2/repository/org/jooq/jooq/3.11.11/jooq-3.11.11.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class)
WARNING: Please consider reporting this to the maintainers of org.jooq.tools.reflect.Reflect
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
23:32:56.084 [main] INFO org.jooq.Constants - 

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@  @@        @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@        @@@@@@@@@@
@@@@@@@@@@@@@@@@  @@  @@    @@@@@@@@@@
@@@@@@@@@@  @@@@  @@  @@    @@@@@@@@@@
@@@@@@@@@@        @@        @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@        @@        @@@@@@@@@@
@@@@@@@@@@    @@  @@  @@@@  @@@@@@@@@@
@@@@@@@@@@    @@  @@  @@@@  @@@@@@@@@@
@@@@@@@@@@        @@  @  @  @@@@@@@@@@
@@@@@@@@@@        @@        @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@  @@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  Thank you for using jOOQ 3.11.11

23:32:56.093 [main] DEBUG org.jooq.tools.LoggerListener - Executing query          : select PUBLIC.TROOP.ID from PUBLIC.TROOP
23:32:56.099 [main] DEBUG org.jooq.tools.LoggerListener - Exception                
org.jooq.exception.DataAccessException: SQL [select PUBLIC.TROOP.ID from PUBLIC.TROOP]; [SQLITE_ERROR] SQL error or missing database (no such table: PUBLIC.TROOP)
    at org.jooq_3.11.11.SQLITE.debug(Unknown Source)
    at org.jooq.impl.Tools.translate(Tools.java:2430)
    at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:832)
    at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:364)
    at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:323)
    at org.jooq.impl.SelectImpl.fetch(SelectImpl.java:2700)
    at me.leslie.generals.server.repository.TroopJooqRepository.nextID(TroopJooqRepository.java:47)
    at me.leslie.generals.server.repository.TroopJooqRepository.create(TroopJooqRepository.java:60)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
    at org.jooq.lambda.SeqImpl.collect(SeqImpl.java:190)
    at me.leslie.generals.server.repository.TroopJooqRepositoryTest.initializeTroops(TroopJooqRepositoryTest.java:38)
    at me.leslie.generals.server.repository.TroopJooqRepositoryTest.getSomeTroops(TroopJooqRepositoryTest.java:152)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
    at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
    at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)
    at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)
    at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
    at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
    at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: PUBLIC.TROOP)
    at org.sqlite.core.DB.newSQLException(DB.java:941)
    at org.sqlite.core.DB.newSQLException(DB.java:953)
    at org.sqlite.core.DB.throwex(DB.java:918)
    at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
    at org.sqlite.core.NativeDB.prepare(NativeDB.java:134)
    at org.sqlite.core.DB.prepare(DB.java:257)
    at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:47)
    at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
    at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:19)
    at org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:35)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:241)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:205)
    at org.jooq.impl.ProviderEnabledConnection.prepareStatement(ProviderEnabledConnection.java:109)
    at org.jooq.impl.SettingsEnabledConnection.prepareStatement(SettingsEnabledConnection.java:73)
    at org.jooq.impl.AbstractResultQuery.prepare(AbstractResultQuery.java:239)
    at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:322)
    ... 77 common frames omitted

共有1个答案

严峰
2023-03-14

根据jOOQ 3.12,DDLDatabase解析您的SQL脚本,将其转换为H2,并在内存H2数据库中运行它们,该数据库使用公共模式作为默认模式,从代码生成器的角度来看,您的所有表现在都位于该模式中。

SQLite中不存在这样的模式,这就是为什么会出现所看到的错误。jOOQ 3.12通过支持一个新的unqualifiedSchema属性来解决这个问题,该属性允许指定DDL脚本中所有非限定表的隐含模式。默认情况下,假设这映射到H2中的公共模式,则假设公共模式为“默认模式”。因此,要解决这个问题,只需升级到jOOQ 3.12。请参阅此处的更多详细信息:https://www.jooq.org/doc/latest/manual/code-generation/codegen-ddl

注意:您已经在使用这个标志,但没有使用jOOQ 3.12。它在3.11.11中还不可用。

 类似资料:
  • 我的项目需要我自己生成DAO和POJO。必须对其进行某些修改。我的SRS说我必须使用jOOQ。我第一次使用它;我知道jOOQ自己生成POJO和DAO,但我找不到禁用它的方法。 我在jOOQ的文档中找不到任何对我有帮助的东西。有人能告诉我如何从DAO和POJO代禁用jOOQ,但仍然提供记录吗。 Maven pom。xml文件如下: 而且构建使用了gradle。我也不知道这会对更改产生什么影响。

  • 我试图自动生成刀的视图。我已经预先设置了配置: whitch为表生成dao,但不为视图生成dao。根据文件规定: 如果您使用的是jOOQ的代码生成器,那么可以将其配置为为为您生成POJO和DAO。然后,jOOQ为每个UpdateRecord生成一个DAO,即为每个表生成一个单列主键。 和 公共接口 它表示来自表或视图的记录-表记录其基础表或视图具有“主唯一键”,即主键或至少一个唯一键jOOQ使用“

  • 我有一个表,有两列(都是文本类型),其中一列是主键。另一个不为空。我将jooq与以下生成部分一起使用: 生成的DAO不包含插入方法。我也没有看到任何警告。我正在使用。

  • MySQL不接受此处生成的datetime值。我探索并发现jOOQ转换器可以用于自定义转换。我可以找到如何在获取数据时使用转换的示例,但无法弄清楚如何在查询时使用转换器。如何使用jOOQ转换器生成SQL而不生成代码?或者是否有更好的方法为SQL生成此查询。

  • 问题内容: 我正在使用:Eclipse Java EE IDE Web开发人员版本:靛蓝发行 使用hibernate工具,我是第一次在Eclipse中hibernate,因此我学习了如何配置hibernate并生成带有注释的POJO(我认为它比.xml更好)。 因此,在生成我的POJO和DAO之后,我尝试进行插入,但是对我的实体管理器启动了“空点异常”,这就是hibernate工具生成dao类的方

  • 要用jooq创建一张唱片,我说 这将引发一个id为null的约束冲突异常。如果我设置一个id(如 我没有得到异常,但postgres不会自动生成值。 http://www.postgresql.org/docs/current/static/datatype-numeric.html#datatype-serial的postgres文档指出“在INSERT中省略SERIAL列,或者指定DEFAUL