playframework - 包 javax.persistence 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20426271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:51:06  来源:igfitidea点击:

playframework - package javax.persistence does not exist

javapersistenceebeanplayframework-2.2

提问by masterdany88

I have Issue with using database I was going with this tutorial http://vimeo.com/58969923#(one from playframework.com page) in the model:

我在模型中使用数据库时遇到问题我正在使用本教程 http://vimeo.com/58969923#(来自 playframework.com 页面的一个):

play-2.2.1/jcirs/app/models/MedicalIncident.java

play-2.2.1/jcirs/app/models/MedicalIncident.java

public class MedicalIncident extends Model{}

公共类 MedicalIncident 扩展模型{}

I am trying to use Entity. For that I have to import: javax.persistence.* and play.db.ebean.* but none of them can be found. The error is

我正在尝试使用实体。为此,我必须导入: javax.persistence.* 和 play.db.ebean.* 但都找不到。错误是

package javax.persistence does not exist

包 javax.persistence 不存在

What should I do? Use any other database engine? Or should download some dependency. I would like to go with playframework best way. Please help.

我该怎么办?使用任何其他数据库引擎?或者应该下载一些依赖项。我想以最好的方式使用 playframework。请帮忙。

My configuration application.conf:

我的配置application.conf:

 db.default.driver=org.h2.Driver
 db.default.url="jdbc:h2:mem:play"
 ebean.default="models.*"

stack trace:

堆栈跟踪:

[jcirs] $ run 8081

--- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:8081

(Server started, use Ctrl+D to stop and go back to the console...)

[info] Compiling 5 Scala sources and 5 Java sources to /home/daniel/play-2.2.1/jcirs/target/scala-2.10/classes...
[error] /home/daniel/play-2.2.1/jcirs/app/models/MedicalIncident.java:4: error: package javax.persistence does not exist
[error] import javax.persistence.*;
[error] ^
[error] /home/daniel/play-2.2.1/jcirs/app/models/MedicalIncident.java:6: error: package play.db.ebean does not exist
[error] import play.db.ebean.*;
[error] ^
[error] /home/daniel/play-2.2.1/jcirs/app/models/MedicalIncident.java:14: error: cannot find symbol
[error] public class MedicalIncident extends Model {
[error]                                      ^
[error]   symbol: class Model
[error] /home/daniel/play-2.2.1/jcirs/app/models/MedicalIncident.java:13: error: cannot find symbol
[error] @Entity
[error]  ^
[error]   symbol: class Entity
[error] /home/daniel/play-2.2.1/jcirs/app/models/MedicalIncident.java:15: error: cannot find symbol
[error]     @id
[error]      ^
[error]   symbol:   class id
[error]   location: class MedicalIncident
[error] /home/daniel/play-2.2.1/jcirs/app/controllers/MedicalIncident.java:21: error: cannot find symbol
[error]         MedicalIncident medical_incident = Form.form(MedicalIncident.class).bindFormRequest().get();
[error]                                            ^
[error]   symbol:   variable Form
[error]   location: class MedicalIncident
[error] 6 errors
[error] (compile:compile) javac returned nonzero exit code
[error] application - 

! @6gfjpj0cf - Internal server error, for (GET) [/] ->

play.PlayExceptions$CompilationException: Compilation error[error: package javax.persistence does not exist]
        at play.PlayReloader$$anon$$anonfun$reload$$anonfun$apply$$anonfun$apply.apply(PlayReloader.scala:304) ~[na:na]
        at play.PlayReloader$$anon$$anonfun$reload$$anonfun$apply$$anonfun$apply.apply(PlayReloader.scala:304) ~[na:na]
        at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
        at play.PlayReloader$$anon$$anonfun$reload$$anonfun$apply.apply(PlayReloader.scala:304) ~[na:na]
        at play.PlayReloader$$anon$$anonfun$reload$$anonfun$apply.apply(PlayReloader.scala:298) ~[na:na]
        at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
[warn] play - No application found at invoker init

采纳答案by Ruslans Uralovs

Have you updated your project dependencies in Build.scala? Not sure if anything has changed in Play 2.2.1 but for Play 2.1.3 dependencies in Build.scala would look like this:

您是否在 Build.scala 中更新了您的项目依赖项?不确定 Play 2.2.1 中是否有任何更改,但 Build.scala 中的 Play 2.1.3 依赖项如下所示:

  val appDependencies = Seq(
    javaCore,
    javaJdbc,
    javaEbean
  )

EDIT: Once you update your dependencies in Build.scala don't forget to update dependencies in your IDEA or Eclipse by running corresponding play command, e.g.

编辑:在 Build.scala 中更新依赖项后,不要忘记通过运行相应的播放命令来更新 IDEA 或 Eclipse 中的依赖项,例如

play idea

回答by cahen

Playframework is generating the IDEA specific files when you run "play idea" (if you're using Eclipse, run "play eclipse"). If you imported the project in the same way the tutorial explained, you should have the necessary JARs already available in the project.

当您运行“play idea”(如果您使用的是 Eclipse,请运行“play eclipse”)时,Playframework 会生成 IDEA 特定的文件。如果您按照教程所解释的相同方式导入了项目,那么您应该已经在项目中提供了必要的 JAR。

If it's still not working, verify if IDEA correctly assigned a JDK to your project.

如果它仍然不起作用,请验证 IDEA 是否正确地为您的项目分配了 JDK。

Try to understand what this framework is doing under the hood, don't just try to make it work and ignore the important stuff.

试着理解这个框架在幕后做了什么,不要只是试图让它工作而忽略重要的东西。

回答by victorf

I had the same problem and I was using Eclipse. I included @Entity, e.g. and in my project console I typed eclipse with-source=true. After a "Refresh" in my Eclipse project and a "Clean", it brings me all JARs and then I clicked on the issue and the IDE offered the possibility to include javax.persistence.*, play.db.ebean.Model, etc.

我有同样的问题,我正在使用 Eclipse。我包括了@Entity,例如,在我的项目控制台中,我输入了eclipse with-source=true. 经过“刷新”在我的Eclipse项目和一个“干净”,它带给我的所有JAR文件,然后点击我在这个问题上和IDE提供的可能性包括javax.persistence.*play.db.ebean.Model等等。

回答by A. Rick

I was having the same problem, while using a heavily modified version of Eclipse Neon.
The clue to solving it for me, was that the only place I was seeing the errors was during a Mavenbuild. The Eclipse IDE was able to compile and run the program just fine.
I was able to solve it by adding a couple of dependencies to the project's pom.xml file. See below for the xml code for the dependencies.
I was able to get the groupId's and artifactId's versions by drilling down the directories out on Maven Central. I just walked the tree until I found the metadata.xml files for the persistence.coreand the persistence.jpaartifacts.

我在使用经过大量修改的 Eclipse Neon 版本时遇到了同样的问题。
为我解决它的线索是,我看到错误的唯一地方是在Maven构建期间。Eclipse IDE 能够很好地编译和运行程序。
我能够通过向项目的 pom.xml 文件添加几个依赖项来解决它。有关依赖项的 xml 代码,请参见下文。
通过在Maven Central上向下钻取目录,我能够获得 groupId 和 artifactId 的版本。我只是遍历树,直到找到了persistence.corepersistence.jpa工件的metadata.xml 文件。

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.core</artifactId>
        <version>2.6.4</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa</artifactId>
        <version>2.6.4</version>
        <scope>compile</scope>
    </dependency>