首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >播放2.4中Hibernate实体的问题

问播放2.4中Hibernate实体的问题
EN

Stack Overflow用户
提问于 2015-05-01 19:30:00
回答 1查看 417关注 0票数 0

我试图在Play 2.4中使用JPA,但它在执行过程中的每一步都会出现错误,这些错误似乎都没有得到很好的记录。

我这里有一个测试实体:

代码语言:javascript
复制
@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中使用了这个属性

代码语言:javascript
复制
<property name="hibernate.hbm2ddl.auto" value="create"/>

我正在努力学习如何真正地坚持这个实体。到目前为止,我在我的一个控制器中得到了这样的信息:

代码语言:javascript
复制
@Transactional
public static Result runExperiment() {
    TestModel tester = new TestModel("testingasdf");
    JPA.em().persist(tester);
    return ok();
}

当我第一次运行它时,它运行得很好,并且我验证了DB中的条目。但是当我第二次运行它时,我会得到错误消息:

代码语言:javascript
复制
a.d.Dispatcher - EntityManager is closed

在类似的说明中,我看到的示例在id上使用了这个符号,这样它就不会经常尝试将0作为id插入:

代码语言:javascript
复制
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;

但当我尝试这样做的时候,我会犯这样的错误:

代码语言:javascript
复制
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,但我不知道.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-01 19:56:01

事实证明,教程中的播放帖子是特定于他们的H2数据库系统的。作为一个菜鸟,我忽略了这样一个事实:您需要告诉Hibernate使用什么方言。在persistence.xml中,我将方言行改为:

代码语言:javascript
复制
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

看来我的问题已经解决了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29993781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档