1

我的游戏框架配置有一个问题,当我插入一个新的时,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;
   }
}
4

2 回答 2

1

我认为错误:

发生 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,您应该AUTO_INCREMENT在 ID 列上定义为序列定义。


更新 - 我已经为 H2 DB 设置尝试过这个

使用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;
   }
}
于 2013-04-08T14:16:55.960 回答
0

我发现了错误!!我只需要添加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;
  }
}
于 2013-04-12T21:07:19.457 回答