Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
Spring boot Spring引导JPA关联与普通SQL连接_Spring Boot_Spring Data Jpa_Hibernate Criteria - Fatal编程技术网

Spring boot Spring引导JPA关联与普通SQL连接

Spring boot Spring引导JPA关联与普通SQL连接,spring-boot,spring-data-jpa,hibernate-criteria,Spring Boot,Spring Data Jpa,Hibernate Criteria,我有三张桌子: 公司: 法律: 价格 在公司->法律和公司->价格之间有两个一对多的关联。公司中的公司数据很少更改。另外两个表中的数据每天都被持久化。相应实体如下所示: @Entity @Table(name = "company") public class CompanyEntity { @Id @Column(name = "company_id", nullable = false) private Integer companyID; @Colum

我有三张桌子:

公司: 法律: 价格 在公司->法律和公司->价格之间有两个一对多的关联。公司中的公司数据很少更改。另外两个表中的数据每天都被持久化。相应实体如下所示:

@Entity
@Table(name = "company")
public class CompanyEntity {


    @Id
    @Column(name = "company_id", nullable = false)
    private Integer companyID;

    @Column(name = "company_name")
    private String companyName;


     @OneToMany(mappedBy = "company", fetch = FetchType.LAZY,
                cascade = CascadeType.ALL)
    private Set<LegalEntity> legal;

     @OneToMany(mappedBy = "company", fetch = FetchType.LAZY,
                cascade = CascadeType.ALL)
    private Set<PriceEntity> price;
======

@Entity
@Table(name = "legal")
public class LegalEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;

@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "company_id", nullable = false)
private CompanyEntity company;

@Column(name = "legal_price")
private Double legalPrice;
@Entity
@Table(name = "price")
public class PriceEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;

@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "company_id", nullable = false)
private CompanyEntity company;

@Column(name = "price")
private Integer price;
为了在legal或price中持久化新数据,我首先使用findBycompanyID获取公司实体中的相应字段:

    CompanyEntity cmp = crep.findBycompanyID(13);
    LegalEntity leg = new LegalEntity(cmp, 523.5);
    lrep.save(leg);
对于查询数据,我使用CriteriaBuilder API:

      CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
      CriteriaQuery<LegalEntity> criteriaQuery = criteriaBuilder.createQuery(LegalEntity.class);
      Root<LegalEntity> root = criteriaQuery.from(LegalEntity.class);
      criteriaQuery = criteriaQuery.select(root);
      TypedQuery<LegalEntity> typedQuery = em.createQuery(criteriaQuery);
      List<LegalEntity> results = typedQuery.getResultList();
           for (LegalEntity legalEntity : results) {
                log.info(legalEntity.toString());
                }
在研究Hibernate SQL命令时,我看到:

Hibernate: 
    select
        legalentit0_.id as id1_1_,
        legalentit0_.company_id as company_3_1_,
        legalentit0_.legal_price as legal_pr2_1_ 
    from
        legal legalentit0_
Hibernate: 
    select
        companyent0_.company_id as company_1_0_0_,
        companyent0_.company_name as company_2_0_0_ 
    from
        company companyent0_ 
    where
        companyent0_.company_id=?
Hibernate: 
    select
        companyent0_.company_id as company_1_0_0_,
        companyent0_.company_name as company_2_0_0_ 
    from
        company companyent0_ 
    where
        companyent0_.company_id=?
Hibernate: 
    select
        companyent0_.company_id as company_1_0_0_,
        companyent0_.company_name as company_2_0_0_ 
    from
        company companyent0_ 
    where
        companyent0_.company_id=?
Hibernate: 
    select
        companyent0_.company_id as company_1_0_0_,
        companyent0_.company_name as company_2_0_0_ 
    from
        company companyent0_ 
    where
        companyent0_.company_id=?

我的问题是,哪种方法更有效?我更喜欢使用CriteriaBuilderAPI,因为编码非常简单,也不容易出错,但在每次持续之前,我必须查询company字段

如果您可以在一条SQL语句中完成,那么它总是比加载整个对象图更高效。你知道构造函数表达式吗?@SimonMartinelli我想做的全部事情就是基于一个公共列对两个表进行内部联接,并从两个表中获取一些列。你是说JPQL中的东西吗?看看这里:@SimonMartinelli谢谢。我的问题是,以这种方式在查询中嵌入不同的参数有点困难。例如,一个用户可能想要更改日期范围或排序顺序,而另一个用户可能只想要价格信息。通过带有可选参数的GET请求。我应该为每种情况创建不同的查询还是使用一些字符串操作?您还可以将CriteriaAPI与构造函数表达式一起使用。请阅读这些文件
    CompanyEntity cmp = crep.findBycompanyID(13);
    LegalEntity leg = new LegalEntity(cmp, 523.5);
    lrep.save(leg);
      CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
      CriteriaQuery<LegalEntity> criteriaQuery = criteriaBuilder.createQuery(LegalEntity.class);
      Root<LegalEntity> root = criteriaQuery.from(LegalEntity.class);
      criteriaQuery = criteriaQuery.select(root);
      TypedQuery<LegalEntity> typedQuery = em.createQuery(criteriaQuery);
      List<LegalEntity> results = typedQuery.getResultList();
           for (LegalEntity legalEntity : results) {
                log.info(legalEntity.toString());
                }
SELECT c.company_name, l.legal_price from legal l
JOIN company c
ON l.company_id = c. company_id;
Hibernate: 
    select
        legalentit0_.id as id1_1_,
        legalentit0_.company_id as company_3_1_,
        legalentit0_.legal_price as legal_pr2_1_ 
    from
        legal legalentit0_
Hibernate: 
    select
        companyent0_.company_id as company_1_0_0_,
        companyent0_.company_name as company_2_0_0_ 
    from
        company companyent0_ 
    where
        companyent0_.company_id=?
Hibernate: 
    select
        companyent0_.company_id as company_1_0_0_,
        companyent0_.company_name as company_2_0_0_ 
    from
        company companyent0_ 
    where
        companyent0_.company_id=?
Hibernate: 
    select
        companyent0_.company_id as company_1_0_0_,
        companyent0_.company_name as company_2_0_0_ 
    from
        company companyent0_ 
    where
        companyent0_.company_id=?
Hibernate: 
    select
        companyent0_.company_id as company_1_0_0_,
        companyent0_.company_name as company_2_0_0_ 
    from
        company companyent0_ 
    where
        companyent0_.company_id=?