0

I use Java Play Framework and ebean to connent Mysql,I use JoinColumn,Data is repeat,Why???

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 package models;

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;

}

My 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;

when i use @OneToMany,the products data is error like this

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

the products data repeat

lxg
  • 215
  • 3
  • 9
  • Your mapping is all wrong. You should do something like [this](http://stackoverflow.com/questions/21202490/ebean-query-by-onetomany-relationship). If you want to make the bridge table have a compounded id you have to do something like [this](http://stackoverflow.com/questions/25057752/ebean-embeddedid-mapping-column-to-manytoone-relation) Ps: you should post only the code relevant to the question and not print screens. – pedroct92 Apr 10 '17 at 14:10

1 Answers1

0

I solved the problem,product_to_category table is a relationship table this is the solution

lxg
  • 215
  • 3
  • 9