Java Play2 MongoDb新插入更新记录

Java Play2 MongoDb新插入更新记录,java,mongodb,playframework,insert,Java,Mongodb,Playframework,Insert,我正在尝试为一个简单的产品页面制作一个CRUD表单。我遇到了一个奇怪的问题,每当我尝试插入一个新产品时,我总是更新旧产品,因此每次保存时都会更新一个产品 我的index.scala.html如下 @main("Product List"){ @inputText(productForm("search"),'_label -> "Search",'id -> "search") <h3>@products.size() product(s)</h3> &

我正在尝试为一个简单的产品页面制作一个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"/>
}
}
我的模型
Product.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());
        }
    }
}
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);
        }
    }

}
公共类产品{
私有静态最终长serialVersionUID=94587092799205246L;
@身份证
公共长id;
@必需的
公共字符串标题;
@必需的
公开定价;
公共产品(){
}
公共产品(字符串标题){
this.title=标题;
}
私有静态JacksonDBCollection产品=MongoDB
.getCollection(“产品”、产品类、长类);
公共静态列表所有(){
return Product.products.find().toArray();
}
公共静态无效创建(产品){
产品。产品。保存(产品);
}
公共静态无效删除(长id){
产品p=产品.products.findOneById(id);
如果(p!=null){
products.products.remove(p);
}
}
}
我不确定这是因为表单被声明为静态的还是表单没有被清除。在这里多看一双眼睛会很有帮助的。谢谢您抽出时间

试试看

  @Id
  @ObjectId
  public String id;
添加
@ObjectId
和键入
字符串
。似乎无法生成@marcinn所指出的id

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


您的db中正在更新的此对象的id是什么?>db.product.find(){“_id”:NumberLong(0),“title”:“Potato”,“pricing”:{“cost”:10,“price”:20,“promoPrice”:10,“savings”:10,“onSale”:true}@marcinn它会一次又一次地更新这个对象,而不是创建一个新的insertIt。它似乎没有设置ID—您反复尝试插入ID为0的元素。这就是为什么你会出错。看看@Id属性,你能试着把它定义为字符串吗?谢谢,但是我来自一个关系数据库学派,我的想法是,
\u Id
应该总是长的,而不是字符串。。。?还建议使用相同的?@g0c00l.g33k我的解决方案解决了您的问题problem@singakash不,那似乎也没用