[英]Error insert element (primary key)
我的播放框架配置有一个问题,当我插入一个新的Notification
我收到了错误。 我不知道为什么会发生此错误。 因为我扩展了Model
类,所以Play必须为每一行生成一个新的ID。
如果您可以对我说出问题是什么,或采取其他更好的方法来做到这一点,也许是因为我扩展了GenericModel
并执行了如下注释的代码:
// @Id
// @GeneratedValue(strategy = GenerationType.AUTO)
// public long id;
但是如果这样做,我该如何插入新行?
非常感谢!!!!
发现错误:
发生PersistenceException :
org.hibernate.HibernateException
:数据库未返回任何本机生成的标识值
这是/app/controllers/WSConfiguracion.java
:
if (cliente.valorBateria < filasConfiguracionClientes.get(i).limiteBateria) {
if (!estadoNotificacion.hayNotiBateria) {
// code below generated the error
Notificacion notificacion = new Notificacion(
filasConfiguracionClientes.get(i).imeiadmin,imei, "bateria baja"
).save();
estadoNotificacion.hayNotiBateria = true;
//notificacion.save();
estadoNotificacion.save();
renderText("NOTIFICA BATERIA");
}
} else {
...
}
package models;
import javax.persistence.Entity;
import play.db.jpa.Model;
@Entity
public class Notificacion extends Model {
//@Id
//@GeneratedValue(strategy = GenerationType.AUTO)
//public long id;
//@Id
public long imeiadmin;
//@Id
public long imeiclient;
public String detalleNotificacion;
public Notificacion(long imeiadmin, long imeiclient,String detalleNotificacion) {
this.imeiadmin = imeiadmin;
this.imeiclient = imeiclient;
this.detalleNotificacion = detalleNotificacion;
}
}
我认为错误:
发生PersistenceException :
org.hibernate.HibernateException
:数据库未返回任何本机生成的标识值
发生此错误是因为您的数据库中没有序列 。 如果您为模型扩展了Model
类,并且处于开发模式,则Play!Framework会自动在名为hibernate_sequence
的数据库上生成序列。 该序列用于为您的模型生成ID 。 您可以检查数据库以确保存在该序列。
如果存在hibernate_sequence
则可以像之前一样插入数据:
Notificacion notificacion = new Notificacion(
filasConfiguracionClientes.get(i).imeiadmin,imei, "bateria baja"
).save();
然后,应解决以上错误。
注意:
如果您使用PostgreSQL数据库,我指的是此答案。 如果使用MySQL等其他数据库,则应在ID列上定义
AUTO_INCREMENT
作为序列定义。
使用H2
数据库作为application.conf
配置:
# Development mode
application.mode=dev
# Set simple file written database (H2 file stored)
db=fs
# JPA DDL update for development purpose only
jpa.ddl=update
public static void test15878866() {
// force to insert dummy data
Notificacion notificacion = new Notificacion(
1L, 2L, "This is new notificacion"
).save();
renderText(notificacion.detalleNotificacion);
}
@Entity
public class Notificacion extends Model {
public long imeiadmin;
public long imeiclient;
public String detalleNotificacion;
public Notificacion(long imeiadmin, long imeiclient,String detalleNotificacion) {
this.imeiadmin = imeiadmin;
this.imeiclient = imeiclient;
this.detalleNotificacion = detalleNotificacion;
}
}
我发现了错误! 我只需要添加super(); 在构造函数中。 谢谢大家,iwawiwi。
该模型 :
@Entity
public class Notificacion extends Model {
public long imeiadmin;
public long imeiclient;
public String detalleNotificacion;
public Notificacion(long imeiadmin, long imeiclient,String detalleNotificacion) {
super();
this.imeiadmin = imeiadmin;
this.imeiclient = imeiclient;
this.detalleNotificacion = detalleNotificacion;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.