我正在尝试使用play framework 2.5和postgresql创建一个简单的应用程序。我们的想法是在表格中保存新条目。
数据库包含两个表。日志连接到manyToOne关系中的引用,因此引用必须始终具有日志。两个表都有更多字段,但总结如下:
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;
我已经将表映射到Play,就像这样:
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
视图已正确创建,它显示正确的字段,并使用Journal表中的日记名称进行选择。
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>
}
运行应用程序时,我收到消息“数据库'默认'需要进化!SQL脚本将在您的数据库上运行 - ”。问题是字段journal_id在脚本中被重命名为journal_journal_id。
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));
当然,在尝试保存新条目时,我得到的错误是字段journal_journal_id在表引用中不存在。 我检查了数据库和模型中的名称,它们是正确的。我也多次重新编写脚本而没有成功。我没有太多的游戏经验,所以我不知道错误的来源是什么。
提前致谢!非常感谢任何帮助。