连接“计算机-数据库-jpa"使用 MySQL 播放 2.1 示例应用程序 [英] Connect "computer-database-jpa" Play 2.1 sample application with MySQL

查看:24
本文介绍了连接“计算机-数据库-jpa"使用 MySQL 播放 2.1 示例应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩 computer-database-jpa (Java) Play Framework 2.1 示例应用程序.当我在内存数据库中使用 H2 时一切正常,但是当我想将应用程序与 MySQL 连接时遇到问题.

I'm playing with computer-database-jpa (Java) Play Framework 2.1 sample application. Everything works fine when I'm using H2 in memory database but I had problems when I want to connect the application with MySQL.

有些人遇到了同样的问题(帮助希望将示例应用程序连接到MySQL) 但没有解决方案.

Some one had the same problem (Help wanted getting sample app connected to MySQL) but there was no solution.

我添加了 mysql-connector (Build.scala):

I've added mysql-connector (Build.scala):

val appDependencies = Seq(
  ....
  "mysql" % "mysql-connector-java" % "5.1.18"
)

并编辑application.conf:

and edited application.conf:

db.default.url="jdbc:mysql://password:user@localhost/my-database"
db.default.driver=com.mysql.jdbc.Driver

当我启动应用程序并应用 1.sql(进化脚本)时,出现错误:

When I start the applications and apply 1.sql (evolution script) I get an error:

You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'sequence company_seq
start with 1000' at line 1 [ERROR:1064, SQLSTATE:42000]

有人知道如何解决这个问题吗?

Does anyone have an idea how to solve the problem?

推荐答案

我找到了解决方案 - https://github.com/opensas/openshift-play2-computerdb.

I've found the solution - https://github.com/opensas/openshift-play2-computerdb.

进化脚本中使用的语法不符合MySQL:

Syntax used in evolution scripts isn't conform with MySQL:

  • 添加了 engine=innodb,以启用参照完整性
  • 用自动增量替换 id 字段的序列
  • 将SET REFERENTIAL_INTEGRITY"命令替换为SET FOREIGN_KEY_CHECKS"
  • 用日期时间替换时间戳字段
  • 将计算机数据拆分为 2.sql 和 3.sql 文件(避免在 mysql 上运行时出现错误)
  • 从 Computer.list sql 查询中删除了最后一个空值"
  • 修改 Computer.insert 以跳过 id 字段(因为是由 mysql 自动分配的)

因为我玩的是 Java 而不是 Scala 版本,所以我要更改 Company.javaComputer.java 文件.我添加了 @GeneratedValue 注释:

Because I was playing with Java and not Scala version I'd to change Company.java and Computer.java files. I've added @GeneratedValue annotation:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;

在这里你可以找到修改后的进化脚本:https://github.com/opensas/openshift-play2-computerdb/tree/master/conf/evolutions/default

Here you can find modified evolution scripts: https://github.com/opensas/openshift-play2-computerdb/tree/master/conf/evolutions/default

这篇关于连接“计算机-数据库-jpa"使用 MySQL 播放 2.1 示例应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆