代码之家  ›  专栏  ›  技术社区  ›  smeeb

JPQL只返回第一条记录

  •  0
  • smeeb  · 技术社区  · 6 年前

    这里是Spring-Boot/JPA/Hibernate/MySQL。我有以下表格:

    describe states;
    +------------+---------------------+------+-----+---------+----------------+
    | Field      | Type                | Null | Key | Default | Extra          |
    +------------+---------------------+------+-----+---------+----------------+
    | state_id   | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
    | country_id | bigint(20) unsigned | NO   | MUL | NULL    |                |
    | state_name | varchar(250)        | NO   |     | NULL    |                |
    | state_code | varchar(3)          | YES  | MUL | NULL    |                |
    +------------+---------------------+------+-----+---------+----------------+
    
    describe countries;
    +--------------+---------------------+------+-----+---------+----------------+
    | Field        | Type                | Null | Key | Default | Extra          |
    +--------------+---------------------+------+-----+---------+----------------+
    | country_id   | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
    | country_name | varchar(250)        | NO   | UNI | NULL    |                |
    | country_code | varchar(3)          | NO   | UNI | NULL    |                |
    +--------------+---------------------+------+-----+---------+----------------+
    

    当我跑步时 SELECT * FROM states WHERE country_id = 1; 我得到:

    +----------+------------+----------------------+------------+
    | state_id | country_id | state_name           | state_code |
    +----------+------------+----------------------+------------+
    |     3805 |          1 | Alabama              | NULL       |
    |     3806 |          1 | Alaska               | NULL       |
    |     3807 |          1 | Arizona              | NULL       |
    |     3808 |          1 | Arkansas             | NULL       |
    |     3810 |          1 | California           | NULL       |
    ...
    |     3860 |          1 | Virginia             | NULL       |
    |     3861 |          1 | Washington           | NULL       |
    |     3862 |          1 | West Virginia        | NULL       |
    |     3863 |          1 | Wisconsin            | NULL       |
    |     3864 |          1 | Wyoming              | NULL       |
    +----------+------------+----------------------+------------+
    
    (All 50 states)
    

    具有相应JPA实体:

    @Entity
    @Table(name = "states")
    public class State {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        @JsonIgnore
        @NotNull
        private String name;
    
        @Column(name = "state_code")
        @NotEmpty
        private String code;
    
        @ManyToOne(fetch = FetchType.EAGER, cascade = [CascadeType.PERSIST, CascadeType.MERGE])
        @JoinColumn(name = "country_id", referencedColumnName = "country_id")
        @NotNull
        @Valid
        private Country country;
    
        // Getters, setters & constructors
    }
    
    @Entity
    @Table(name = "countries")
    public class Country {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        @JsonIgnore
        @NotNull
        private String name;
    
        @Column(name = "state_code")
        @NotEmpty
        private String code;
    
        // Getters, setters & constructors
    }
    

    我还有以下几点 CrudRepository :

    public interface StateRepository extends CrudRepository<State, Long> {
        @Query("FROM Province WHERE country = :country")
        public Set<Province> findAllByCountry(@Param("country") Country country);
    }
    

    最后,我有一个服务方法 Country 并返回所有 States 与之相关的:

    public Set<Province> getProvincesByCountryCode(Country country) {
        log.info("Country id is " + country.getId());
        Set<Province> provinces = stateRepository.findAllByCountry(country);
        log.info("Found " + provinces.size() + " states associated with this country (" + country.getCode() + ")");
    
        // TODO: Return 'provinces' once I get this fixed...
        return null;
    }
    

    当此服务方法执行时,它只返回 List<State> 用一个 State 其中(因此大小为1),日志输出为:

    Country id is 1
    Found 1 states associated with this country (US)
    

    所以MySQL数据库知道所有50个状态,但是 StateRepository 仅返回1。特别是它返回了第一个州记录(阿拉巴马州),我觉得很有趣。。。

    很明显,我的JPQL在 积垢沉积 ,谁能发现丢失了什么?

    2 回复  |  直到 6 年前
        1
  •  0
  •   Bhushan Uniyal    6 年前

    使用以下代码:

    public interface StateRepository extends JpaRepository<Province, Long> {
        public Set<Province> findAllByCountry(Country country);
    }
    

    此外,调试此类内容的最佳方法是在应用程序中启用sql日志,请参阅sql生成的日志,您可以轻松了解所见日志背后的情况

        2
  •  0
  •   smeeb    6 年前

    最后,我使用了一个本地查询:

    String query = "SELECT * FROM states where country_id = " + countryId;
    entityManager.createNativeQuery(query, State.class).getResultList();
    

    很有魅力!