【问题标题】:Error insert element (primary key)错误插入元素(主键)
【发布时间】:2013-04-08 12:19:36
【问题描述】:

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

【问题讨论】:

  • 你能重新格式化一下,让它可读吗?例如删除行号。

标签: java web-services jpa playframework playframework-1.x


【解决方案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,你应该在ID列定义AUTO_INCREMENT作为序列定义。


更新 - 我已经为 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;
   }
}

【讨论】:

  • 您好,非常感谢您的回答,我现在正在像数据库一样使用系统文件服务器,并且在 conf 文件中使用开发模式,但错误并没有消失.我不知道在哪里可以启用 hibernate_sequence。我将为生产做一个 MySQL 数据库,但对于开发我更喜欢文件系统进行测试。当我要做一个 MySQL 数据库时,我必须在哪里定义 AUTO_INCREMENT?谢谢!!!
  • 您是使用SQLite 还是H2 数据库进行开发?我对SQLite 不熟悉,但是,如果您使用类似的内存数据库并使用Model not GenericModel 扩展您的模型(Notification.java),则错误应该不会出现。序列将自动生成。然后,对于 AUTO_INCREMENT 的 MySQL 配置,您应该遵循此 tutorial。希望对你有帮助。
  • 是的,我使用 H2 数据库进行开发,并使用 Model 扩展了我的 Model Notification.java,没有其他 ID,只有自动生成的 ID,但错误继续出现。真奇怪。非常感谢您的宝贵时间。
  • 好的,请。我不知道为什么这个错误......很奇怪......发布你的代码,我现在试试,我告诉你结果。非常感谢!
  • 嗨!!!我已经解决了问题!!!你不会相信的!!我忘了在构造函数的第一个添加 super() ......我添加了这个,问题就解决了!非常感谢您的帮助!!当你想要的时候,我请你喝几杯啤酒!
【解决方案2】:

我发现了错误!我只需要在构造函数中添加 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-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多