gpt4 book ai didi

java - Playframework 2.3.x JAVA + JPA + MySQL

转载 作者:行者123 更新时间:2023-11-29 07:44:35 29 4
gpt4 key购买 nike

我正在使用 playframework 2.3.x 和 Java。对于我的项目,我想连接 mysql 并使用 JPA,连接工作正常(我认为),但是当我尝试创建模型用户时,出现此错误:

[error] C:\Users\lpiroche\Documents\www\play_framework\play_sample1\modules\core
\app\models\User.java:3: error: package javax.persistence does not exist
[error] import javax.persistence.Column;
[error] ^
[error] C:\Users\lpiroche\Documents\www\play_framework\play_sample1\modules\core
\app\models\User.java:4: error: package javax.persistence does not exist
[error] import javax.persistence.Entity;
[error] ^
[error] C:\Users\lpiroche\Documents\www\play_framework\play_sample1\modules\core
\app\models\User.java:5: error: package javax.persistence does not exist
[error] import javax.persistence.GeneratedValue;
[error] ^
[error] C:\Users\lpiroche\Documents\www\play_framework\play_sample1\modules\core
\app\models\User.java:6: error: package javax.persistence does not exist
[error] import javax.persistence.GenerationType;
[error] ^
[error] C:\Users\lpiroche\Documents\www\play_framework\play_sample1\modules\core
\app\models\User.java:7: error: package javax.persistence does not exist
[error] import javax.persistence.*;
[error] ^
[error] C:\Users\lpiroche\Documents\www\play_framework\play_sample1\modules\core
\app\models\User.java:9: error: cannot find symbol
[error] @Entity
[error] ^
[error] symbol: class Entity
[error] C:\Users\lpiroche\Documents\www\play_framework\play_sample1\modules\core
\app\models\User.java:10: error: cannot find symbol
[error] @Table(name="users")

Play 文档在连接 mysql 数据库的示例中很差,我读了tuto,帖子,但我没有找到解决方案。我想我忘记了 persistence.xml 文件中的某些内容,但我不知道是什么。

build.sbt

libraryDependencies ++= Seq(  
javaJdbc,
cache,
javaWs,
javaJpa,
"org.hibernate" % "hibernate-entitymanager" % "3.6.9.Final",
"mysql" % "mysql-connector-java" % "5.1.18"
)

application.conf

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/mydb"
db.default.user=root
db.default.password=""
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit

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="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="true"></property>
</properties>
</persistence-unit>
</persistence>

在示例中我发现不同的配置:

<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.format_sql" value="true"></property>

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />  
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />

不知道哪个最合适

最佳答案

我发现了我的问题。也许另一个人可能会遇到类似的问题,所以我会解释一下。

我的项目分为 3 个子项目:例如 core、common、subproject1

  1. core = 公共(public)类和api代码
  2. common = 公共(public)模块(popins、页脚、页眉...)它可以包含 View 和 Controller
  3. subproject1 = 网站或管理模块

common 依赖于 core,subproject1 依赖于 common。我的主项目(根)依赖于 common 和 subproject1 并将它们聚合起来

build.sbt

name := "sample"

version := "1.0"


libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
javaJpa,
"org.hibernate" % "hibernate-entitymanager" % "3.6.9.Final",
"mysql" % "mysql-connector-java" % "5.1.18"
)

lazy val root = (project in file(".")).enablePlugins(PlayJava)
.aggregate(common, subproject1 )
.dependsOn(common, subproject1 )

lazy val core = (project in file("modules/core")).enablePlugins(PlayJava)

lazy val common = (project in file("modules/common")).enablePlugins(PlayJava).dependsOn(core)
lazy val subproject1 = (project in file("modules/subproject1 ")).enablePlugins(PlayJava).dependsOn(common)

当尝试在 core 子项目(包 core.app.models.datas)中创建我的 User 类时,就会出现问题,显然因为 core 没有直接聚合在我的根项目 javax.data 中。未导入持久化包。如果我在 common 或 subproject1 中创建一个 User 类,我没有问题。

为了解决这个问题,我创建了一个具有良好依赖项的 build.sbt 文件:

.../core/build.sbt

name := "core"
version := "1.0"

libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
javaJpa,
"org.hibernate" % "hibernate-entitymanager" % "3.6.9.Final",
"mysql" % "mysql-connector-java" % "5.1.18"
)

关于java - Playframework 2.3.x JAVA + JPA + MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28189632/

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