Spring数据连接多个表

时间:2015-03-14 04:58:53

标签: java database spring join

这是我的数据库项目:

enter image description here 我对表的正确组合有问题,如图中所示。 这是我的文件:

Category.java

@Entity
public class Category {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private String categoryName;

    protected Category() {}

    public Category(String categoryName) {
         this.categoryName = categoryName;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public long getId() {
        return id;
    }

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

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s']",
                id, categoryName);
    }

}

Items.java

@Entity
@Table(name = "Items")

public class Items {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int ItemId;
    private String ItemName;
    private String price;
    private Set<Locals> locals = new HashSet<Locals>(0);

    public Items(){

    }

    public Items(String price, String itemName) {

        this.price = price;
        this.ItemName = itemName;
    }

    public void setItemId(int itemId) {
        ItemId = itemId;
    }

    public void setLocals(Set<Locals> locals) {
        this.locals = locals;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public void setItemName(String itemName) {
        ItemName = itemName;
    }
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ItemId", unique = true, nullable = false)
    public int getItemId() {
        return ItemId;
    }
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "itemsTo")
    public Set<Locals> getLocals() {
        return locals;
    }
    @Column(name = "price", nullable = false, length = 10)
    public String getPrice() {
        return price;
    }
    @Column(name = "ItemName", nullable = false, length = 10)
    public String getItemName() {
        return ItemName;
    }
}

Locals.java

@Entity
@Table(name = "Locals")

public class Locals {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int LocalId;
    private String localName;
    private String address;
    private String phoneNumber;
    private String coorX;
    private String coorY;
    private Set<Items> itemsTo = new HashSet<Items>(0);

    public Locals(){

    }

    public Locals(String localName,String address,String phoneNumber, String coorY, String coorX) {
        this.coorY = coorY;
        this.coorX = coorX;
        this.phoneNumber = phoneNumber;
        this.address = address;
        this.localName = localName;
    }
    public Locals(String localName,String address,String phoneNumber, String coorY, String coorX, Set<Items> itemsTo) {
        this.coorY = coorY;
        this.coorX = coorX;
        this.phoneNumber = phoneNumber;
        this.address = address;
        this.localName = localName;
        this.itemsTo = itemsTo;
    }

    public void setLocalId(int localId) {
        LocalId = localId;
    }

    public void setLocalName(String localName) {
        this.localName = localName;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setCoorX(String coorX) {
        this.coorX = coorX;
    }

    public void setCoorY(String coorY) {
        this.coorY = coorY;
    }

    public void setItemsTo(Set<Items> itemsTo) {
        this.itemsTo = itemsTo;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "LocalId", unique = true, nullable = false)
    public int getLocalId() {
        return LocalId;
    }
    @Column(name = "localName", unique = false, nullable = false, length = 10)
    public String getLocalName() {
        return localName;
    }
    @Column(name = "address", unique = false, nullable = false, length = 10)
    public String getAddress() {
        return address;
    }
    @Column(name = "phoneNumber", unique = false, nullable = false, length = 10)
    public String getPhoneNumber() {
        return phoneNumber;
    }
    @Column(name = "coorX", unique = false, nullable = false, length = 10)
    public String getCoorX() {
        return coorX;
    }
    @Column(name = "coorY", unique = false, nullable = false, length = 10)
    public String getCoorY() {
        return coorY;
    }
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "ItemsFinall", joinColumns = {
            @JoinColumn(name = "LocalId", nullable = false, updatable = false) },
            inverseJoinColumns = { @JoinColumn(name = "ItemId",
                    nullable = false, updatable = false) })
    public Set<Items> getItemsTo() {
        return itemsTo;
    }
}

和ItemsFinall.java

@Entity
@Table(name = "ItemsFinall")
public class ItemsFinall {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private int ItemId;
    private int CategoryId;
    private int LocalId;


    public ItemsFinall(int id, int localId, int categoryId, int itemId) {
        this.LocalId = localId;
        this.CategoryId = categoryId;
        this.ItemId = itemId;
        this.id=id;
    }

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

    public void setLocalId(int localId) {
        LocalId = localId;
    }

    public void setCategoryId(int categoryId) {
        CategoryId = categoryId;
    }

    public void setItemId(int itemId) {
        ItemId = itemId;
    }

    public int getLocalId() {
        return LocalId;
    }

    public int getCategoryId() {
        return CategoryId;
    }

    public int getItemId() {
        return ItemId;
    }

    public int getId() {
        return id;
    }
}

我收到以下错误:Caused by: org.springframework.data.mapping.PropertyReferenceException: No property categoryName found for type ItemsFinall!

我不知道如何正确连接表格。

ItemsServiceImpl.java

package dnfserver.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Łukasz on 2015-03-14.
 */
@Component
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryRepository categoryRepository;
    private Map<Integer,String> categoryMap = new HashMap<Integer, String>();


    @Override
    public Category create(Category category) {

        Category createdCategory = category;
        return categoryRepository.save(createdCategory);

    }

    @Override
    public Category delete(int id) {
        return null;
    }

    @Autowired
    public Map<Integer,String> findAll(){
        int i=0;
        for (Category cat : categoryRepository.findAll()) {
            categoryMap.put(i,cat.toString());
            i++;
        }
        return categoryMap;
    }

    @Override
    public Category update(Category shop) {
        return null;
    }

    @Override
    public Category findById(int id) {
        return categoryRepository.findOne(id);

    }
}

1 个答案:

答案 0 :(得分:1)

在hibernate / JPA中,您可以建模实体之间的关系,但不能使用普通的ID!

例如

@Entity
@Table(name = "ItemsFinall")
public class ItemsFinall {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private int id;

  //instead of: private int ItemId;
  @OneToMany
  private Item item;

  //instead of: private int CategoryId;
  @OneToMany
  private Category category;

  //instead of: private int LocalId;
  @OneToMany
  private Local local;

...

(您还需要修复Set<Locals> locals中的item