Rahul Kulhari
Rahul Kulhari

Reputation: 1165

How to work with Jpa in play framework?

I am working with Play framework with JPA to store database but some issue is coming :

what am i doing is i am storing user into database but exception is coming.

My controller is:

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");
        }

    }

}

Model is:

@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);

    }

and Exception is:

[RollbackException: Error while committing the transaction] 

terminal screen is :

[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]

can any one please give me idea to fix this issue and is it necessary to create sql file before run the app

give some idea!

persistence.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: u can see on this link:

application.conf

it is creating database but it is not creating evolution file in project.

Upvotes: 2

Views: 7220

Answers (1)

mguillermin
mguillermin

Reputation: 4181

To enable schema auto-creation / auto-update, you need to edit your persistence unit configuration file (usually conf/META-INF/persistence.xml).

You need to set the desired value to the property hibernate.hbm2ddl.auto (look at the Hibernate documentation for the available values). You can start with value update, that will make Hibernate create the schema if it doesn't exist and update it if it doesn't match your entities.

Ex :

<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>

Upvotes: 0

Related Questions