Hibernate: hbm2 java не генерирует файлы - PullRequest
0 голосов
/ 09 июля 2020

Я хотел бы сгенерировать код Java для Hibernate из файла hbm. xml. Если возможно, я хотел бы сделать это без инструментов обратного проектирования и без подключения к БД.

Для генерации я использую задачу ant из инструментов гибернации. Но я вызываю его из gradle:

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
    maven { url 'https://repository.jboss.org/nexus/content/groups/public' }
}

configurations {
    codegen
}

dependencies {
    codegen group: 'org.hibernate', name: 'hibernate-tools', version: '5.4.18.Final'
    codegen group: 'org.hibernate', name: 'hibernate-core', version: '4.3.9.Final'
    codegen group: 'org.hibernate', name: 'hibernate-entitymanager', version: '4.3.9.Final'
    codegen group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.5'
    codegen group: 'log4j', name: 'log4j', version: '1.2.17'
    codegen group: 'com.oracle.database.jdbc', name: 'ojdbc10', version: '19.7.0.0'
    codegen group: 'com.h2database', name: 'h2', version: '1.4.193'
}

project.ext {
    hibernateDestDir = file("$buildDir/generated")
}

ant.lifecycleLogLevel = "VERBOSE"
task codegen {
    outputs.dir hibernateDestDir

    doLast {
        hibernateDestDir.exists() || hibernateDestDir.mkdirs()
        ant {
            taskdef(name: "hibernatetool",
                    classname: "org.hibernate.tool.ant.HibernateToolTask",
                    classpath: configurations.codegen.asPath + ":$projectDir/src/main/resources")
            hibernatetool(destdir: hibernateDestDir) {
                classpath {
                    pathelement(path: configurations.codegen.asPath)
                }
                configuration {
                    propertyfile="$projectDir/src/main/resources/hibernate.properties"
                        fileset(dir: "$projectDir/src") {
                            include { name = "**/*.hbm.xml" }
                    }
                }
                //hbm2ddl()
                //hbm2dao(destdir: "$buildDir/generated")
                hbm2java(destdir: "$buildDir/generated")
            }
        }
    }
}

Конфигурация Hibernate выглядит следующим образом (hibernate.properties):

hibernate.connection.driver_class = org.h2.Driver
hibernate.dialect = org.hibernate.dialect.H2Dialect
hibernate.connection.url = jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
hibernate.current_session_context_class = thread
hibernate.connection.zeroDateTimeBehavior = convertToNull
hibernate.show_sql = true
hibernate.generate_statistics = true
hibernate.max_fetch_depth = 3

И файл сопоставления (mapping.hbm.xml):

<hibernate-mapping>
<class name="Account" table="ACCOUNT">
    <id name="accountId">
        <column name="ACCOUNT_ID" />
        <generator class="native" />
    </id>
    <property name="description">
              <column name="DESC" />
    </property>
</class>
</hibernate-mapping>

Это моя структура проекта:

├── build.gradle
├── settings.gradle
└── src
    └── main
        ├── java
        └── resources
            ├── hibernate.properties
            ├── log4j.properties
            └── mapping.hbm.xml

Но сгенерированные файлы не создаются. Результат выполнения gradle:

> Task :codegen
parsing buildfile jar:file:/home/wolu/.gradle/wrapper/dists/gradle-6.1.1-all/cfmwm155h49vnt3hynmlrsdst/gradle-6.1.1/lib/ant-1.10.7.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/home/wolu/.gradle/wrapper/dists/gradle-6.1.1-all/cfmwm155h49vnt3hynmlrsdst/gradle-6.1.1/lib/ant-1.10.7.jar!/org/apache/tools/ant/antlib.xml from a zip file
[ant:echo] /home/wolu/lab/hibernate-example/build/generated
[ant:hibernatetool] Executing Hibernate Tool with a Standard Configuration
[ant:hibernatetool] 1. task: hbm2java (Generates a set of .java files)
[ant:hibernatetool] 08:42:38,755 DEBUG [org.jboss.logging] - Logging Provider: org.jboss.logging.Log4jLoggerProvider
[ant:hibernatetool] 08:42:38,813 DEBUG [org.hibernate.integrator.internal.IntegratorServiceImpl] - Adding Integrator [org.hibernate.cfg.beanvalidation.BeanValidationIntegrator].
[ant:hibernatetool] 08:42:38,816 DEBUG [org.hibernate.integrator.internal.IntegratorServiceImpl] - Adding Integrator [org.hibernate.secure.spi.JaccIntegrator].
[ant:hibernatetool] 08:42:38,820 DEBUG [org.hibernate.integrator.internal.IntegratorServiceImpl] - Adding Integrator [org.hibernate.cache.internal.CollectionCacheInvalidator].
[ant:hibernatetool] 08:42:38,841 INFO  [org.hibernate.Version] - HHH000412: Hibernate ORM core version 5.4.18.Final
[ant:hibernatetool] 08:42:38,844 INFO  [org.hibernate.cfg.Environment] - HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.current_session_context_class=thread, hibernate.max_fetch_depth=3, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.connection.zeroDateTimeBehavior=convertToNull, hibernate.generate_statistics=true, hibernate.connection.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1, hibernate.bytecode.use_reflection_optimizer=false, hibernate.show_sql=true}
[ant:hibernatetool] 08:42:39,013 DEBUG [org.hibernate.tool.hbm2x.POJOExporter] - org.hibernate.tool.hbm2x.POJOExporter outputdir:/home/wolu/lab/hibernate-example/build/generated path: []
[ant:hibernatetool] 08:42:39,110 DEBUG [org.hibernate.service.spi.ServiceBinding] - Overriding existing service binding [org.hibernate.secure.spi.JaccService]
[ant:hibernatetool] 08:42:39,123 DEBUG [org.hibernate.cache.internal.RegionFactoryInitiator] - Cannot default RegionFactory based on registered strategies as `[]` RegionFactory strategies were registered
[ant:hibernatetool] 08:42:39,124 DEBUG [org.hibernate.cache.internal.RegionFactoryInitiator] - Cache region factory : org.hibernate.cache.internal.NoCachingRegionFactory
[ant:hibernatetool] 08:42:39,164 INFO  [org.hibernate.annotations.common.Version] - HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
    ...
[ant:hibernatetool] 08:42:39,307 WARN  [org.hibernate.orm.connections.pooling] - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
[ant:hibernatetool] 08:42:39,308 INFO  [org.hibernate.orm.connections.pooling] - HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:mem:test;DB_CLOSE_DELAY=-1]
[ant:hibernatetool] 08:42:39,309 INFO  [org.hibernate.orm.connections.pooling] - HHH10001001: Connection properties: {zeroDateTimeBehavior=convertToNull}
[ant:hibernatetool] 08:42:39,309 INFO  [org.hibernate.orm.connections.pooling] - HHH10001003: Autocommit mode: false
[ant:hibernatetool] 08:42:39,311 DEBUG [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] - Initializing Connection pool with 1 Connections
[ant:hibernatetool] 08:42:39,312 INFO  [org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl] - HHH000115: Hibernate connection pool size: 20 (min=1)
[ant:hibernatetool] 08:42:39,454 DEBUG [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] - Database ->
[ant:hibernatetool]        name : H2
[ant:hibernatetool]     version : 1.4.193 (2016-10-31)
[ant:hibernatetool]       major : 1
[ant:hibernatetool]       minor : 4
[ant:hibernatetool] 08:42:39,454 DEBUG [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] - Driver ->
[ant:hibernatetool]        name : H2 JDBC Driver
[ant:hibernatetool]     version : 1.4.193 (2016-10-31)
[ant:hibernatetool]       major : 1
[ant:hibernatetool]       minor : 4
[ant:hibernatetool] 08:42:39,454 DEBUG [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] - JDBC version : 4.0
[ant:hibernatetool] 08:42:39,468 INFO  [org.hibernate.dialect.Dialect] - HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
[ant:hibernatetool] 08:42:39,540 DEBUG [org.hibernate.boot.model.relational.Namespace] - Created database namespace [logicalName=Name{catalog=null, schema=null}, physicalName=Name{catalog=null, schema=null}]
[ant:hibernatetool] 08:42:39,543 DEBUG [org.hibernate.type.spi.TypeConfiguration$Scope] - Scoping TypeConfiguration [org.hibernate.type.spi.TypeConfiguration@423cf5e] to MetadataBuildingContext [org.hibernate.boot.internal.MetadataBuildingContextRootImpl@7283e671]

Что мне здесь не хватает?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...