ActiveJdbc 更新 join-table 属性

ActiveJdbc update join-table attribute

晚上好, 我正在尝试更新现有记录的 join-table 属性。我有两个模型,一个叫 Records,另一个叫 Shoppingcarts。它们通过名为 records_shoppingcarts 的联接 table 连接。我向名为 record_amount 的连接 table 添加了一个关系属性,它应该保存连接 table 中每条记录的数量。 目前,每当将一件商品添加到购物车时,只会添加一件商品。然后,用户可以在结帐视图中修改购物车中的商品数量。我通过查询联接 table records_shoppingcarts 以查找具有相应 record_id 的条目并将列 record_amount 更新为购物车中的新记录数来处理此问题。这看起来像是链接:

List<RecordsShoppingcarts> associations = RecordsShoppingcarts.where("shoppingcart_id = ?", s.get("id"));

            //update the number of records in the join table
            for(RecordsShoppingcarts rs : associations) {                    

                if((int)rs.getInteger("record_id") == (int)rec.getInteger("id")) {
                    rs.delete(); //cleanup
                    rs.set("record_amount", amount);
                    rs.saveIt();                        

                }
            }                
        }

不幸的是,每当 saveIt() 被调用时;该条目未在数据库中更新,但使用相同的 shoppingcart_idrecordId 和更新后的金额创建了一个新条目。我可能在这里遗漏了一些东西,因为我无法想象手动更新 join tables 是有意的。不幸的是,我在 join tables 中找不到任何关于更新关系属性的信息。这甚至是由 ActiveJdbc 提供的吗?

如有任何帮助,我们将不胜感激。 最良好的祝愿,derelektrischemoench

该框架完全按预期工作。您需要通过阅读此页面来熟悉它如何决定创建新记录或更新现有记录:http://javalite.io/surrogate_primary_keys

基本上,您要通过以下方式删除记录:

 rs.delete();

所以,下次你这样做:

rs.saveIt();   

它创造了一个新记录。只需删除 rs.delete(); 就可以了。