【问题标题】:Ebean One to Many Mapping not working.Ebean 一对多映射不起作用。
【发布时间】:2014-12-10 06:30:21
【问题描述】:

我正在编写一个包含产品、客户、信用卡和购物车的网络应用程序。客户在信用卡和购物车上都有一对多的映射。在我的客户类中,我有一个列表和列表。当我访问 customer.creditCardList 时,一切正常。当我为 customer.shoppingCartList 尝试相同的操作时,我收到此错误。

error] Test models.ModelsTest.createAndRetrieveCart failed: javax.persistence.PersistenceException:
ERROR executing DML bindLog[] error[Field 'customer_id' doesn't have a default value]

在我没有收到任何错误之前,我的 shoppingCartList 始终为空,即使数据库中有商品。

这是我抛出错误的测试函数:

@测试

public void createAndRetrieveCart(){
    Customer walter = Customer.find.where().eq("email", "test@banananow.com").findUnique();
    Product p1 = Product.find.where().idEq(1).findUnique();
    Product p2 = Product.find.where().idEq(2).findUnique();

    new ShoppingCart(walter, p1, 2).save();
    new ShoppingCart(walter, p2, 3).save();

    if(walter.shoppingCartList.size() < 2){
        assert(false);
    }
    for(ShoppingCart cart : walter.shoppingCartList){
        assertNotNull(cart.product.name);
        assertNotNull(cart.product.price);
        System.out.println(cart.product.name);
        System.out.println(cart.product.price);
        System.out.println(cart.quantity);

    }
}

错误发生在第 123 行,即第一个 ShoppingCart.save() 奇怪的部分与 creditCardList 通行证的代码几乎相同,并且工作正常。这是那个测试

public void createAndAddCreditCard(){
        Customer walter = Customer.find.where().eq("email", "tester@test.com").findUnique();
        //Date date = new Date(2017, 10, 1);
        //new CreditCard(1234567812345679L, walter, "walter wooodall", date, 21919, 123).save();
        for(CreditCard card : walter.creditCardList){
           System.out.println(card.number);
        }
    }

这是我的模型:

客户:

@Entity
public class Customer extends Model {

    @Id
    public int id;
    public String email;
    public String password;
    public String name;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="address_id")
    public Address address;
    public double balance;
    @OneToMany(mappedBy = "customer")
    public List<CreditCard> creditCardList;
    @OneToMany(mappedBy = "customer")
    public List<Basket> basketList;
    @OneToMany(mappedBy = "customer")
    public List<ShoppingCart> shoppingCartList;

    public Customer(String email, String password, String name, Address address){
        this.email = email.toLowerCase();
        this.password = password;
        this.name = name.toLowerCase();
        this.address = address;
        this.balance = 0.0;
        //this.creditCardList = new ArrayList<CreditCard>();
        //this.shoppingCartList = new ArrayList<ShoppingCart>();
    }

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

    public static Customer authenticate(String email, String password) {
        return find.where().eq("email", email)
                .eq("password", password).findUnique();
    }

    public static Customer exists(String email) {
        return find.where().eq("email", email).findUnique();
    }

    /*
    public List<CreditCard> getCreditCardList(){
        return CreditCard.find.where().eq("customer_id", this.id).findList();
    }
    */
}

购物车:

@Entity
@Table(name="shopping_cart")
public class ShoppingCart extends Model{
    @Id
    public int id;
    @ManyToOne
    @JoinColumn(name = "customer_id", insertable = false, updatable = false)
    public Customer customer;
    @ManyToOne
    @JoinColumn(name = "product_id", insertable = false, updatable = false)
    public Product product;
    public int quantity;


    public ShoppingCart(Customer customer, Product product, int quantity){
        this.customer = customer;
        this.product = product;
        this.quantity = quantity;
    }

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


}

产品:

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

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

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

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

    public static List<Product> getTopProducts(){
        String sql = "select * from top_product";
        SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
        List<SqlRow> sqlRows = sqlQuery.findList();
        List<Product> productList = null;
        if(sqlRows != null){
            productList = new LinkedList<Product>();
            for(SqlRow row : sqlRows){
                int id = row.getInteger("id");
                String name = row.getString("name");
                float price = row.getFloat("price");
                String category = row.getString("category");
                String subcategory = row.getString("subcategory");
                String imageUrl = row.getString("image_url");
                String url = row.getString("url");
                String storeId = row.getString("store_id");
                productList.add(new Product(id, name, price, category, subcategory, imageUrl, url, Store.find.byId(storeId)));
            }
        }
        return productList;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", category='" + category + '\'' +
                ", subcategory='" + subcategory + '\'' +
                ", image_url='" + imageUrl + '\'' +
                ", url='" + url + '\'' +
                '}';
    }
}

我的表中有 id、customer_id、product_id 和数量,以及对客户和产品的 FK 引用。

其他人有这个问题并且知道如何解决吗??

【问题讨论】:

标签: orm playframework playframework-2.0 ebean


【解决方案1】:

以下行似乎没有读取客户:

Customer walter = Customer.find.where().eq("email", "test@banananow.com").findUnique();

您的第二个测试运行良好,但您在那里使用不同的电子邮件查询客户。所以尝试将上一行更改为:

Customer walter = Customer.find.where().eq("email", "tester@test.com").findUnique();

第二个问题是你有:

@JoinColumn(name = "customer_id", insertable = false, updatable = false)
@JoinColumn(name = "product_id", insertable = false, updatable = false)

这就是为什么这些关系没有被保存。删除这些行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多