Play 2.4 中的 Hibernate 实体问题
Trouble with Hibernate Entity in Play 2.4
我正在尝试在 Play 2.4 中使用 JPA,但它在每一步都出现错误,none 其中似乎有据可查。
我这里有一个测试实体:
@Entity
@Table(name="testit")
public class TestModel {
@Id
private long id;
private String title;
public TestModel(String title) {
this.title = title;
}
public long getId() {
return id;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
}
因为我要经常更新和创建实体,所以我在 persistence.xml
:
中使用这个 属性
<property name="hibernate.hbm2ddl.auto" value="create"/>
而且我正在努力学习如何真正持久化这个实体。这是我目前在我的一个控制器中得到的:
@Transactional
public static Result runExperiment() {
TestModel tester = new TestModel("testingasdf");
JPA.em().persist(tester);
return ok();
}
当我第一次 运行 时,它工作得很好,我验证了数据库中的条目。但是当我第二次 运行 时,我收到错误消息:
a.d.Dispatcher - EntityManager is closed
类似地,我看到的例子在 id 上有这个符号,所以它不会经常尝试插入 0
作为 id:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
但是当我尝试这样做时,出现如下错误:
o.h.t.h.SchemaExport - HHH000389: Unsuccessful: drop table testit if exists
o.h.t.h.SchemaExport - 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 'if exists' at line 1
o.h.t.h.SchemaExport - HHH000389: Unsuccessful: create table testit (id bigint generated by default as identity, title varchar(255), primary key (id))
o.h.t.h.SchemaExport - 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 'generated by default as identity, title varchar(255), primary key (id))' at line 1
老实说,我在试图弄清楚如何设置像这样的简单实体时有点迷茫。网络上的所有建议都建议使用 JPA 而不是 eBeans,但我不知道....
原来教程Play posts是针对他们的H2数据库系统的。作为菜鸟,我错过了一个事实,即您需要告诉 Hibernate 使用哪种方言。在 persistence.xml 中,我将方言行更改为:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
看来我的问题已经解决了。
我正在尝试在 Play 2.4 中使用 JPA,但它在每一步都出现错误,none 其中似乎有据可查。
我这里有一个测试实体:
@Entity
@Table(name="testit")
public class TestModel {
@Id
private long id;
private String title;
public TestModel(String title) {
this.title = title;
}
public long getId() {
return id;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
}
因为我要经常更新和创建实体,所以我在 persistence.xml
:
<property name="hibernate.hbm2ddl.auto" value="create"/>
而且我正在努力学习如何真正持久化这个实体。这是我目前在我的一个控制器中得到的:
@Transactional
public static Result runExperiment() {
TestModel tester = new TestModel("testingasdf");
JPA.em().persist(tester);
return ok();
}
当我第一次 运行 时,它工作得很好,我验证了数据库中的条目。但是当我第二次 运行 时,我收到错误消息:
a.d.Dispatcher - EntityManager is closed
类似地,我看到的例子在 id 上有这个符号,所以它不会经常尝试插入 0
作为 id:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
但是当我尝试这样做时,出现如下错误:
o.h.t.h.SchemaExport - HHH000389: Unsuccessful: drop table testit if exists
o.h.t.h.SchemaExport - 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 'if exists' at line 1
o.h.t.h.SchemaExport - HHH000389: Unsuccessful: create table testit (id bigint generated by default as identity, title varchar(255), primary key (id))
o.h.t.h.SchemaExport - 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 'generated by default as identity, title varchar(255), primary key (id))' at line 1
老实说,我在试图弄清楚如何设置像这样的简单实体时有点迷茫。网络上的所有建议都建议使用 JPA 而不是 eBeans,但我不知道....
原来教程Play posts是针对他们的H2数据库系统的。作为菜鸟,我错过了一个事实,即您需要告诉 Hibernate 使用哪种方言。在 persistence.xml 中,我将方言行更改为:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
看来我的问题已经解决了。