我试图在Play 2.4中使用JPA,但它在执行过程中的每一步都会出现错误,这些错误似乎都没有得到很好的记录。
我这里有一个测试实体:
@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();
}当我第一次运行它时,它运行得很好,并且我验证了DB中的条目。但是当我第二次运行它时,我会得到错误消息:
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,但我不知道.
发布于 2015-05-01 19:56:01
事实证明,教程中的播放帖子是特定于他们的H2数据库系统的。作为一个菜鸟,我忽略了这样一个事实:您需要告诉Hibernate使用什么方言。在persistence.xml中,我将方言行改为:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>看来我的问题已经解决了。
https://stackoverflow.com/questions/29993781
复制相似问题