0

正如我在标题中提到的,我在谷歌应用引擎项目中有两个实体。第一个是“商店”,另一个是“产品”。在添加 ManyToMany 注释之前,实体会定期存储,但在添加关系之后,即使返回成功消息,实体也无法存储!

以下是实体:

存储.java

package com.cheapchase.server;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name="store", catalog="content", uniqueConstraints={
    @UniqueConstraint(columnNames = "STORE_NAME"),
    @UniqueConstraint(columnNames = "STORE_CODE")})
public class Store{

    private int storeCode;
    private Date dueDate;
    private String emailAddress;
    private String storeName;
    private String storeAddress;
    private String storeDescription;
    private String userId;
    private Long id;
    private Set<Product> product;

    public Store(){

    }

    public Store(String storeName, int storeCode){
        this.storeName = storeName;
        this.setStoreCode(storeCode);
    }

    public Store(String storeName, int storeCode, Set<Product> product){
        this.storeName = storeName;
        this.storeCode = storeCode;
        this.product = product;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "STORE_ID", unique = true, nullable = false)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name="STORE_NAME", nullable=false)
    public String getStoreName() {
        return this.storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

    /*
     * Define the Many To Many relationship with product
     */
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "storae_product", catalog = "content", joinColumns = {
            @JoinColumn(name = "STORE_ID", nullable = false)},
            inverseJoinColumns = {@JoinColumn(name = "PRODUCT_ID", nullable = false)})
    public Set<Product> getProduct(){
        return this.product;
    }
    public void setProduct(Set<Product> product){
        this.product = product;
    }

    /*
     * Define the rest getters and setters
     */
    public Date getDueDate() {
        return dueDate;
    }

    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    public String getEmailAddress() {
        return this.emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getStoreDescription() {
        return storeDescription;
    }

    public void setStoreDescription(String storeDescription) {
        this.storeDescription = storeDescription;
    }

    public String getStoreAddress() {
        return storeAddress;
    }

    public void setStoreAddress(String storeAddress) {
        this.storeAddress = storeAddress;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getStoreCode() {
        return storeCode;
    }

    public void setStoreCode(int storeCode) {
        this.storeCode = storeCode;
    }

    public String toString(){
        StringBuilder builder = new StringBuilder();
        builder.append("Store [dueDate=");
        builder.append(dueDate);
        builder.append(", name=");
        builder.append(storeName);
        builder.append(", description=");
        builder.append(storeDescription);
        builder.append("]");
        return builder.toString();

    }

}

产品.java

package com.cheapchase.server;

import java.util.Set;
import java.util.HashSet;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Column;

import com.google.appengine.api.datastore.Blob;

@Entity
@Table(name = "product", catalog = "content")
public class Product {

    private Long productId;
    private String name;
    private String description;
    private int barCode;
    Blob image;
    private Set<Store> stores = new HashSet<Store>(0);

    public Product(){

    }
    public Product(String name, String description){
        this.name = name;
        this.description = description;
    }
    public Product(String name, String description, Set<Store> stores){
        this.name = name;
        this.description = description;
        this.stores = stores;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PRODUCT_ID", unique = true, nullable = false)
    public Long getProductId(){
        return this.productId;
    }

    public void setProductId(Long productId){
        this.productId = productId;
    }

    @Column(name = "NAME", nullable = false)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "DESCRIPTION")
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "product")
    public Set<Store> getStore(){
        return this.stores;
    }

    public void setStore(Set<Store> store){
        this.stores = store;
    }

    public int getBarCode() {
        return barCode;
    }

    public void setBarCode(int barCode) {
        this.barCode = barCode;
    }

}
4

0 回答 0