Play Framework Ebean JoinColumn错误

时间:2017-04-08 09:04:58

标签: java mysql playframework ebean

我使用Java Play Framework和ebean连接Mysql,我使用JoinColumn,数据重复,为什么???

Category.java

package models;

import com.avaje.ebean.FetchConfig;
import com.avaje.ebean.Model;
import com.avaje.ebean.annotation.PrivateOwned;
import sun.rmi.runtime.Log;

import javax.persistence.*;
import java.util.*;

@Table(name="category")
@Entity
public class Category extends Model {

@Id
@Column(name = "category_id")
public Long category_id;

@Column(name = "name")
public String name;

public byte status;

public Long sort_order;

public Long parent_id;



@JoinColumn(name = "category_id")
@OneToMany(cascade = CascadeType.ALL)
public List<ProductToCategory> products;

/**
 * Generic query helper for entity Category with id Long
 */
public static Find<Long,Category> find = new Find<Long,Category>(){};

public List<Category> list(){

    List<Category> category = Category.find
            .fetch("products")
            .fetch("products.product")
            .where()
            .eq("status",1)
            //.eq("category_id",1)
            .orderBy("sort_order asc")
            .findList();
    return category;
  }
}

ProductToCategory.java     包模型;

import com.avaje.ebean.Model;
import org.springframework.context.annotation.Primary;
import play.data.validation.Constraints;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name="product_to_category")
public class ProductToCategory extends Model{
  @Id
  @Column(name = "category_id")
  public Long category_id;

  @EmbeddedId
  public Long product_id;

  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "product_id")
  public Product product;

}

我的表

CREATE TABLE `category` (
  `category_id` int(11) NOT NULL AUTO_INCREMENT,
  `image` varchar(255) DEFAULT NULL,
  `name` varchar(255) NOT NULL DEFAULT '',
  `parent_id` int(11) NOT NULL DEFAULT '0',
  `sort_order` tinyint(1) NOT NULL DEFAULT '0',
  `status` tinyint(1) NOT NULL,
  `date_added` datetime NOT NULL,
  `date_modified` datetime NOT NULL,
 PRIMARY KEY (`category_id`,`status`),
 KEY `parent_id` (`parent_id`)
) ENGINE=InnoDB AUTO_INCREMENT=259 DEFAULT CHARSET=utf8;


CREATE TABLE `product_to_category` (
  `product_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  PRIMARY KEY (`category_id`,`product_id`),
  KEY `category_id` (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

当我使用@OneToMany时,产品数据就像这样

{
    "category_id": 1,
    "name": "分类1",
    "status": 1,
    "sort_order": 0,
    "parent_id": 0,
    "products": [
      {
        "category_id": 1,
        "product_id": 1,
        "product": {
          "product_id": 1,
          "name": "商品1",
          "image": "1.jpg",
          "status": 1,
          "date_available": 1507630210000,
          "date_added": 1507630210000,
          "date_modified": 1507630210000,
          "quantity": 990
        }
      },
      {
        "category_id": 1,
        "product_id": 1,
        "product": {
          "product_id": 1,
          "name": "商品1",
          "image": "1.jpg",
          "status": 1,
          "date_available": 1507630210000,
          "date_added": 1507630210000,
          "date_modified": 1507630210000,
          "quantity": 990
        }
      },
      {
        "category_id": 1,
        "product_id": 1,
        "product": {
          "product_id": 1,
          "name": "商品1",
          "image": "1.jpg",
          "status": 1,
          "date_available": 1507630210000,
          "date_added": 1507630210000,
          "date_modified": 1507630210000,
          "quantity": 990
        }
      }
    ]
  }]

产品数据重复

1 个答案:

答案 0 :(得分:0)

我解决了问题,product_to_category表是一个关系表 this is the solution