3

我是 ORM 的忠实拥护者,尤其是当涉及到带有实体框架的 .NET 时,并且使用 LINQ 可以让您的数据访问变得不那么乏味,而且更有趣。

但是,我目前正在研究 Java,尤其是在研究 ORM。我已经针对我们的 SQL Server 数据库尝试了 nHybernate——由于某些表上没有键(唯一约束),我开始研究 JOOQ。

然而,我已经设法从 SQL Server 数据库生成了我的 Java 类——并非没有错误——使用 JOOQ (v3.2.0)。

根据this page JOOQ Connections,它提到了一个类org.jooq.util.sqlserver.SQLServerDatabase。但是,当我在配置中使用它生成时,我得到类未找到异常并且没有生成任何内容。我不得不使用 org.jooq.util.jdbc.JDBCDatabase 来生成我的代码。

这是我的配置文件(myjooqdbconfig.xml):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.2.0.xsd">
  <jdbc>
    <driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver>
    <url>jdbc:sqlserver://MYSERVER:1433;DatabaseName=MYDB</url>
    <user>MYUSER</user>
    <password>PWD</password>
  </jdbc>

  <generator>
    <name>org.jooq.util.DefaultGenerator</name>

    <database>
      <name>org.jooq.util.jdbc.JDBCDatabase</name>

      <inputSchema>dbo</inputSchema>

      <includes>.*</includes>

      <excludes></excludes>
    </database>

    <target>
      <packageName>com.quorum.sentinel.dataAccess</packageName>

      <directory>F:\Libraries\jooq\jOOQ-3.2.0\lib\gen</directory>
    </target>
  </generator>
</configuration>

由于类未找到异常,我使用 Java Decompiler 查看了 JAR 文件 jooq-meta-3.2.0.jar,这些类的位置似乎是这些类。我看不到 org.jooq.util.sqlserver.SQLServerDatabase 类定义。

JOOQ 反编译

我正在编译:

java -classpath jooq-3.2.0.jar;jooq-meta-3.2.0.jar;jooq-codegen-3.2.0.jar;sqljdbc4.jar;. org.jooq.util.GenerationTool /myjooqdbconfig.xml

这个 org.jooq.util.sqlserver.SQLServerDatabase 定义是否存在于任何 JAR 中?

此外,从上述配置生成代码后,它似乎需要很长时间并且似乎处于某种循环中。

查看 dbo - Keys/Tables/Dbo 下生成的文件,当我将文件复制到 Netbeans 项目中时,我注意到我的表有重复的名称(很多错误)。我删除了重复项,到目前为止一切顺利,我可以在下面的代码中从数据库中读取各种表:

public static void main(String[] args) {
    Connection conn = null;

    String userName = "MYUSER";
    String password = "PWD";
    String url = "jdbc:sqlserver://MYSERVER:1433;DatabaseName=MYDB";

    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        conn = DriverManager.getConnection(url, userName, password);

        DSLContext create = DSL.using(conn);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(new Date( new GregorianCalendar().getTimeInMillis())));
        SelectJoinStep<Record> from = create.select().from(Configuration.CONFIGURATION);
        Result<Record> result = from.fetch();
        System.out.println(simpleDateFormat.format(new Date( new GregorianCalendar().getTimeInMillis())));
        for (Record r : result) {
            Object id =  r.getValue(Configuration.CONFIGURATION.NAME);
            Object time = r.getValue(Configuration.CONFIGURATION.VALUE);
            System.out.println("Record:              : ID: " + id + " Time: " + time);
        }

        System.out.println(simpleDateFormat.format(new Date( new GregorianCalendar().getTimeInMillis())));

    } catch (Exception e) {
        // For the sake of this tutorial, let's keep exception handling simple
        e.printStackTrace();
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
        }
    }
}

所以只是在混乱中重申我的问题:

  1. SQL Server 特定类 org.jooq.util.sqlserver.SQLServerDatabase 在哪里?
  2. 以上信息是您通常如何生成 SQL Server 对象映射的吗?
  3. 鉴于上述内容几乎正确生成,并且在 Keys/Tables/Dbo .java 文件中生成了重复的定义,我不想立即说这是 Jooq 的错误 - 但可能是错误的数据库设计/不正确的配置/愚蠢的开发者!
4

2 回答 2

7

在 jOOQ 3.2 中,jOOQ 已成为双重许可的. SQL Server 集成仅适用于jOOQ 专业版许可证。但是,您可以下载适用于 SQL Server 的 30 天免费试用版。

请注意, jOOQ 3.2.1(即将发布)还有一个修复程序,为社区版用户提供更多信息,而不仅仅是堆栈跟踪。

另请参阅:jooq-3.2.0 和 db2 数据库

于 2013-11-01T14:33:11.723 回答
2

我想分享我的 sql server 2012 和 2014 工作配置,经过多次尝试后,它们都可以工作。我下载了 jooq3.7.2 企业版使其工作。

这是我的示例配置文件“library.xml”

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.7.0.xsd">
  <!-- Configure the database connection here -->
  <jdbc>
    <driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver>
    <url>jdbc:sqlserver://testdelta1:1433;databaseName=NEO_DB</url>
    <user>LOB_ManilaUsers</user>
    <password>lobpassword3123</password>
  </jdbc>

  <generator>
    <!-- The default code generator. You can override this one, to generate your own code style.
         Supported generators:
         - org.jooq.util.JavaGenerator
         - org.jooq.util.ScalaGenerator
         Defaults to org.jooq.util.JavaGenerator -->
    <name>org.jooq.util.JavaGenerator</name>

    <database>
      <!-- The database type. The format here is:
           org.util.[database].[database]Database -->
      <!--<name>org.jooq.util.mysql.MySQLDatabase</name> -->
      <name>org.jooq.util.sqlserver.SQLServerDatabase</name>
      <!-- The database schema (or in the absence of schema support, in your RDBMS this
           can be the owner, user, database name) to be generated -->

      <!-- specify database name -->
      <inputSchema>dbo</inputSchema>

      <!-- All elements that are generated from your schema
           (A Java regular expression. Use the pipe to separate several expressions)
           Watch out for case-sensitivity. Depending on your database, this might be important! -->
      <includes>.*</includes>

      <!-- All elements that are excluded from your schema
           (A Java regular expression. Use the pipe to separate several expressions).
           Excludes match before includes -->
      <excludes></excludes>
    </database>

    <target>
      <!-- The destination package of your generated classes (within the destination directory) -->
      <packageName>com.neoapi.entity</packageName>

      <!-- The destination directory of your generated classes -->
      <directory>jooq_directory/src_generated</directory>
    </target>
  </generator>
</configuration>

通常默认模式值为“dbo”。起初,我输入了不起作用的“NEO_DB”。

你可以指定你想要的和

免费版或开源 jooq 3.7.2 不包含数据库名称 SQLServerDatabase。企业版(30天试用)有效。我不确定试用天数是如何开始计算的。

这是我终端的示例日志:

> INFO: Generating table         : Tobservationrawdataobservations.java [input=tObservationRawDataObservations, output=tObservationRawDataObservations, pk=PK_ObservationRawDataObservations]
Feb 16, 2016 7:03:35 AM org.jooq.tools.JooqLogger info

INFO: Generating table         : UdvPublisheddata.java [input=udv_PublishedData, output=udv_PublishedData, pk=N/A]
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating table         : UdvPublishinghistory.java [input=udv_PublishingHistory, output=udv_PublishingHistory, pk=N/A]
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating table         : UdvScreamertanksystems.java [input=udv_ScreamerTankSystems, output=udv_ScreamerTankSystems, pk=N/A]
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating table         : UdvSiradwmconfig.java [input=udv_SiraDWMConfig, output=udv_SiraDWMConfig, pk=N/A]
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating table         : Vwauditlog.java [input=vwAuditLog, output=vwAuditLog, pk=N/A]
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating table         : Vwcompanysitetanksystem.java [input=vwCompanySiteTankSystem, output=vwCompanySiteTankSystem, pk=N/A]
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating table         : Vwdocketdeliveries.java [input=vwDocketDeliveries, output=vwDocketDeliveries, pk=N/A]
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Tables generated         : Total: 01:06
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating table references
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Table refs generated     : Total: 01:06, +17.847ms
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating Keys          
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Keys generated           : Total: 01:06, +49.833ms
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating table records 
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : _Tmptsis_20151019Record.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : DataRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : FuncgetchasedatatableRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : FuncgetcompliancestatisticspasstableRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : FuncgetcurrentfailsinctableRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcalibrationsRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcompaniesRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcompanyfilereadersRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcompanyrolecontactsRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcompanyrolepermissionsRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcompanyrolesRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcontactregionsRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcontactsRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcontactsitesRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcorrectiongroupRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TcorrectiongroupsiteRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TdomainusersRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TfilereadersRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TgradealiasesRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TgradesRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TimportedfilemessagesRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating record        : TimportedfilesRecord.java
Feb 16, 2016 7:03:40 AM org.jooq.tools.JooqLogger info
INFO: Generating routine       : SpCreatediagram.java
Feb 16, 2016 7:03:43 AM org.jooq.tools.JooqLogger info
INFO: Generating routine       : SpDropdiagram.java
Feb 16, 2016 7:03:45 AM org.jooq.tools.JooqLogger info
INFO: Generating routine       : SpHelpdiagramdefinition.java
Feb 16, 2016 7:03:45 AM org.jooq.tools.JooqLogger info
INFO: Generating routine       : SpHelpdiagrams.java
Feb 16, 2016 7:03:46 AM org.jooq.tools.JooqLogger info
INFO: Generating routine       : SpRenamediagram.java
Feb 16, 2016 7:03:46 AM org.jooq.tools.JooqLogger info
INFO: Routines generated       : Total: 01:11, +5.132s
Feb 16, 2016 7:03:46 AM org.jooq.tools.JooqLogger info
INFO: Packages fetched         : 0 (0 included, 0 excluded)
Feb 16, 2016 7:03:46 AM org.jooq.tools.JooqLogger info
INFO: Removing excess files    
Feb 16, 2016 7:03:46 AM org.jooq.tools.JooqLogger info
INFO: GENERATION FINISHED: dbo : Total: 01:11, +2.713ms
于 2016-02-15T23:22:25.523 回答