连接"computer-database-jpa".使用MySQL播放2.1示例应用程序 [英] Connect "computer-database-jpa" Play 2.1 sample application with MySQL

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

问题描述

我正在玩计算机数据库-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:

将计算机数据库示例应用程序从H2移植到mysql所需的更改列表

conf/evolutions/default/1.sql

  • 添加了engine = innodb,以启用参照完整性
  • 具有自动递增的id字段的替换序列
  • 将"SET REFERENTIAL_INTEGRITY"命令替换为"SET FOREIGN_KEY_CHECKS"
  • 将时间戳记字段替换为日期时间

List of changes needed to port computer-database sample app from H2 to mysql

conf/evolutions/default/1.sql

  • added engine=innodb, to enable referential integrity
  • replaced sequences with autoincrement for id fields
  • replaced 'SET REFERENTIAL_INTEGRITY' command with 'SET FOREIGN_KEY_CHECKS'
  • replaced timestamp fields with datetime
  • 在2.sql和3.sql文件之间拆分计算机数据(避免在mysql上运行的演变中出现错误)
  • 从Computer.list sql查询中删除了"nulls last"
  • 修改了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

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

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