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