I am trying to create a simple application using play Framework 2.5 with postgresql. The idea is to save new entries in a table.
The database contains two tables. A journal is connected to a reference in a manyToOne relationship, so a reference must always have a journal. Both tables have more fields, but in summary they look like this:
CREATE TABLE reference (
reference_id integer NOT NULL,
journal_id integer NOT NULL
title character varying(512));
CREATE TABLE journal (
journal_id integer NOT NULL,
journal_title character varying(128) NOT NULL);
ALTER TABLE ONLY reference ADD CONSTRAINT reference_journal_id_fkey FOREIGN KEY (journal_id)
REFERENCES journal(journal_id) ON UPDATE CASCADE ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED;
I have mapped the tables to Play like this:
Reference.java
import com.avaje.ebean.Model;
import com.avaje.ebean.*;
@Entity
@Table(name="reference")
public class Reference extends Model{
@Id
public Long reference_id;
@ManyToOne
@Constraints.Required
public Journal journal;
@Constraints.Required
public String title;
Journal.java
import com.avaje.ebean.Model;
import com.avaje.ebean.*;
@Entity
@Table(name="journal")
public class Journal extends Model {
@Id
public Long journal_id;
public String journal_title;
@OneToMany(mappedBy = "journal")
public List<Reference> journal_references = new ArrayList<Reference>();
public static Find<Long,Journal> find = new Find<Long,Journal>(){};
public static Map<String,String> options() {
LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
for(Journal j: Journal.find.orderBy("journal_title").findList()) {
options.put(j.journal_id.toString(), j.journal_title);
}
return options;
}
plugins.sbt
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.4")
addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "3.0.1")
Build.sbt
lazy val root = (project in file(".")).enablePlugins(PlayJava)
lazy val myProject = (project in file(".")).enablePlugins(PlayJava, PlayEbean)
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
"com.adrianhurt" %% "play-bootstrap" % "1.0-P25-B3",
"org.hibernate" % "hibernate-entitymanager" % "4.3.8.Final"
)
libraryDependencies += "postgresql" % "postgresql" % "9.1-901-1.jdbc4"
routesGenerator := InjectedRoutesGenerator
fork in run := true
The view is created correctly, it shows the proper fields and a select with the journal names from the Journal table.
create.scala.html
@b3.form(routes.Reference.save()) {
<fieldset>
@b3.text(referenceForm("title"), '_label -> "Title", '_help -> "")
@b3.select(
journal_referenceForm("journal.journal_id"),
options(Journal.options),
'_label -> "Journal:",
'_default -> "-- Choose a Journal --",
'_showConstraints -> false,
'multiple -> false ,
'_help -> ""
)
@b3.free(){
<input type="submit" value="Save the Reference" class="btn btn-primary"> or
<a href="@routes.HomeController.index()" class="btn">Cancel</a>
}
</fieldset>
}
When running the application I get the message "Database 'default' needs evolution! An SQL script will be run on your database -". The problem is that the field journal_id is renamed to journal_journal_id in the script.
Create table core.reference (
journal_reference_id bigserial not null,
journal_journal_id bigint,
title varchar(255)
constraint pk_journal_reference primary key (journal_reference_id));
And of course, when trying to save a new entry, I get the error that the field journal_journal_id doesn't exist in the table reference. I have checked the names in the database and the models and they are correct. I have also regenerated the script many times without success. I have no much experience with play so I have no idea what the source of the error could be.
Thanks in advance ! Any help is greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire