2

我正在使用带有 JPA 的 Play 框架来存储数据库,但出现了一些问题:

我在做什么是将用户存储到数据库中,但异常即将到来。

我的控制器是:

public class Application extends Controller {



    @Transactional(readOnly=true)
    public static Result index() {

        Form<Userd> ud=form(Userd.class);
        return ok(index.render(ud));
    }
    @Transactional
    public static Result enter(){
        Form<Userd> crForm = form(Userd.class).bindFromRequest();

        if(crForm.hasErrors()){

            return badRequest(index.render(crForm));
        }
        else{

            crForm.get().save();
            return ok("value saved");
        }

    }

}

型号为:

@Entity
public class Userd {


    @Required
    public String name;

    @Email
    public String email;
    @Id
    public Long empid;
    @Required
    public String password;

    public static Userd findById(Long id) {
        return JPA.em().find(Userd.class,id );
    }
    public Userd() {

    }
    public Userd(String name, String email, Long empid, String password) {

        this.name = name;
        this.email = email;
        this.empid = empid;
        this.password = password;
    }
    public void save(){
        JPA.em().persist(this);

    }

例外

[RollbackException: Error while committing the transaction] 

终端屏幕是:

[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)
[error] o.h.u.JDBCExceptionReporter - Table "USERD" not found; SQL statement:
insert into Userd (email, name, password, empid) values (?, ?, ?, ?) [42102-168]
[error] application - 

! @6fiph0k4b - Internal server error, for (POST) [/login] ->

play.api.Application$$anon$1: Execution exception[[RollbackException: Error while committing the transaction]]
    at play.api.Application$class.handleError(Application.scala:289) ~[play_2.10.jar:2.1.2]
    at play.api.DefaultApplication.handleError(Application.scala:383) ~[play_2.10.jar:2.1.2]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$play$core$server$netty$PlayDefaultUpstreamHandler$$handle$1$1.apply(PlayDefaultUpstreamHandler.scala:143) ~[play_2.10.jar:2.1.2]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$play$core$server$netty$PlayDefaultUpstreamHandler$$handle$1$1.apply(PlayDefaultUpstreamHandler.scala:139) ~[play_2.10.jar:2.1.2]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) ~[play_2.10.jar:2.1.2]
    at play.api.libs.concurrent.PlayPromise$$anonfun$extend1$1.apply(Promise.scala:113) ~[play_2.10.jar:2.1.2]
javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
    at play.db.jpa.JPA.withTransaction(JPA.java:107) ~[play-java-jpa_2.10.jar:2.1.2]
    at play.db.jpa.TransactionalAction.call(TransactionalAction.java:14) ~[play-java-jpa_2.10.jar:2.1.2]
    at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:80) ~[play_2.10.jar:2.1.2]
    at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:79) ~[play_2.10.jar:2.1.2]
    at play.libs.F$Promise$PromiseActor.onReceive(F.java:425) ~[play_2.10.jar:2.1.2]
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert: [models.Userd]
    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.TransactionImpl.commit(TransactionImpl.java:81) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
    at play.db.jpa.JPA.withTransaction(JPA.java:107) ~[play-java-jpa_2.10.jar:2.1.2]
    at play.db.jpa.TransactionalAction.call(TransactionalAction.java:14) ~[play-java-jpa_2.10.jar:2.1.2]
    at play.core.j.JavaAction$$anon$2.apply(JavaAction.scala:80) ~[play_2.10.jar:2.1.2]
Caused by: org.hibernate.exception.SQLGrammarException: could not insert: [models.Userd]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) ~[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.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2454) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2874) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
    at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) ~[hibernate-core-3.6.9.Final.jar:3.6.9.Final]
Caused by: org.h2.jdbc.JdbcSQLException: Table "USERD" not found; SQL statement:
insert into Userd (email, name, password, empid) values (?, ?, ?, ?) [42102-168]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2.jar:1.3.168]
    at org.h2.message.DbException.get(DbException.java:169) ~[h2.jar:1.3.168]
    at org.h2.message.DbException.get(DbException.java:146) ~[h2.jar:1.3.168]
    at org.h2.command.Parser.readTableOrView(Parser.java:4770) ~[h2.jar:1.3.168]
    at org.h2.command.Parser.readTableOrView(Parser.java:4748) ~[h2.jar:1.3.168]
    at org.h2.command.Parser.parseInsert(Parser.java:958) ~[h2.jar:1.3.168]

任何人都可以给我解决这个问题的想法吗?是否有必要在运行应用程序之前创建 sql 文件

给点主意!

持久性.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>

</persistence>

application.conf:你可以在这个链接上看到:

应用程序.conf

它正在创建数据库,但没有在项目中创建进化文件。

4

1 回答 1

0

要启用模式自动创建/自动更新,您需要编辑持久性单元配置文件(通常是conf/META-INF/persistence.xml)。

您需要为该属性设置所需的值hibernate.hbm2ddl.auto(查看 Hibernate 文档以获取可用值)。您可以从 value 开始,update如果它不存在,这将使 Hibernate 创建模式,如果它与您的实体不匹配,则更新它。

前任 :

<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>DefaultDS</non-jta-data-source>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>
</persistence-unit>
于 2013-09-16T09:33:31.487 回答