2015-02-09 191 views
0

我想爲簡單的產品頁面做一個CRUD表單。我有一個奇怪的問題,每當我嘗試插入新產品時,我總是會更新舊產品,因此總會有一種產品在每次保存時都會更新。Play2 MongoDb新插入更新記錄

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); 
     } 
    } 

} 

我不知道這是否是因爲表單聲明爲靜態或表單doe不會在某個地方被清除。一雙額外的眼睛在這裏會有很大的幫助。感謝您的時間

+0

什麼是你的數據庫中更新此對象的ID? – marcinn 2015-02-09 14:42:20

+0

> db.product.find() {「_id」:NumberLong(0),「title」:「Potato」,「pricing」:{「cost」:10,「price」:20,「promoPrice」:10 ,「儲蓄」:10,「onSale」:true}} – 2015-02-09 15:11:09

+0

@marcinn它不斷地更新這個對象,而不是做一個新的插入 – 2015-02-09 15:11:38

回答

0

嘗試

@Id 
    @ObjectId 
    public String id; 

加入@ObjectId,並鍵入String .Seems爲指向的@marcinn

docs

如果該ID是不能產生服務器首先收到一個沒有_id字段 的文檔,然後服務器將該字段移到開頭。

+0

謝謝,但來自一個關係數據庫學派的思想,我是在'_id'應該總是長而不是字符串的假設下......?http://docs.mongodb.org/manual/tutorial/create-一個自動遞增字段/也表明相同? – 2015-02-09 23:54:21

+0

@ g0c00l.g33k我的解決方案也可以解決您的問題 – silentprogrammer 2015-02-10 04:50:18

+0

@singakash nope這似乎也沒有幫助 – 2015-02-10 09:34:09