[英]In play 2.3.x, differing failures with both ebean and jpa
我遇到數據庫問題。 在下面的控制器中,我嘗試切換具有傳入ID的記錄上的布爾值。 當我使用ebean語法時,我可以查詢,插入和刪除,但不能更新。 好像我無法提交有關更新的事務,但是它們與其他操作一起自動提交。 當我使用JPA語法時,似乎與內存數據庫而不是已配置的postgresql進行交互。 我之所以這樣說,是因為當我查詢時,我會返回null。 但是,如果我使用JPA插入一條記錄,那么我將能夠成功地查詢和更新它。 但是這些更新不會出現在postgres中,加上我查詢的記錄中設置的唯一字段是id和boolean標志。
這是我的代碼和配置。 謝謝!
來自build.sbt
libraryDependencies ++= Seq(
javaJdbc,
javaEbean,
javaJpa,
"org.hibernate" % "hibernate-entitymanager" % "4.3.6.Final",
"play2-crud" %% "play2-crud" % "0.7.4-SNAPSHOT",
cache,
javaWs,
"javax.mail" % "javax.mail-api" % "1.5.2",
"javax.activation" % "activation" % "1.1.1",
"postgresql" % "postgresql" % "9.1-901-1.jdbc4"
)
來自application.conf
# Database configuration
# ~~~~~
db.default.driver=org.postgresql.Driver
db.default.url="postgres://username:password@localhost/playdb"
db.default.autocommit=true
# You can expose this datasource via JNDI if needed (Useful for JPA)
db.default.jndiName=DefaultDS
jpa.default=defaultPersistenceUnit
# Ebean configuration
# ~~~~~
ebean.default="models.*"
來自persistence.xml
org.hibernate.ejb.HibernatePersistence DefaultDS
和我的控制器
// JPA style
@Transactional
public static Result toggleMailboxJpa(Long mailbox)
{
// does not update
MailboxConfig x = JPA.em().find(MailboxConfig.class, mailbox);
x.suspended = !x.suspended;
// transactional appears to work, these have no effect
JPA.em().persist(x);
JPA.em().flush();
return redirect("/admin/mailboxStatus");
}
@Transactional
public static Result toggleMailboxEbean(Long mailbox)
{
MailboxConfig x = MailboxConfig.find.byId(mailbox);
x.suspended = !x.suspended;
x.update();
// this syntax has the same effect (reads from the db fine but does not commit)
/*
MailboxConfig x = Ebean.find(MailboxConfig.class).where().eq("id", mailbox).findUnique();
x.suspended = !x.suspended;
Ebean.save(x);
*/
return redirect("/admin/mailboxStatus");
}
嘗試
x.update(mailbox)
帶有“郵箱”的ID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.