gpt4 book ai didi

java - Hibernate 注释扫描在 Gradle 5.4 下不起作用

转载 作者:行者123 更新时间:2023-11-30 05:40:01 24 4
gpt4 key购买 nike

我正忙着将 Maven 构建转换为 Gradle 构建,我一切都在运行,除了现在每当我尝试通过 Hibernate 对我尝试查询的所有实体运行 hql 查询(通过 Maven 运行)时,我都会看到此错误仍然有效,pom 文件仍在项目中):

org.hibernate.hql.internal.ast.QuerySyntaxException: Account is not mapped [SELECT o FROM Account AS o WHERE lower(o.email)=:email]

我在 Maven 下有以下依赖项:

    <dependencies>

...

<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
<exclusion>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>${hibernate.version}</version>
</dependency>

...

</dependencies>

在 Gradle (Kotlin DSL) 下,它看起来很相似(尽管更短):

dependencies {

...

// hibernate
val hibernateVersion = "5.2.13.Final"
implementation(group = "org.hibernate", name = "hibernate-core", version = hibernateVersion)
implementation(group = "org.hibernate", name = "hibernate-entitymanager", version = hibernateVersion).
exclude(group = "cglib", module = "cglib").
exclude(group = "dom4j", module = "dom4j")
implementation(group = "org.hibernate", name = "hibernate-validator", version = hibernateVersion)
implementation(group = "org.hibernate", name = "hibernate-c3p0", version = hibernateVersion)
implementation(group = "org.hibernate", name = "hibernate-java8", version = hibernateVersion)

...

}

Persistence.xml 非常标准:

<persistence-unit name="default">
<description>Persistence XML</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>

<!-- Hibernate Config -->
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect" />
<property name="hibernate.generate_statistics" value="false" />

<property name="hibernate.hbm2ddl.auto" value="validate"/>

<property name="hibernate.physical_naming_strategy" value="com.application.util.CustomNamingStrategy"/>

<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.use_sql_comments" value="false"/>

<!-- JDBC Config -->
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />

<property name="javax.persistence.jdbc.time_zone" value="UTC" />
<property name="hibernate.jdbc.time_zone" value="UTC"/>

<!-- Connection Pool -->
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />
<property name="hibernate.c3p0.max_size" value="5" />
<property name="hibernate.c3p0.min_size" value="1" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="300" />
<property name="hibernate.c3p0.max_statements" value="0" />
<property name="hibernate.c3p0.timeout" value="100" />

<!-- Batch writing -->
<property name="hibernate.jdbc.batch_size" value = "50"/>
<!-- if this is switched on fixtures does not want to run for some or other reason -->
<!--<property name="hibernate.order_inserts" value = "true"/>-->
<property name="hibernate.order_updates" value = "true"/>
<property name="hibernate.jdbc.batch_versioned_data" value = "true"/>

</properties>
</persistence-unit>

Maven使用标准的shade插件:

            <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>ApplicationApi</Main-Class>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>junit:junit</exclude>
<exclude>rebel.xml</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
</excludes>
</filter>
</filters>
<artifactSet/>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>standalone</shadedClassifierName>
<outputFile>${project.build.directory}/api.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>

而 Gradle 使用 ShadowJar 插件:

plugins {
...
id("com.github.johnrengelman.shadow").version("5.0.0")
...
}

虽然从 IntelliJ 运行不使用阴影/阴影震动来运行,所以即使我删除阴影/阴影震动,它仍然表现出相同的行为。

我注意到的是构建后的包结构,maven 拥有 classes 下的所有内容。而 Gradle 将类分为 java/kotlin

Maven:

enter image description here

Gradle :

enter image description here

不确定上述结构对注释扫描是否有影响。

我尝试添加 <property name="packagesToScan" value="....." />在 persistence.xml 中,对于每个受影响的实体没有任何影响。

实体都已正确注释(正如我提到的,这在 Maven 下仍然可以正确运行)

@Entity
@Table
@PersistenceContext(unitName = "default")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@BatchSize(size = 50)
class Account(
email: String,
...

知道为什么我在切换到 Gradle 后会收到此错误吗?

org.hibernate.hql.internal.ast.QuerySyntaxException: Account is not mapped [SELECT o FROM Account AS o WHERE lower(o.email)=:email]
at org.hibernate.internal.ExceptionConverterImpl.convert (ExceptionConverterImpl.java:133)
at org.hibernate.internal.ExceptionConverterImpl.convert (ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert (ExceptionConverterImpl.java:164)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery (AbstractSharedSessionContract.java:670)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery (AbstractSharedSessionContract.java:686)
at org.hibernate.internal.AbstractSessionImpl.createQuery (AbstractSessionImpl.java:23)

最佳答案

您是否尝试过在 build.gradle 上添加以下内容?

sourceSets {
java {
main {
output.resourcesDir = java.outputDir
}
}
}

引用 Java - What is gradle missing to map hibernate? 的博文.

And since Gradle doesn't store the "compiled" resources in the same output directory as the compiled classes, Hibernate doesn't find the mapped entities.

这就是为什么需要添加上述配置的原因。而且从 Gradle 5.x 开始,很多博客文章提到的 sourceSets.main.output.classesDir 的配置也被撤回了。因此,您需要像上面一样使用 sourceSets.main.java.outputDir

另请参阅:SourceSetOutput - Gradle DSL 版本 5.5 https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSetOutput.html

关于java - Hibernate 注释扫描在 Gradle 5.4 下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55843888/

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