1

Play フレームワークの構成に問題が 1 つあります。新しいNotificationものを挿入すると、エラーが発生しました。なぜこのエラーが発生したのかわかりません。クラスを拡張するためModel、Play は行ごとに新しい ID を生成する必要があります。

何が問題なのか、またはこれを行うための他のより良い方法を教えていただければ、おそらく、 を拡張してGenericModel、次のようにコメントされているコードを実行します。

// @Id
// @GeneratedValue(strategy = GenerationType.AUTO)
// public long id;

しかし、これを行うと、新しい行を挿入するにはどうすればよいですか?

どうもありがとう!!!!


エラーが見つかりました:

PersistenceException が発生しました: org.hibernate.HibernateException: データベースは、ネイティブに生成された ID 値を返しませんでした

これは/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: データベースは、ネイティブに生成された ID 値を返しませんでした

データベースにシーケンスがないために発生します。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 に答える