ManyToMany与jointable的关系

时间:2010-09-13 18:08:34

标签: java jpa

类别EJB

package session;

import com.Entity.Category;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

项目EJB

package session;

import com.Entity.Item;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class saveItemBean implements saveItemRemote {

    @PersistenceContext
    private EntityManager em;

    public void saveItem(Item itm) {
        em.persist(itm);
    }

    public void persist(Object object) {
        em.persist(object);
    }
}

保存项目的Servlet

package src;

import com.Entity.Item;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import session.CategoryFacadeRemote;
import session.saveItemRemote;

public class saveItemAndIemCategory extends HttpServlet {
    @EJB
    private saveItemRemote saveItemBean;

    @EJB
    private CategoryFacadeRemote categoryFacadeBean;

    processRequest(HttpServletRequest request, HttpServletResponse response) {
        try {
            Item itm = new Item();
            itm.setName("HelloNew");
            saveItemBean.saveItem(itm);
        } catch (Exception e) {
            e.printStackTrace();      
        } finally {
            out.close();
        }
    } 

}

项目实体Bean

package com.Entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "item")
@NamedQueries({@NamedQuery(name = "Item.findAll", query = "SELECT i FROM Item i"), @NamedQuery(name = "Item.findByItemId", query = "SELECT i FROM Item i WHERE i.itemId = :itemId"), @NamedQuery(name = "Item.findByName", query = "SELECT i FROM Item i WHERE i.name = :name")})
public class Item implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "item_id")
    private Integer itemId;

    @Basic(optional = false)
    @Column(name = "name")
    private String name;

    @ManyToMany
    @JoinTable(name = "item_cat", 
        joinColumns = {@JoinColumn(name = "item_id", referencedColumnName = "item_id")}, 
        inverseJoinColumns = {@JoinColumn(name = "cat_id", referencedColumnName = "cat_id")})
    private List<Category> categoryCollection;

    public Item() {
    }

    public Item(Integer itemId) {
        this.itemId = itemId;
    }

    public Item(Integer itemId, String name) {
        this.itemId = itemId;
        this.name = name;
    }

    public Integer getItemId() {
        return itemId;
    }

    public void setItemId(Integer itemId) {
        this.itemId = itemId;
    }

    public String getName() {
        return name;
    }

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

    public List<Category> getCategoryCollection() {
        return categoryCollection;
    }

    public void setCategoryCollection(List<Category> categoryCollection) {
        this.categoryCollection = categoryCollection;
    }

}

我正在尝试使用这些编码来保存数据,但只有Category和Item表正在获取字段(这里没有类别编码,这些都很好)我的assosiative表item_cat没有得到保存数据。

2 个答案:

答案 0 :(得分:0)

如果您有中间连接表,则需要使用@JoinTable注释:

@JoinTable(name="item_cat", joinColumns={ @JoinColumn(name="item_id") }, inverseJoinColumns={ @JoinColumn(name="category_id") })

答案 1 :(得分:0)

确保在保留实体之前设置两侧的实体。像这样:

Item itm = new Item();
itm.setName("NewItem");

Category cat = new Category();
cat.setName("NewCategory");

itm.getCategoryCollection().add(cat);
cat.getItemCollection().add(itm);

如果您发布分类模型,那将非常有用。