gpt4 book ai didi

java - 我应该如何使用 Hibernate(JPA) 和 Gradle 构建我的 Java 代码?

转载 作者:行者123 更新时间:2023-12-02 11:00:42 26 4
gpt4 key购买 nike

我对 Hibernate(JPA) 非常陌生。

以下是相关版本信息:Java 8、Gradle 4.0、Hibernate 5.3.2.Final

我的单元测试(JUnit)在使用 Gradle 构建时失败了。

我有一些测试代码,在 Eclipse 上运行时肯定可以通过,但使用 Gradle 运行时失败(./gradlew build)。

我在 SO 上发现了以下 QA,我可以使用此解决方案通过 Gradle 进行测试,但会显示警告消息。

java - Hibernate with Tomcat and gradle - Stack Overflow

此答案建议将以下设置添加到 build.gradle。

sourceSets.all {
output.resourcesDir = output.classesDir
}

警告消息如下。

> Configure project :
Gradle now uses separate output directories for each JVM language, but this build assumes a single directory for all classes from a source set. This behaviour has been deprecated and is scheduled to be removed in Gradle 5.0

如您所见,当 Gradle 升级到 5.0 时,此解决方法将失败。

所以,我想问你我应该如何使用 Hibernate 和 Gradle 构建我的代码。

根据一些SO的QA,我尝试添加以下依赖项,但它不起作用。

compile group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api', version: '1.0.2.Final'

另外,我发现了 Hibernate Gradle Plugin,所以我将其添加到依赖项中,但它也不起作用。

compile group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: '5.3.2.Final'

Maven Repository: org.hibernate » hibernate-gradle-plugin » 5.3.2.Final

这是我的目录结构的一部分。

.
├── README.md
├── build.gradle
└── src
└── main
   ├── java
   │   └── com
   │   └── xxxxx
   │   └── xxxxx
   │   └── api
│      ├── dao
│      │   └── JpaDao.class
   │   └── dto
   │      └── Tasks.java
   ├── resources
   │   ├── META-INF
   │   │   └── persistence.xml
   │   └── hibernate.properties
   └── webapp
   ├── index.html
   └── v1-docs
   └── index.html

我的 build.gradle 文件的一部分如下:

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'war'
apply plugin: 'checkstyle'

def defaultEncoding = 'UTF-8'
tasks.withType(AbstractCompile).each {
it.options.encoding = defaultEncoding
}
tasks.withType(GroovyCompile).each {
it.groovyOptions.encoding = defaultEncoding
}
[compileJava, compileTestJava].each {
it.options.compilerArgs += ['-source', '1.8', '-target', '1.8']
}

repositories {
jcenter()
}

dependencies {
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.2.Final'
testCompile group: 'junit', name: 'junit', version: '4.12'
}

sourceSets.all {
output.resourcesDir = output.classesDir
}

我的实体,即任务类(该类名称显示在我的错误输出中)如下:

@Entity
public class Tasks {

@Id
public UUID id;

@Column(name = "is_finished")
public boolean isFinished;

@Column(name = "updated_at")
public Timestamp updatedAt;

@Column(name = "user_id")
public String userId;
}

我的JPA调用代码如下:

EntityManager em = entityManagerFactory.createEntityManager();
TypedQuery<Tasks> query = em
.createQuery("SELECT task FROM Tasks task WHERE task.userId = :userId", Tasks.class)
.setParameter("userId", userId);
tasksDtoList = query.getResultList();
em.close();

错误输出如下(有点长,但我不确定要解决的重要线索在哪里):

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Tasks is not mapped [SELECT task FROM Tasks task WHERE task.userId = :userId]
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:715)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:731)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
at com.soybs.taskrtomato.api.dao.JpaDao.getTaskList(JpaDao.java:54)
at com.soybs.taskrtomato.api.service.UserServiceTest.test(UserServiceTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy1.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:146)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Tasks is not mapped [SELECT task FROM Tasks task WHERE task.userId = :userId]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:219)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:119)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:597)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:706)
... 51 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Tasks is not mapped
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:169)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:331)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3695)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3584)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:271)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:191)
... 57 more

最佳答案

我可以使用 build.gradle 上的以下配置来解决它。

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

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

关于java - 我应该如何使用 Hibernate(JPA) 和 Gradle 构建我的 Java 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51353433/

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