I am having database woes. In the controller below I am trying to toggle a boolean on a record with a passed-in ID. When I use ebean syntax I can query, insert, and delete but not update. It's as if I am unable to commit my transactions on updates but they get auto-committed with the other operations. When I use JPA syntax it appears that I am interacting with an in-memory database rather than the configured postgresql. I say that because when I query I get null back. But if I insert a record using JPA I will subsequently be able to query and update it successfully. But those updates do not appear in postgres plus the only fields that are set on the record I have queried are the id and the boolean flag.
Here is my code and config. Thank you!
from 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"
)
from 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.*"
from persistence.xml
org.hibernate.ejb.HibernatePersistence DefaultDS
and from my controller
// 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");
}
Try
x.update(mailbox)
with "mailbox" the id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.