我有数据库问题。在下面的控制器中,我试图在带有传入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");
}
答案 0 :(得分:0)
尝试
x.update(mailbox)
使用“mailbox”id