2017-04-08 4 views
0

Ich benutze Java Play Framework und Ebean, um Mysql zu verbinden, ich benutze JoinColumn, Daten wiederholen, warum ???
Play Framework Ebean JoinColumn Fehler

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 Paket-Modelle;

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; 

wenn ich @OneToMany verwenden, ist die Produkte Datenfehler wie diese

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

die Produkte Daten

+0

Ihre Zuordnung ist alles falsch. Sie sollten etwas wie [dies] (http://stackoverflow.com/questions/21202490/ebean-query-by-onetomany-relationship) tun. Wenn Sie möchten, dass die Bridge-Tabelle eine Compound-ID hat, müssen Sie etwas tun wie [this] (http://stackoverflow.com/questions/25057752/ebeane-embeddedid-mapping-column-to-manytoone-relation). Sie sollten nur den für die Frage relevanten Code posten und keine Bildschirme drucken. – pedroct92

Antwort

0

wiederhole ich das Problem gelöst, product_to_category Tabelle ist eine Beziehungstabelle this is the solution

Verwandte Themen