当前位置: 首页 > 知识库问答 >
问题:

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

督辉
2023-03-14

我对Hibernate(JPA)很陌生。

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'
    null
compile group: 'org.hibernate', name: 'hibernate-gradle-plugin', version: '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
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;
}
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();

错误输出如下所示(位a很长,但我不确定要解决的重要线索在哪里):

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

共有1个答案

谭刚毅
2023-03-14

我可以通过build.gradle上的以下配置来解决这个问题。

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

另见:sourcesetoutput-gradle DSL版本5.5https://docs.Gradle.org/current/DSL/org.Gradle.api.tasks.SourceSetOutput.html

 类似资料:
  • 问题内容: 我有一个我编码的应用程序,我正在对其进行重构以更好地利用Spring Integration。该应用程序处理文件的内容。 问题(如我所见)是我当前的实现通过了,而不是Spring Integration 。 为了避免进一步滚动我自己的代码(以后必须维护),我想知道在Spring Integration中是否存在用于构造Messages的推荐结构。我想知道的是,是否建议将某些建议与类似的

  • 问题内容: 这个问题在很多地方都被问到,变化不尽。例如Java-getClassLoader()。getResource()不仅使我烦恼不已。)我仍然无法使其正常工作。 这是一个代码片段: 此作品-请注意,我硬编码的路径,包含剪辑文件,该文件的目录 是 有,而且 是 在同一目录作为我的.class文件。las,注释掉的代码仅返回url的空值。 其他大多数帖子似乎都涉及getResourceAsSt

  • 问题内容: 我使用JPA,并将Hibernate作为其实现。我的项目中 需要* 具备哪些依赖关系? * 问题答案: 我相信您仅需要两件事就是hibernate的entitymanager,然后是SLF4J日志记录捆绑包之一。其他所有内容都应作为依赖项引入:

  • 问题内容: 我正在将Java库移植到C#。我使用的是Visual Studio 2008,因此没有停止使用的Microsoft Java语言转换助手程序(JLCA)。 我的方法是创建一个与Java库具有类似项目结构的新解决方案,然后将Java代码复制到ac#文件中,并将其逐行转换为有效的c#。考虑到我觉得Java易于阅读,两种语言之间的细微差别使我感到惊讶。 有些事情很容易移植(命名空间,继承等)

  • 问题内容: 我已经看到许多有关使用方法的堆栈溢出问题的答案。我还看到用户在他们的评论下说“ apply很慢,应该避免”。 我已经阅读了许多有关性能的文章,这些文章解释得很慢。我还在文档中看到了关于免除apply传递UDF的便捷功能的免责声明(现在似乎找不到)。因此,普遍的共识是,应尽可能避免。但是,这引起了以下问题: 如果apply太糟糕了,那为什么在API中呢? 我应该如何以及何时使代码免费?

  • 让我们假设我想在协调计划中定义一个圆。在C中,我可以做这样的事情: 在java中,我正在尝试以下操作... 如果上面的行不正确,请您为我提供正确的代码好吗?