[英]Many-to-many relationship in play framework java
I am trying to use many to many approach product has many to many relation with shop Product.java 我正在尝试使用多对多方法产品与商店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;
}
}
play created a table product_shop which has shop_id and product_id as foreign key but not able to store any value in product_shop table on adding a product any help would be appreciated. 播放创建了一个表product_shop,该表具有shop_id和product_id作为外键,但是在添加产品时无法在product_shop表中存储任何值,将不胜感激。
I have Apps
and Users
. 我有
Apps
和Users
。 Each app can have several Users, each User can use several Apps. 每个应用程序可以有多个用户,每个用户可以使用多个应用程序。
In your case, you can replace App
with Product
and User
with Shop
在您的情况下,您可以将
App
替换为Product
,将User
替换为Shop
Here is my working code under Play! 这是我在Play下的工作代码! 2.2.2
2.2.2
Evolutions of 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
);
Model App 模型应用
@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;
....
}
Model User 模型使用者
@Entity
@Table(name = "app_user")
// user is reserved keyword in DB
public class User extends Model {
@Id
public UUID id;
}
I don't need to access Apps of a User so I didn't have an App field in User. 我不需要访问用户的应用程序,因此用户中没有“应用程序”字段。
Try looking into an embedded id. 尝试查看嵌入式ID。
Make a class for your primary key 为您的主键上课
@Embeddable
public class ProductShopKey {
public Long productId;
public Long shopId;
public boolean equals(Object object) {
if(object isInstanceOf ProductShopKey){
ProductShopKey key = (ProductShopKey) object
if(this.productId == key.productId
&& this.shopId == key.shopId
){
return true;
}
}
return false;
}
public int hashCode() {
return productId.hashCode() + shopId.hashCode();
}
}
Then make an entity for the class 然后为课程创建一个实体
@entity
public class ProductShop {
@EmbeddedId
public ProductShopKey psKey = new ProductShopKey();
@ManyToOne
@JoinColumn(name = "product_id")
public Product product;
@ManyToOne
@JoinColumn(name = "shop_id")
public Shop shop;
//Extra value to go in your join table
public String someValue;
...
}
Then Shop
and Product
each have a one to many relationship with ProductShop
然后
Shop
和Product
与ProductShop
都有一对多的关系
Inserting entries into the table is done manually 将条目插入表中是手动完成的
ProductShop ps = new ProductShop();
ps.psKey.productId = someProductId;
ps.psKey.shopId = someShopId;
ps.someValue = someValue;
ps.save();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.