Orm Ebean一对多映射不起作用。
我正在编写一个包含产品、客户、信用卡和购物车的web应用程序。客户在信用卡和购物车上都有一对多映射。在我的客户类中,我有一个列表和一个列表。当我访问customer.creditCardList时,一切正常。当我对customer.shoppingCartList尝试同样的方法时,我得到了这个错误Orm Ebean一对多映射不起作用。,orm,playframework,playframework-2.0,ebean,Orm,Playframework,Playframework 2.0,Ebean,我正在编写一个包含产品、客户、信用卡和购物车的web应用程序。客户在信用卡和购物车上都有一对多映射。在我的客户类中,我有一个列表和一个列表。当我访问customer.creditCardList时,一切正常。当我对customer.shoppingCartList尝试同样的方法时,我得到了这个错误 error] Test models.ModelsTest.createAndRetrieveCart failed: javax.persistence.PersistenceException:
error] Test models.ModelsTest.createAndRetrieveCart failed: javax.persistence.PersistenceException:
ERROR executing DML bindLog[] error[Field 'customer_id' doesn't have a default value]
之前我没有收到任何错误,但我的shoppingCartList始终为空,即使数据库中有项目
下面是抛出错误的测试函数:
@试验
以下是我的模型:
客户:
@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();
}
*/
}
@实体
公共类客户扩展模型{
@身份证
公共int id;
公共字符串电子邮件;
公共字符串密码;
公共字符串名称;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“address\u id”)
公共广播;
公共双平衡;
@OneToMany(mappedBy=“客户”)
公共信用卡名单;
@OneToMany(mappedBy=“客户”)
公众名单篮名单;
@OneToMany(mappedBy=“客户”)
公众购物清单;
公共客户(字符串电子邮件、字符串密码、字符串名称、地址){
this.email=email.toLowerCase();
this.password=密码;
this.name=name.toLowerCase();
this.address=地址;
此值为0.0;
//this.creditCardList=新的ArrayList();
//this.shoppingCartList=new ArrayList();
}
公共静态查找器find=新查找器(String.class、Customer.class);
公共静态客户身份验证(字符串电子邮件、字符串密码){
返回find.where().eq(“电子邮件”,电子邮件)
.eq(“密码”,password).findUnique();
}
存在公共静态客户(字符串电子邮件){
返回find.where().eq(“email”,email).findUnique();
}
/*
公共列表getCreditCardList(){
返回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);
}
@实体
@表(name=“购物车”)
公共类ShoppingCart扩展模型{
@身份证
公共int id;
@许多酮
@JoinColumn(name=“customer\u id”,insertable=false,updateable=false)
公众客户;
@许多酮
@JoinColumn(name=“product_id”,insertable=false,updateable=false)
公共产品;
公共整数;
公共购物车(客户、产品、整数数量){
this.customer=customer;
本产品=产品;
这个。数量=数量;
}
publicstaticfinder=newfinder(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 + '\'' +
'}';
}
}
@实体
公共类产品扩展模型{
@身份证
公共int id;
公共字符串名称;
公开浮动价格;
公共字符串类别;
公共字符串子类别;
@列(name=“image\u url”)
公共字符串imageUrl;
公共字符串url;
@许多酮
公共商店;
@OneToMany(cascade=CascadeType.ALL,mappedBy=“产品”)
公共产品清单;
@OneToMany(mappedBy=“产品”)
公众购物清单;
公共产品(字符串名称、浮动价格、字符串类别、字符串子类别、字符串图像url、字符串url、商店){
this.name=名称;
这个价格=价格;
this.category=类别;
this.subcategory=子类别;
this.imageUrl=imageUrl;
this.url=url;
this.store=商店;
}
公共产品(int-id、字符串名称、浮动价格、字符串类别、字符串子类别、字符串图像url、字符串url、商店){
this.id=id;
this.name=名称;
这个价格=价格;
this.category=类别;
this.subcategory=子类别;
this.imageUrl=imageUrl;
this.url=url;
this.store=商店;
}
公共静态查找器find=新查找器(String.class、Product.class);
公共静态列表getTopProducts(){
String sql=“从顶级产品中选择*”;
SqlQuery SqlQuery=Ebean.createSqlQuery(sql);
List sqlRows=sqlQuery.findList();
List productList=null;
if(sqlRows!=null){
productList=新的LinkedList();
对于(SqlRow行:sqlRows){
int id=row.getInteger(“id”);
字符串名称=row.getString(“名称”);
浮动价格=row.getFloat(“价格”);
字符串类别=row.getString(“类别”);
String subcategory=row.getString(“subcategory”);
String imageUrl=row.getString(“image_url”);
字符串url=row.getString(“url”);
String storeId=row.getString(“store_id”);
add(新产品(id、名称、价格、类别、子类别、imageUrl、url、Store.find.byId(storeId));
}
}
返回产品列表;
}
@凌驾
公共字符串toString(){
返回“产品{”+
“id=”+id+
“,name=”“+name+”\“””+
“,price=“+价格+
,category=''+category+'\''+
“,子类别=”“+子类别+”\“””+
,image\u url=''+imageUrl+'\''+
“,url=”+url+“\”+
'}';
}
}
我的表中有id、customer\u id、product\u id和quantity,FK引用了customer和product
其他任何人都有这个问题,并且知道如何解决???下面的一行似乎没有读到客户:
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)
这就是为什么这些关系没有得到保存。删除这些行。看看这个
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)