java - 玩框架Ebean JoinColumn错误

标签 java mysql playframework ebean

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

类别.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;

}

我的 table

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
        }
      }
    ]
  }]

产品数据重复

最佳答案

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

关于java - 玩框架Ebean JoinColumn错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43292143/

相关文章:

java - 在 JAVA 世界中是否有等同于 ASP.NET WEB API 的东西?

Python 到 SQL : list of lists

mysql - Node-Mysql保存数据出错

java - 找不到处理 Intent 应用程序设置的 Activity

java - Alfresco 共享登录问题

mysql - 按两列中的最大值排序

java - 使用playframework删除,更新mysql中的单个记录

java - circleCI 中的 "$ play test"下拉菜单是什么?

scala - 如何配置 Play!与 ScalaQuery 和 H2 一起使用的框架?

java - 安卓测试 : Can I send sms to the emulator number from test method?