简体   繁体   English

在游戏2.3.x中,ebean和jpa的失败不同

[英]In play 2.3.x, differing failures with both ebean and jpa

I am having database woes. 我遇到数据库问题。 In the controller below I am trying to toggle a boolean on a record with a passed-in ID. 在下面的控制器中,我尝试切换具有传入ID的记录上的布尔值。 When I use ebean syntax I can query, insert, and delete but not update. 当我使用ebean语法时,我可以查询,插入和删除,但不能更新。 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. 当我使用JPA语法时,似乎与内存数据库而不是已配置的postgresql进行交互。 I say that because when I query I get null back. 我之所以这样说,是因为当我查询时,我会返回null。 But if I insert a record using JPA I will subsequently be able to query and update it successfully. 但是,如果我使用JPA插入一条记录,那么我将能够成功地查询和更新它。 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. 但是这些更新不会出现在postgres中,加上我查询的记录中设置的唯一字段是id和boolean标志。

Here is my code and config. 这是我的代码和配置。 Thank you! 谢谢!

from build.sbt 来自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 来自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 来自persistence.xml

org.hibernate.ejb.HibernatePersistence DefaultDS 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 带有“邮箱”的ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM