insert/update 使用 java 条件 api 与另一个实体具有多对多关系的实体

insert/update an entity that has a many to many relationship with another entity using java criteria api

正如问题所说。我有两个 tables Productscategories.
产品table

...
create table products (
  id int primary key generated always as identity(start with 100, increment by 1),
  name varchar(30),
  quantity int,
  unit_cost decimal not null,
  brand_name varchar(30)
)
...  

类别table

...
create table categories
(
  name varchar(30) primary key,
  description varchar(30),
  date_created date,
)

... 他们之间的关系是多对多关系,所以我创建了一个关系table。

关系table

...
create table product_categories
(
  product_id int not null,
  category_name varchar(30) not null
)
alter table product_categories add constraint product_categoriesFK foreign key (product_id)
references products (id)
alter table product_categories add constraint category_productsFK foreign key (category_name)
references categories (name)  

这些是各自的实体 class。

产品实体

@Entity
@Table(name = "PRODUCTS")
public class Product implements Serializable {
     private static final long serialVersionUID = 1L;
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Basic(optional = false)
     @Column(name = "ID")
     private Integer id;
     @Size(max = 255)
     @Column(name = "NAME")
     private String name;
     @Column(name = "QUANTITY")
     private Integer quantity;
     @Column(name = "UNIT_COST")
     private Integer unitCost;
     @JoinTable(name = "REQUISITION_PRODUCTS", joinColumns = {
          @JoinColumn(name = "PRODUCT_ID", referencedColumnName = "ID")}, inverseJoinColumns = {
     @JoinColumn(name = "REQUISITION_ID", referencedColumnName = "ID")})
     @ManyToMany
     private Collection<Requisition> requisitionCollection;
     @ManyToMany(mappedBy = "productCollection", cascade = CascadeType.ALL)
     private Collection<Category> categoryCollection;
     @JoinColumn(name = "BRAND_NAME", referencedColumnName = "NAME")
     @ManyToOne
     private Brand brand;
     @JoinColumn(name = "DEAL_ID", referencedColumnName = "ID")
     @ManyToOne
     private Deal dealId;
     @OneToMany(mappedBy = "product")
     private Collection<Feature> featureCollection;
     ...

类别实体

@Entity
@Table(name = "CATEGORIES")
public class Category implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "NAME")
    private String name;
    @Column(name = "DATE_CREATED")
    @Temporal(TemporalType.DATE)
    private LocalDate dateCreated;
    @Size(max = 255)
    @Column(name = "DESCRIPTION")
    private String description;
    @JoinTable(name = "PRODUCT_CATEGORIES", joinColumns = {
    @JoinColumn(name = "CATEGORY_NAME", referencedColumnName = "NAME")}, inverseJoinColumns = {
    @JoinColumn(name = "PRODUCT_ID", referencedColumnName = "ID")})
    @ManyToMany
    private Collection<Product> productCollection;
    ...

这些实体 classes 是使用 netbeans ide 生成的。实体的 table 关系未创建,所以我不知道如何 insert/update Categories Product 属于.
这是我执行 insert/update 的代码。

@Override
public int update(Object value, Product t) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaUpdate<Product> cu = cb.createCriteriaUpdate(Product.class);

    cu.set(Product_.brand, t.getBrand());
    cu.set(Product_.id, t.getId());
    cu.set(Product_.name, t.getName());
    cu.set(Product_.quantity, t.getQuantity());
    cu.set(Product_.unitCost, t.getUnitCost());

     // cu.set(Product_.categoryCollection, t.getCategoryCollection());

    Root<Product> root = cu.getRoot();
    Predicate tester = cb.equal(root.get(Product_.id), value);
    cu.where(tester);

    Query q = getEntityManager().createQuery(cu);
    return q.executeUpdate();
} 

产品元模型class

...
public class Product_ {
     public static volatile SingularAttribute<Product, Integer> quantity;
     public static volatile CollectionAttribute<Product, Requisition> requisitionCollection;
     public static volatile CollectionAttribute<Product, Category> categoryCollection;
     public static volatile SingularAttribute<Product, Deal> dealId;
     public static volatile SingularAttribute<Product, Integer> unitCost;
     public static volatile SingularAttribute<Product, String> name;
     public static volatile CollectionAttribute<Product, Feature> featureCollection;
     public static volatile SingularAttribute<Product, Integer> id;
     public static volatile SingularAttribute<Product, Brand>    brand;
     ...

据我所知,元模型 class 需要一个类别而不是类别的集合。请有人告诉我如何解决这个问题。
软件
网豆 8.0.2
glassfish 服务器 4.1
jdk1.8.0_40

当您拥有完整记录时,您不应该使用 CriteriaUpdate 通过 id 进行简单更新。只需使用

getEntiyManager().merge(t);