本文介绍了CRUD模块播放! 1.2.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Play开发一个网络应用!框架(1.2.4)。我已经成功导入了CRUD模块,并使用JDBC连接了MySQL数据库。我创建了这样的模型:

I'm developing a webapp with Play! Framework (1.2.4). I've import CRUD module successfully, and connected a MySQL DB with JDBC.I've created a model like this:

@Entity

public class MyEntity extends Model {

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

    public String name;
    public String phone;
    public String web;
    public String address;

    public MyEntity (String name, String phone, String web, String address) {
        this.name=name;
        this.phone=phone;
        this.web=web;
        this.address=address;
    }
}

localhost:9000 / admin ,在管理CRUD默认面板中,我尝试添加MyEntity的新对象,但不起作用:

In localhost:9000/admin, in Administration CRUD default panel, I try to Add a new object of MyEntity, doesn't works:

PersistenceException occured : org.hibernate.exception.GenericJDBCException: could not insert: [models.MyEntity]

LogFail: object.save() (around line 152)

在我的MySQL数据库中,我有一个名为MyEntity的表,其中包含5个字段:

In my MySQL DB i have a table called MyEntity with 5 fields:

id INT (PK, NotNull, AUTO_INCREMENT);
name VARCHAR;
phone INT;
web VARCHAR;
address VARCHAR

不知道为什么它不起作用。我知道JPA使用一个自动生成的ID字段,但我希望该自动生成的ID将是MyEntity ID字段。

Don't know why it doesn't works. I know JPA uses an autogenerated id field, but i want that autogenerated id will be MyEntity id field.

日志说:

[...]Caused by: java.sql.SQLException: Field 'id' doesn't have a default value.

对此有任何想法吗?

推荐答案

模型已经有一个ID。

如果要使用自己的ID,则应该扩展GenericModel。

If you want to use your own Id then you should extend GenericModel instead.

这篇关于CRUD模块播放! 1.2.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:21