【问题标题】:Play2 MongoDb new insert updates recordPlay2 MongoDb 新插入更新记录
【发布时间】:2015-02-09 14:36:42
【问题描述】:

我正在尝试为一个简单的产品页面制作一个 CRUD 表单。我有一个奇怪的问题,每当我尝试插入新产品时,我总是更新旧产品,因此每次保存时总会有一个产品更新。

我的index.scala.html如下

@main("Product List"){

@inputText(productForm("search"),'_label -> "Search",'id -> "search")

<h3>@products.size() product(s)</h3>

<table class="table table-condensed">
    <thead>
      <tr>
        <th>Title</th>
        <th>Cost</th>
        <th>Price</th>
        <th>Promo Price</th>
        <th>Savings</th>
        <th>On Sale</th>
      </tr>
    </thead>
    <tbody>
    @for(product <- products){
        <tr>
        <td>
            @product.title
        </td>
        <td>
            @product.pricing.cost
        </td>
        <td>
            @product.pricing.price
        </td>
        <td>
            @product.pricing.promoPrice
        </td>
        <td>
            @product.pricing.savings
        </td>
        <td>
            @product.pricing.onSale
        </td>
        <td>    
            @helper.form(action = routes.Application.deleteProduct(product.id)){
                <input type="submit" value="Delete"/>
            }
        </td>
        </tr>
    }
    </tbody>
</table>

<h2>Add new product</h2>
@form(action = routes.Application.newProduct()){
    @inputText(productForm("title"),'_label -> "Title")
    @inputText(productForm("pricing.cost"),'_label -> "Cost")
    @inputText(productForm("pricing.price"),'_label -> "Price")
    @inputText(productForm("pricing.promoPrice"),'_label -> "Promo Price")
    @inputText(productForm("pricing.savings"),'_label -> "Savings")
    @checkbox(productForm("pricing.onSale"),'_label -> "On Sale")
    <input type="submit" class="btn btn-default" value="Add"/>
}
}

我的Application.java如下,

public class Application extends Controller {
....
static Form<Product> productForm = new Form<Product>(Product.class);

public static Result products() {
        return ok(views.html.index.render(Product.all(), productForm));
    }

public static Result newProduct() {
        Form<Product> filledForm = productForm.bindFromRequest();
        if (productForm.hasErrors()) {
            return badRequest(views.html.index
                    .render(Product.all(), filledForm));
        } else {
            Product.create(filledForm.get());
            return redirect(routes.Application.products());
        }
    }
}

而我的模型Product.java如下,

public class Product {


    private static final long serialVersionUID = 94587092799205246L;

    @Id
    public long id;

    @Required
    public String title;

    @Required
    public Pricing pricing;

    public Product(){
    }

    public Product(String title){
        this.title = title;
    }

    private static JacksonDBCollection<Product, Long> products = MongoDB
            .getCollection("product", Product.class, Long.class);

    public static List<Product> all() {
        return Product.products.find().toArray();
    }

    public static void create(Product product) {
        Product.products.save(product);
    }

    public static void delete(Long id) {
        Product p = Product.products.findOneById(id);
        if(p != null){
            Product.products.remove(p);
        }
    }

}

我不确定是因为表单被声明为静态还是表单没有在某处被清除。额外的一双眼睛在这里会有很大的帮助。感谢您的宝贵时间

【问题讨论】:

  • 你的数据库中正在更新的这个对象的 id 是什么?
  • > db.product.find() { "_id" : NumberLong(0), "title" : "Potato", "pricing" : { "cost" : 10, "price" : 20 , "promoPrice" : 10, "savings" : 10, "onSale" : true } }
  • @marcinn 它会一次又一次地更新这个对象,而不是进行新的插入
  • 似乎没有设置 ID - 您反复尝试插入具有相同 ID=0 的元素。这就是你得到错误的原因。看一下@Id 属性,你可以尝试将其定义为 String 吗?

标签: java mongodb playframework insert


【解决方案1】:

试试

  @Id
  @ObjectId
  public String id;

添加@ObjectId 并输入String。似乎无法按照@marcinn 的指示生成id

来自docs

如果服务器接收到的文档没有 _id 字段 首先,服务器会将字段移到开头。

【讨论】:

  • 谢谢,但来自关系数据库学派,我假设_id 应该始终是长而不是字符串...?docs.mongodb.org/manual/tutorial/… 也建议相同?
  • @g0c00l.g33k 我的解决方案也解决了您的问题
  • @singakash 不,这似乎也没有帮助
猜你喜欢
  • 1970-01-01
  • 2013-02-06
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
相关资源
最近更新 更多