Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring数据连接多个表_Java_Database_Spring_Join - Fatal编程技术网

Java Spring数据连接多个表

Java Spring数据连接多个表,java,database,spring,join,Java,Database,Spring,Join,这是我的数据库项目: 如图所示,我对表格的正确组合有问题。 这是我的文件: Category.java @Entity public class Category { @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id; private String categoryName; protected Category() {} public Category(Str

这是我的数据库项目:

如图所示,我对表格的正确组合有问题。 这是我的文件:

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;
    }
}
我得到以下错误:原因:org.springframework.data.mapping.PropertyReferenceException:找不到类型itemsfinal的属性categoryName

我不知道该怎么做才能正确连接表

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

    }
}

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


您还需要修复item

中的设置局部变量,确切地说,您何时会收到错误?My error now thread main org.springframework.beans.factory.BeanCreationException中的异常:创建名为“ItemsFinalServiceImpl”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.dao.InvalidDataAccessResourceUsageException:无法准备语句;SQL[选择itemsfinal0.id作为id1\u 2\u,itemsfinal0.category\u id作为category2\u,itemsfinal0\u.item\u id作为item\u id3\u 2\u,itemsfinal0\u.local\u id作为items\u FinallItemsFinal0\u中的local\u id4\u 2\u];嵌套异常为org.hibernate.exception.sqlgrammareexception:无法准备语句顺便说一句:您的代码显示了图中未显示的项和局部变量之间的依赖关系。
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);

    }
}
@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;