玩框架 1.3.3 和休眠缓存

play framework 1.3.3 and hibernate cache

项目正在使用 play framework 1.3.3 我有一个这样的控制器:

public static void save(Item item) {
    if (item.id != null) { //It means that item is not new, it is being edited
        Item existingOldItem = Item.findById(item.id);
        //Here I should have an old version of an item as "existingOldItem"
        //and new one coming from http request as "item"
    }

但问题是 item 和 existingOldItem 非常相同。 Item.findById 行不是 return 数据库中的旧项目,而是 return 来自 http 请求的新项目(与 JPA.em().createQuery 相同)。我想 play 框架会在缓存中发送一个新项目,然后 findById returns 来自缓存的项目,而不是来自数据库的项目。拜托,谁能解释一下背后的逻辑以及解决问题的方法。

the problem is item and existingOldItem are very identical. Item.findById line doesn't return me an old item from database

这是预期的行为。 Item.findById() 正在返回由 HTTP 客户端修改的旧项目。从 Play 看看 JPA object binding!查看调用模式的文档。

Please, could someone explain me the logic behind it...

简而言之,HTTP 客户端应在 HTTP 请求中提供项目记录的 ID 及其新的 属性 值。玩!设置 item 参数准备保存,在数据库中查找项目并根据 POST 参数修改其属性。所以你不应该有一个 "old" 和一个 "new" 项目,你应该有一个 "item" 对象可能是旧的也可能是新的,取决于是否找到了 HTTP 客户端提供的 ID 在数据库中。你所有的控制器动作必须做的就是调用 item.save().

魔法就在JPAPlugin.bind。如您所料,它首先在数据库中查找对象。 如果找到该对象,则它会使用该对象调用 Item.edit()。这也是魔术和 默认实现设置 Item 的所有属性,其中有匹配的 HTTP 参数。

... and the ways how to solve the problem.

我不确定您认为问题出在哪里,但是如果您想要旧的和新的物品,那么客户不应该提供 item.id 参数。如果你不喜欢那个玩!深入您的数据库以实例化 item 参数,然后您可以提供自定义 活页夹或让控制器操作接受外观相似的 POJO class,而不是 JPA class.