akk*_*kku 2 java many-to-many playframework ebean
我正在尝试使用多对多方法产品与商店 Product.java具有多对多关系
package models;
@Entity
public class Product extends Model {
@Id
@SequenceGenerator(name="product_gen", sequenceName="product_id_seq", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="product_gen")
@Column(name="id")
public Long id;
@Required
public String name;
@Required
public Float price;
@ManyToMany(cascade = CascadeType.ALL)
public List<Shop> shops = new ArrayList<Shop>();
public Product(String name, float price) {
this.name = name;
this.price = price;
}
public static List<Product> all(){
return find.all();
}
public static Model.Finder<Long, Product> find = new Model.Finder(Long.class, Product.class);
public static Product create(String name,float price) {
Product product = new Product(name, price);
product.save();
product.saveManyToManyAssociations("shops");
return product;
}
}
Run Code Online (Sandbox Code Playgroud)
play 创建了一个表product_shop,其中有shop_id 和product_id 作为外键,但无法在添加产品时在product_shop 表中存储任何值,如有任何帮助,我们将不胜感激。
我有Apps和Users。每个应用程序可以有多个用户,每个用户可以使用多个应用程序。
在你的情况下,你可以替换App为Product和UserShop
这是我在 Play 下的工作代码!2.2.2
DB的演变:
create table app_user (
id varchar(40) not null,
constraint pk_fb_user primary key (id)
);
create table app (
id varchar(40) not null,
name text,
constraint pk_app_id primary key (id)
);
create table membership (
app_id varchar(40) not null,
app_user_id varchar(40) not null,
constraint fk_membership_app_id foreign key (app_id) references app,
constraint fk_membership_app_user_id foreign key (app_user_id) references app_user
);
Run Code Online (Sandbox Code Playgroud)
模型应用程序
@Entity
public class App extends Model {
@Id
public UUID id;
@Column
public String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "membership")
public List<User> users;
....
}
Run Code Online (Sandbox Code Playgroud)
模范用户
@Entity
@Table(name = "app_user")
// user is reserved keyword in DB
public class User extends Model {
@Id
public UUID id;
}
Run Code Online (Sandbox Code Playgroud)
我不需要访问用户的应用程序,因此用户中没有应用程序字段。