Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.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 Boot JPA关系映射和联接查询_Java_Spring_Hibernate_Spring Boot_Jpa - Fatal编程技术网

Java Spring Boot JPA关系映射和联接查询

Java Spring Boot JPA关系映射和联接查询,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,开始学习Spring Boot,JPA。我发现很难理解JPA关系的概念,我尝试连接两个表,但未能达到预期的结果。有人能帮助我获得预期的结果吗 低于要求 我们有两张如下的表格 产品主模型 产品分类模型 ProductMasterRepository 调用getProductCat方法时,将结果获取为 productMasterModel值[productMasterModel[productId=1011, productName=Pencil,productCategoryId=10],Pr

开始学习Spring Boot,JPA。我发现很难理解JPA关系的概念,我尝试连接两个表,但未能达到预期的结果。有人能帮助我获得预期的结果吗

低于要求

我们有两张如下的表格

产品主模型

产品分类模型

ProductMasterRepository

调用getProductCat方法时,将结果获取为

productMasterModel值[productMasterModel[productId=1011, productName=Pencil,productCategoryId=10],ProductMasterModel [productId=1012,productName=Mobile,productCategoryId=11]]

因为ProductMasterModel没有productType变量,所以它没有显示productType

我需要下面的结果连接两个表,请帮助我实现这一点

[[productName=Pencil,productType=Stational],[productName= 移动设备,产品类型=电子设备]]


您需要在这两个表之间建立ont-to-one关系。 看看这个:
是的,一对一的关系应该有效。 应该在POJO中进行更改

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "product_master")
public class ProductMasterModel {
    @Id
    @GeneratedValue
    @Column(name = "product_id")
    private int productId;

    @Column(name = "product_name")
    private String productName;

    @Column(name = "product_category_id")
    private int productCategoryId;

    @OneToOne(mappedBy= product_master, fetch = FetchType.LAZY)
    public ProductCatagoryMasterModel productCatagory;    

    public int getProductId() {
        return productId;
    }

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

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getProductCategoryId() {
        return productCategoryId;
    }

    public void setProductCategoryId(int productCategoryId) {
        this.productCategoryId = productCategoryId;
    }

    public ProductMasterModel() {

    }

    public ProductMasterModel(String productName, int productCategoryId) {
        super();
        this.productName = productName;
        this.productCategoryId = productCategoryId;

    }
}
下一个地址是您的类别模型

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table (name="product_catagory")
public class ProductCatagoryMasterModel {

    @Id
    @GeneratedValue
    @Column(name="product_catogory_id")
    private int productCategoryId;

    @Column(name="product_type")
    private String productType;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "product_master", referencedColumnName = "product_id")
    private ProductMasterModel productMaster;

    public int getProductCategoryId() {
        return productCategoryId;
    }

    public void setProductCategoryId(int productCategoryId) {
        this.productCategoryId = productCategoryId;
    }

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public ProductCatagoryMasterModel() {
        super();
    }

    public ProductCatagoryMasterModel(String productType) {
        super();
        this.productType = productType;

    }   
}
我们也需要刀

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.lollipop.model.ProductMasterModel;

@Repository
public interface ProductMasterRepository extends CrudRepository<ProductMasterModel, Integer> {
}

产品服务

import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lollipop.model.ProductMasterModel;
import com.lollipop.repository.ProductMasterRepository;

@Service
@Transactional
public class ProductService {

    @Autowired
    private ProductMasterRepository productMasterRepository;

    public List<ProductMasterModel > getAllProducts() {
    return productMasterRepository.findAll();
    }

    public Optional<ProductMasterModel > getProductById(int productId) {
    if (!productMasterRepository.existsById(productId)) {
        throw new ResourceNotFoundException("Product with id " + productId+ " not found");
    }
    return productMasterRepository.findById(productId);
}
    }
}

在我的模型中添加了OnetoOne关系,但下面列出了例外情况:我们在这里建立的列引用是否错误?。。为了更加清晰,我有product_category表,其中包含product_category_idPK列、product_type列和product_master表,其中product_idPK列、product_name列、,product\u category\u idFK到product\u category表product\u category\u id列异常:原因:org.hibernate.AnnotationException:未知映射项位于:com.lollipop.model.ProductMasterModel.productCatagory,引用的属性未知:com.lollipop.model.ProductCatagoryMasterModel.product_master位于org.hibernate.cfg.OneToOneSecondPass.doSecondPass.doSecondPass.java:168~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]]这就是我们在您的案例中应该如何处理一对一关系的示例,但是我可以看到我错过了放置映射by=product\u master和product\u category谢谢。。我解决了得到的异常,并成功地创建了关系,但仍然不确定如何显示预期结果。在ProductService类productMasterRepository.findAll中,正在从product_主表返回数据,但我需要结果作为product_名称和product_类型,它位于product_类别表上。你能帮我接电话吗this@user7294900你能帮个忙吗
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "product_master")
public class ProductMasterModel {
    @Id
    @GeneratedValue
    @Column(name = "product_id")
    private int productId;

    @Column(name = "product_name")
    private String productName;

    @Column(name = "product_category_id")
    private int productCategoryId;

    @OneToOne(mappedBy= product_master, fetch = FetchType.LAZY)
    public ProductCatagoryMasterModel productCatagory;    

    public int getProductId() {
        return productId;
    }

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

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getProductCategoryId() {
        return productCategoryId;
    }

    public void setProductCategoryId(int productCategoryId) {
        this.productCategoryId = productCategoryId;
    }

    public ProductMasterModel() {

    }

    public ProductMasterModel(String productName, int productCategoryId) {
        super();
        this.productName = productName;
        this.productCategoryId = productCategoryId;

    }
}
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table (name="product_catagory")
public class ProductCatagoryMasterModel {

    @Id
    @GeneratedValue
    @Column(name="product_catogory_id")
    private int productCategoryId;

    @Column(name="product_type")
    private String productType;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "product_master", referencedColumnName = "product_id")
    private ProductMasterModel productMaster;

    public int getProductCategoryId() {
        return productCategoryId;
    }

    public void setProductCategoryId(int productCategoryId) {
        this.productCategoryId = productCategoryId;
    }

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public ProductCatagoryMasterModel() {
        super();
    }

    public ProductCatagoryMasterModel(String productType) {
        super();
        this.productType = productType;

    }   
}
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.lollipop.model.ProductMasterModel;

@Repository
public interface ProductMasterRepository extends CrudRepository<ProductMasterModel, Integer> {
import java.util.List;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lollipop.model.ProductMasterModel;
import com.lollipop.repository.ProductMasterRepository;

@Service
@Transactional
public class ProductService {

    @Autowired
    private ProductMasterRepository productMasterRepository;

    public List<ProductMasterModel > getAllProducts() {
    return productMasterRepository.findAll();
    }

    public Optional<ProductMasterModel > getProductById(int productId) {
    if (!productMasterRepository.existsById(productId)) {
        throw new ResourceNotFoundException("Product with id " + productId+ " not found");
    }
    return productMasterRepository.findById(productId);
}
    }
}