Hibernate JPA未创建数据库模式的脚本。

huangapple go评论73阅读模式
英文:

Hibernate JPA not creating script for database schema

问题

我已配置jakarta.persistence.schema-generation.scripts.create-target属性以生成脚本到一个文件名中。但是它确实没有被创建,因为我在整个家目录(在MacOS上)使用find命令搜索文件,但找不到它。

我尝试过foo.sql./foo.sql/tmp/foo.sql,但无论是在家目录还是/tmp中都找不到。

数据库表确实被创建,输出中也没有看到任何错误。

英文:

I have configured the jakarta.persistence.schema-generation.scripts.create-target property to generate a script into a file name. But it is surely not being created, as I searched my entire home directory (on MacOS) for the file using the find command and it is not being found.

I have tried foo.sql, ./foo.sql and /tmp/foo.sql and cannot find it anywhere in my home directory or /tmp

The database tables are indeed being created and I don't see any errors in the output.

Here is my full simple code:

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
    <persistence-unit name="foo">
        <class>Foo</class>
        <properties>
            <property name="jakarta.persistence.jdbc.url" value="jdbc:postgresql:///main" />
            <property name="jakarta.persistence.jdbc.user" value="main" />
            <property name="jakarta.persistence.jdbc.password" value="big1704cloudmain" />
            <property name="hibernate.default_schema" value="foo" />
            <property name="jakarta.persistence.schema-generation.database.action" value="validate" />
            <property name="jakarta.persistence.schema-generation.scripts.create-target" value="foo.sql" />
        </properties>
    </persistence-unit>
</persistence>

Main.kt

import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Persistence

@Entity
data class Foo (@Id val n: Int)

fun main() {
    Persistence.createEntityManagerFactory("foo")
    println("Hello World!")
}

build.gradle.kts

plugins {
    kotlin("jvm") version "latest.release"
    application
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.postgresql:postgresql:latest.release")
    implementation("org.hibernate:hibernate-core:latest.release")
}

application {
    mainClass.set("MainKt")
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(18))
    }
}

Run Output

3:15:36 PM: Executing 'run'...

> Task :compileKotlin UP-TO-DATE
> Task :compileJava NO-SOURCE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE

> Task :run
Hello World!

BUILD SUCCESSFUL in 737ms
3 actionable tasks: 1 executed, 2 up-to-date
May 13, 2023 3:15:36 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [name: foo]
May 13, 2023 3:15:36 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.2.2.Final
May 13, 2023 3:15:36 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000406: Using bytecode reflection optimizer
May 13, 2023 3:15:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using built-in connection pool (not intended for production use)
May 13, 2023 3:15:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: Loaded JDBC driver class: org.postgresql.Driver
May 13, 2023 3:15:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001012: Connecting with JDBC URL [jdbc:postgresql:///main]
May 13, 2023 3:15:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=main}
May 13, 2023 3:15:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
May 13, 2023 3:15:36 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 20 (min=1)
May 13, 2023 3:15:36 PM org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl logSelectedDialect
INFO: HHH035001: Using dialect: org.hibernate.dialect.PostgreSQLDialect, version: org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$DialectResolutionInfoImpl@72ade7e3
May 13, 2023 3:15:36 PM org.hibernate.bytecode.internal.BytecodeProviderInitiator buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : bytebuddy
May 13, 2023 3:15:36 PM org.hibernate.metamodel.internal.EntityInstantiatorPojoStandard resolveConstructor
INFO: HHH000182: No default (no-argument) constructor for class: Foo (class must be instantiated by Interceptor)
May 13, 2023 3:15:36 PM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
May 13, 2023 3:15:36 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@7ff35a3f] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
3:15:36 PM: Execution finished 'run'.

答案1

得分: 1

根据使用Spring Data JPA生成数据库架构,您需要在持久性单元属性中包含以下内容:

spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create

如果在添加并使用上述内容后仍然找不到生成的脚本,请尝试将create-target的路径名设置为绝对路径。

英文:

According to Generate Database Schema with Spring Data JPA, you need to include this:

 spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create

in the persistence unit properties.

If you still can't find the generated script after adding and using the above, try making the create-target pathname an absolute pathname.

huangapple
  • 本文由 发表于 2023年5月13日 16:18:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定