Play 2.7 strange behaviour when creating "User" model

I can’t use save() method when I create a model named User. However when I create it’s duplicate, but with a different class name, let’s say Product, it can be used. I’m not really sure where is the problem came from, I have stucked solving this issue for hours.

I use the basic Play application template, generated by running

sbt new playframework/play-java-seed.g8

Tools being used

addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "5.0.2")
def scalaVersion = System.getProperty("scala.binary.version", /* default = */ "2.13")
def playVersion = "2.7.3"
scalaVersion := "2.13.0"
openjdk version "11.0.4" 2019-07-16
psql (PostgreSQL) 10.9 (Ubuntu 10.9-1.pgdg18.04+1)
libraryDependencies += "org.postgresql" % "postgresql" % "42.2.6"
libraryDependencies += "org.glassfish.jaxb" % "jaxb-core" % "2.3.0.1"
libraryDependencies += "org.glassfish.jaxb" % "jaxb-runtime" % "2.3.2"

Below are the code that I have modified

conf/application.conf

# This is the main configuration file for the application.
# https://www.playframework.com/documentation/latest/ConfigFile

db {
    default {
        driver=org.postgresql.Driver
        url="jdbc:postgresql://localhost:5432/sea_pay"
        # schema=test_schema
        username="postgres"
        password="123"
        logStatements=true
    }
}

# You can regenerate the secret key by running
# 
# ```
# $ sbt
# [seapay] $ playGenerateSecret
# Generated new secret: SECRET_KEY
# ```
play.http.secret.key = "6_I4RKeAF9ptV`X>yRMDburIn222wHE4=?oFilR@knD`90iERh]Je98vK?QF>CFP"

ebean.default = ["models.*"]

project/plugins.sbt

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "5.0.2")

// Defines scaffolding (found under .g8 folder)
// http://www.foundweekends.org/giter8/scaffolding.html
// sbt "g8Scaffold form"
addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.11.0")

build.sbt

name := """SEAPay"""
organization := "com.seapay"

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)

scalaVersion := "2.13.0"

libraryDependencies += guice
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
// https://mvnrepository.com/artifact/org.postgresql/postgresql
libraryDependencies += "org.postgresql" % "postgresql" % "42.2.6"
// https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api
// libraryDependencies += "javax.xml.bind" % "jaxb-api" % "2.3.1"
libraryDependencies += "org.glassfish.jaxb" % "jaxb-core" % "2.3.0.1"
libraryDependencies += "org.glassfish.jaxb" % "jaxb-runtime" % "2.3.2"

app/models/Product.java

package models;

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

import io.ebean.*;
import play.data.format.*;
import play.data.validation.*;

@Entity
public class Product extends Model {
    @Id
    public int id;
    public String name;
}

app/models/User.java

package models;

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

import io.ebean.*;
import play.data.format.*;
import play.data.validation.*;

@Entity
public class User extends Model {
    @Id
    public int id;
    public String name;
}

app/controllers/HomeController.java

package controllers;

import play.mvc.*;
import models.User;
import models.Product;

public class HomeController extends Controller {
    public Result index() {
        // Uncomment the code below to see the error
        // User user = new User();
        // user.name = "Ok";
        // user.save();
        Product prod = new Product();
        prod.name = "Ok";
        prod.save();
        return ok(views.html.index.render());
    }

}

Hey @andraantariksa,

What happens? A compilation error? Or is there an exception? Or nothing is persisted into the database?

Best.

Sorry, I forgot to put the error log. Previously, it throws .save() method not found (I have make sure that I extends the io.ebean.Model class), but now when I try to run it again, the error changed :/ to this (I’m not changing anything)

[error] application - 

! @7clab7cl9 - Internal server error, for (GET) [/] ->
 
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[PersistenceException: Error[ERROR: syntax error at or near "user"   Position: 13]]]
	at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:351)
	at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:267)
	at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:448)
	at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:446)
	at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:453)
	at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
	at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:92)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
	at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:94)
	at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:92)
Caused by: javax.persistence.PersistenceException: Error[ERROR: syntax error at or near "user"   Position: 13]
	at io.ebean.config.dbplatform.SqlCodeTranslator.translate(SqlCodeTranslator.java:52)
	at io.ebean.config.dbplatform.DatabasePlatform.translate(DatabasePlatform.java:219)
	at io.ebeaninternal.server.persist.dml.DmlBeanPersister.execute(DmlBeanPersister.java:83)
	at io.ebeaninternal.server.persist.dml.DmlBeanPersister.insert(DmlBeanPersister.java:49)
	at io.ebeaninternal.server.core.PersistRequestBean.executeInsert(PersistRequestBean.java:1280)
	at io.ebeaninternal.server.core.PersistRequestBean.executeNow(PersistRequestBean.java:790)
	at io.ebeaninternal.server.core.PersistRequestBean.executeNoBatch(PersistRequestBean.java:845)
	at io.ebeaninternal.server.core.PersistRequestBean.executeOrQueue(PersistRequestBean.java:836)
	at io.ebeaninternal.server.persist.DefaultPersister.insert(DefaultPersister.java:507)
	at io.ebeaninternal.server.persist.DefaultPersister.insert(DefaultPersister.java:455)
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "user"
  Position: 13
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2468)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2211)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:309)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:446)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:370)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:149)
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:124)
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
	at io.ebeaninternal.server.type.DataBind.executeUpdate(DataBind.java:92)

Oh my god, it was caused by this

Cannot create a database table named ‘user’ in PostgreSQL

Ebean generate a create table query without double quoting the table name, since user is a reversed keyword in PostgreSQL, the table won’t be created.

1 Like