2014-12-07 56 views
1

我正在用Basket,Product和Basket_Product表編寫一個play應用程序。購物籃和產品具有存儲在購物籃_產品中的多對多關係。我正在使用Ebean ORM,我得到這個錯誤空指針EmmbedId類hashCode

[error] Test models.ModelsTest.createAndRetrieveOrder failed: java.lang.NullPointerException: null, took 5.128 sec 
[error]  at models.BasketProductPk.hashCode(BasketProductPk.java:43) 
[error]  at java.util.HashMap.hash(HashMap.java:338) 
[error]  at java.util.HashMap.get(HashMap.java:556) 
[error]  at com.avaje.ebeaninternal.server.transaction.DefaultPersistenceContext$ClassContext.putIfAbsent(DefaultPersistenceContext.java:175) 
[error]  at com.avaje.ebeaninternal.server.transaction.DefaultPersistenceContext$ClassContext.access$100(DefaultPersistenceContext.java:148) 
[error]  at com.avaje.ebeaninternal.server.transaction.DefaultPersistenceContext.putIfAbsent(DefaultPersistenceContext.java:56) 
[error]  at com.avaje.ebeaninternal.server.query.SqlTreeNodeBean.load(SqlTreeNodeBean.java:235) 
[error]  at com.avaje.ebeaninternal.server.query.CQuery.readRow(CQuery.java:541) 
[error]  at com.avaje.ebeaninternal.server.query.CQuery.readBeanInternal(CQuery.java:575) 
[error]  at com.avaje.ebeaninternal.server.query.CQuery.hasNextBean(CQuery.java:702) 
[error]  at com.avaje.ebeaninternal.server.query.CQuery.readTheRows(CQuery.java:689) 
[error]  at com.avaje.ebeaninternal.server.query.CQuery.readCollection(CQuery.java:655) 
[error]  at com.avaje.ebeaninternal.server.query.CQueryEngine.findMany(CQueryEngine.java:175) 
[error]  at com.avaje.ebeaninternal.server.query.DefaultOrmQueryEngine.findMany(DefaultOrmQueryEngine.java:77) 
[error]  at com.avaje.ebeaninternal.server.core.OrmQueryRequest.findList(OrmQueryRequest.java:263) 
[error]  at com.avaje.ebeaninternal.server.core.DefaultServer.findList(DefaultServer.java:1502) 
[error]  at com.avaje.ebeaninternal.server.querydefn.DefaultOrmQuery.findList(DefaultOrmQuery.java:890) 
[error]  at com.avaje.ebeaninternal.util.DefaultExpressionList.findList(DefaultExpressionList.java:173) 
[error]  at models.ModelsTest.createAndRetrieveOrder(ModelsTest.java:100) 

這是我的Junit測試,這是拋出錯誤。當我嘗試訪問Basket_Product表時。我檢查了我的數據庫,並且一切都正確插入,我試圖訪問Basket_Product以獲取特定購物籃時仍然收到錯誤。

模型試驗方法

@Test 
public void createAndRetrieveOrder(){ 
    Customer walter = Customer.find.where().eq("email", "[email protected]_now.com").findUnique(); 
    Product p1 = Product.find.where().idEq(1).findUnique(); 
    Product p2 = Product.find.where().idEq(2).findUnique(); 
    Basket b = new Basket(walter); 

    b.save(); 
    new BasketProduct(p1, b, 2).save(); 
    new BasketProduct(p2, b, 2).save(); 

    Basket b1 = Basket.find.where().idEq(8).findUnique(); 

    List<BasketProduct> bplist = BasketProduct.find.where().eq("basket_id", b1.id).findList(); 

    assertNotNull(bplist); 
} 

嵌入式關鍵

@Embeddable 
public class BasketProductPk implements Serializable{ 
    @Column(name = "product_id") 
    private Product product; 
    @Column(name = "basket_id") 
    private Basket basket; 

    public BasketProductPk(Product product, Basket basket) { 
     this.product = product; 
     this.basket = basket; 
    } 
    @Override 
    public boolean equals(Object otherOb) { 
     if (this == otherOb) { 
      return true; 
     } 
     if (!(otherOb instanceof BasketProductPk)) { 
      return false; 
     } 
     BasketProductPk other = (BasketProductPk) otherOb; 
     return (
       (product==null?other.product==null:product.equals 
         (other.product) 
       ) 
         && 
         (basket == other.basket) 
     ); 
    } 
    @Override 
    public int hashCode() { 
     return (
       (product==null?0:product.hashCode()) 
         ^
         (basket.hashCode()) 
     ); 
    } 

} 

籃產品

@Entity 
@Table(name = "basket_product") 
public class BasketProduct extends Model { 
    @EmbeddedId 
    public BasketProductPk id; 

    @ManyToOne(cascade= CascadeType.ALL) 
    @JoinColumn(name="product_id") 
    public Product product; 

    @ManyToOne(cascade=CascadeType.ALL) 
    @JoinColumn(name="basket_id") 
    public Basket basket; 

    public int quantity; 
    public float subtotal; 


    public BasketProduct(Product product, Basket basket, int quantity){ 
     this.id = new BasketProductPk(product, basket); 
     this.product = product; 
     this.basket = basket; 
     this.quantity = quantity; 
     this.subtotal = quantity * product.price; 
    } 
    protected BasketProduct() {} 

    public static Finder<String, BasketProduct> find = new Finder<String, BasketProduct>(String.class, BasketProduct.class); 
    // getters, setters 
} 

產品

@Entity 
public class Product extends Model{ 
    @Id 
    public int id; 
    public String name; 
    public Float price; 
    public String category; 
    public String subcategory; 
    public String image_url; 
    public String url; 
    @ManyToOne 
    private Store store; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "product") 
    private List<BasketProduct> basket_product; 

    public Product(String name, Float price, String category, String subcategory, String image_url, String url, Store store){ 
     this.name = name; 
     this.price = price; 
     this.category = category; 
     this.subcategory = subcategory; 
     this.image_url = image_url; 
     this.url = url; 
     this.store = store; 
    } 

    public static Finder<String, Product> find = new Finder<String, Product>(String.class, Product.class); 

} 

@Entity 
public class Basket extends Model { 
    @Id 
    public int id; 
    @CreatedTimestamp 
    public Timestamp time; 
    int complete; 
    @ManyToOne 
    public Customer customer; 
    @ManyToOne 
    public Employee employee; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "basket") 
    public List<BasketProduct> basket_product; 

    public Basket(Customer customer){ 
     this.customer = customer; 
     this.complete = 0; 
    } 
    public Basket(Customer customer, Employee employee){ 
     this.customer = customer; 
     this.employee = employee; 
    } 

    public static Finder<String, Basket> find = new Finder<String, Basket>(String.class, Basket.class); 

} 
+0

是我的回答有幫助?它有助於你解決你的問題嗎?如果是的話,考慮接受它。如果沒有,請寫下爲什麼它不夠,也許我會提供一些更多的幫助。 – rtruszk 2014-12-08 19:29:03

+0

@rtruszk對不起還沒有修復它。我一直在研究項目的其他部分。我會盡快做到這一點,一定會讓你知道。 – wwoodal1 2014-12-10 01:01:45

回答

1

此錯誤是因爲當BasketProductPk加載然後不加載BasketProduct,他們都是null

我看到在你的hashCode方法必須:

(product==null?0:product.hashCode())^(basket.hashCode()) 

所以,你檢查的產品是零,但你不檢查,如果籃子是空的。所以你的NullPointerException是因爲籃子爲空。

爲了使你不得不在你的代碼的一些改變此代碼的工作: 見這些問題的答案的詳細信息: