Hibernate playframework JPA.em().merge(this);SQLGrammarException

Hibernate playframework JPA.em().merge(this);SQLGrammarException,hibernate,jpa,merge,playframework-2.2,persist,Hibernate,Jpa,Merge,Playframework 2.2,Persist,我正在尝试将类中的数据插入数据库 这是我的模型课 package models; import java.util.*; import javax.persistence.*; import javax.persistence.Entity; import javax.validation.*; import play.data.validation.Constraints; import play.data.validation.Constraints.*; import play.api

我正在尝试将类中的数据插入数据库

这是我的模型课

package models;

import java.util.*;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.validation.*;

import play.data.validation.Constraints;
import play.data.validation.Constraints.*;
import play.api.db.*;
import play.db.jpa.*;
import javax.persistence.EntityManager;

@Entity
public class MedicalIncident   {

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

    @Constraints.Required
    public String month;

    @Constraints.Required
    public String place;

    @Constraints.Required
    public String unit;

    @Constraints.Required
    public String incident_type;

    @Constraints.Required
    public int age;



    /**
     * Insert this new incident submission.
     */
    public void toDataBase() {
        // persist object - add to entity manager
        JPA.em().merge(this);
        JPA.em().persist(this);
    }

}
.merge方法给了我一个错误:

[info] play - Application started (Dev)
[error] o.h.e.j.s.SqlExceptionHelper - Kolumna "MEDICALINC0_._EBEAN_INTERCEPT" nie istnieje
Column "MEDICALINC0_._EBEAN_INTERCEPT" not found; SQL statement:
select medicalinc0_.id as id1_0_0_, medicalinc0_._ebean_intercept as _ebean_i2_0_0_, medicalinc0_.age as age3_0_0_, medicalinc0_.incident_type as incident4_0_0_, medicalinc0_.month as month5_0_0_, medicalinc0_.place as place6_0_0_, medicalinc0_.unit as unit7_0_0_ from MedicalIncident medicalinc0_ where medicalinc0_.id=? [42122-172]
[error] play - Cannot invoke the action, eventually got an error: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
[error] application - 

! @6ghb8od3o - Internal server error, for (POST) [/incydent_medyczny/zapisz] ->

play.api.Application$$anon$1: Execution exception[[PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement]]
        at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.1]
        at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
        at scala.Option.map(Option.scala:145) [scala-library.jar:na]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1316) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:898) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at models.MedicalIncident.toDataBase(MedicalIncident.java:43) ~[na:na]
        at controllers.MedicalIncidents.submit(MedicalIncidents.java:50) ~[na:na]
Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement
        at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:188) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:159) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1859) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
Caused by: org.h2.jdbc.JdbcSQLException: Kolumna "MEDICALINC0_._EBEAN_INTERCEPT" nie istnieje
Column "MEDICALINC0_._EBEAN_INTERCEPT" not found; SQL statement:
select medicalinc0_.id as id1_0_0_, medicalinc0_._ebean_intercept as _ebean_i2_0_0_, medicalinc0_.age as age3_0_0_, medicalinc0_.incident_type as incident4_0_0_, medicalinc0_.month as month5_0_0_, medicalinc0_.place as place6_0_0_, medicalinc0_.unit as unit7_0_0_ from MedicalIncident medicalinc0_ where medicalinc0_.id=? [42122-172]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2.jar:1.3.172]
        at org.h2.message.DbException.get(DbException.java:169) ~[h2.jar:1.3.172]
        at org.h2.message.DbException.get(DbException.java:146) ~[h2.jar:1.3.172]
        at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:144) ~[h2.jar:1.3.172]
        at org.h2.expression.Alias.optimize(Alias.java:52) ~[h2.jar:1.3.172]
        at org.h2.command.dml.Select.prepare(Select.java:808) ~[h2.jar:1.3.172]
我所能看到的是,它显示了“MEDICALINC0.\u EBEAN\u INTERCEPT”列没有找到,这是很明显的,因为我确实没有db中的那个列。 我的数据库:


还是应该使用persist()方法?

尝试禁用EBean增强功能。在
conf/application.conf
文件中,用ebean*注释掉所有内容。对你的
build.sbt
文件也做同样的操作。

我已经下载了clean play zip。 并在新的基础上创建项目。 然后,我将旧项目中的dir app configuration public放入新创建的项目中。 这有助于我现在得到一个错误:

 [PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]] 


[info] play - datasource [jdbc:h2:mem:play] bound to JNDI as DefaultDS
[info] play - database [default] connected at jdbc:h2:mem:play
[info] play - Application started (Dev)
[[Ljava.lang.Object;@39fa10a8, [Ljava.lang.Object;@505ba354]
------------------------
--> -  - <--
models.Unit@41fc1f20
Form(of=class models.MedicalIncident, data={id=3, incident_type=Skaleczono nożyczkami, unit=Chirurgii, age=15, month=Czerwiec, place=Place}, value=Some(models.MedicalIncident@4ae2fe41), errors={})
------------------------
------------------------
models.MedicalIncident@4ae2fe41
------------------------
[error] o.h.u.JDBCExceptionReporter - Pole nie moze byc puste "ID"
NULL not allowed for column "ID"; SQL statement:
insert into MedicalIncident (id, age, incident_type, month, place, unit) values (null, ?, ?, ?, ?, ?) [23502-172]
[error] play - Cannot invoke the action, eventually got an error: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]
[error] application - 

! @6ghhj38ei - Internal server error, for (POST) [/incydent_medyczny/zapisz] ->

play.api.Application$$anon$1: Execution exception[[PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]]]
        at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.1]
        at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
        at scala.Option.map(Option.scala:145) [scala-library.jar:na]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1321) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:860) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at models.MedicalIncident.toDataBase(MedicalIncident.java:44) ~[na:na]
        at controllers.MedicalIncidents.submit(MedicalIncidents.java:49) ~[na:na]
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
Caused by: org.h2.jdbc.JdbcSQLException: Pole nie moze byc puste "ID"
NULL not allowed for column "ID"; SQL statement:
insert into MedicalIncident (id, age, incident_type, month, place, unit) values (null, ?, ?, ?, ?, ?) [23502-172]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2.jar:1.3.172]
        at org.h2.message.DbException.get(DbException.java:169) ~[h2.jar:1.3.172]
        at org.h2.message.DbException.get(DbException.java:146) ~[h2.jar:1.3.172]
        at org.h2.table.Column.validateConvertUpdateSequence(Column.java:295) ~[h2.jar:1.3.172]
        at org.h2.table.Table.validateConvertUpdateSequence(Table.java:699) ~[h2.jar:1.3.172]
        at org.h2.command.dml.Insert.insertRows(Insert.java:122) ~[h2.jar:1.3.172]
[PersistenceException:org.hibernate.exception.ConstraintViolationException:无法插入:[models.MedicalIncident]]
[info]play-datasource[jdbc:h2:mem:play]作为DefaultDS绑定到JNDI
[信息]播放-数据库[默认]连接在jdbc:h2:mem:play
[信息]播放-应用程序已启动(开发)
[[Ljava.lang.Object;@39fa10a8[Ljava.lang.Object;@505ba354]
------------------------
--> -  - 
play.api.Application$$anon$1:执行异常[[PersistenceException:org.hibernate.exception.ConstraintViolationException:无法插入:[models.MedicalIncident]]
在play.api.Application$class.handleError(Application.scala:293)~[play_2.10.jar:2.2.1]
play.api.DefaultApplication.handleError(Application.scala:399)[play_2.10.jar:2.2.1]
在play.core.server.netty.playdaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(playdaultUpstreamHandler.scala:261)[play_2.10.jar:2.2.1]
在play.core.server.netty.playdaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(playdaultUpstreamHandler.scala:261)[play_2.10.jar:2.2.1]
在scala.Option.map(Option.scala:145)[scala library.jar:na]
在play.core.server.netty.playdaultUpstreamHandler$$anonfun$2.applyOrElse(playdaultUpstreamHandler.scala:261)[play_2.10.jar:2.2.1]
原因:javax.persistence.PersistenceException:org.hibernate.exception.ConstraintViolationException:无法插入:[models.MedicalIncident]
在org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
在org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315)~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
在org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1321)~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
在org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:860)~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
在models.MedicalIncident.toDataBase(MedicalIncident.java:44)~[na:na]
在controllers.MedicalIncidents.submit(MedicalIncidents.java:49)~[na:na]
原因:org.hibernate.exception.ConstraintViolationException:无法插入:[models.MedicalIncident]
在org.hibernate.exception.sqlstatecoverter.convert(sqlstatecoverter.java:96)~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
在org.hibernate.exception.jdbceptionhelper.convert(jdbceptionhelper.java:66)~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
在org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345)~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852)~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
在org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
原因:org.h2.jdbc.jdbcsql异常:Pole nie moze byc puste“ID”
列“ID”不允许为NULL;SQL语句:
插入MedicalIncident(id、年龄、事件类型、月份、地点、单位)值(空、、、、、、、、、、?)[23502-172]
在org.h2.message.DbException.getJdbcSQLException(DbException.java:329)~[h2.jar:1.3.172]
在org.h2.message.DbException.get(DbException.java:169)~[h2.jar:1.3.172]
在org.h2.message.DbException.get(DbException.java:146)~[h2.jar:1.3.172]
在org.h2.table.Column.validateConvertUpdateSequence(Column.java:295)~[h2.jar:1.3.172]
在org.h2.table.table.validateConvertUpdateSequence(table.java:699)~[h2.jar:1.3.172]
在org.h2.command.dml.Insert.insertRows(Insert.java:122)~[h2.jar:1.3.172]

在这个错误中,没有出现ebean。所以我认为它已经解决了。

我在conf/application.conf中没有任何ebean条目build.sbt中没有任何链接当然,对于ebean,你有一些东西:你有javaEbean,一个你在这里写的s
 [PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]] 


[info] play - datasource [jdbc:h2:mem:play] bound to JNDI as DefaultDS
[info] play - database [default] connected at jdbc:h2:mem:play
[info] play - Application started (Dev)
[[Ljava.lang.Object;@39fa10a8, [Ljava.lang.Object;@505ba354]
------------------------
--> -  - <--
models.Unit@41fc1f20
Form(of=class models.MedicalIncident, data={id=3, incident_type=Skaleczono nożyczkami, unit=Chirurgii, age=15, month=Czerwiec, place=Place}, value=Some(models.MedicalIncident@4ae2fe41), errors={})
------------------------
------------------------
models.MedicalIncident@4ae2fe41
------------------------
[error] o.h.u.JDBCExceptionReporter - Pole nie moze byc puste "ID"
NULL not allowed for column "ID"; SQL statement:
insert into MedicalIncident (id, age, incident_type, month, place, unit) values (null, ?, ?, ?, ?, ?) [23502-172]
[error] play - Cannot invoke the action, eventually got an error: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]
[error] application - 

! @6ghhj38ei - Internal server error, for (POST) [/incydent_medyczny/zapisz] ->

play.api.Application$$anon$1: Execution exception[[PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]]]
        at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.1]
        at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
        at scala.Option.map(Option.scala:145) [scala-library.jar:na]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1321) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:860) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
        at models.MedicalIncident.toDataBase(MedicalIncident.java:44) ~[na:na]
        at controllers.MedicalIncidents.submit(MedicalIncidents.java:49) ~[na:na]
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [models.MedicalIncident]
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
        at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
Caused by: org.h2.jdbc.JdbcSQLException: Pole nie moze byc puste "ID"
NULL not allowed for column "ID"; SQL statement:
insert into MedicalIncident (id, age, incident_type, month, place, unit) values (null, ?, ?, ?, ?, ?) [23502-172]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2.jar:1.3.172]
        at org.h2.message.DbException.get(DbException.java:169) ~[h2.jar:1.3.172]
        at org.h2.message.DbException.get(DbException.java:146) ~[h2.jar:1.3.172]
        at org.h2.table.Column.validateConvertUpdateSequence(Column.java:295) ~[h2.jar:1.3.172]
        at org.h2.table.Table.validateConvertUpdateSequence(Table.java:699) ~[h2.jar:1.3.172]
        at org.h2.command.dml.Insert.insertRows(Insert.java:122) ~[h2.jar:1.3.172]