JpaRepository - 使用getAll()删除多个选择

时间:2018-02-21 19:03:56

标签: spring hibernate spring-data spring-data-jpa jpql

我有2个实体,其中一个是另一个实体。

国家

@Entity
@Table(name = "countries", catalog = "librarydb")
@DynamicUpdate
@DynamicInsert
@SelectBeforeUpdate
public class Country {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true)
    private String countryName;
}

作者

@Entity
@Table(name = "authors", catalog = "librarydb")
@DynamicUpdate
@DynamicInsert
@SelectBeforeUpdate
public class Author {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private Long id;
    private String firstName;
    private String lastName;

    @ManyToOne
    private Country country;

}

AuthorRepository

public interface AuthorRepository extends JpaRepository<Author, Long> {}

所以当我调用authorRepository.getAll()时;来自我的控制器的方法我得到多个选择,如:

Hibernate: select author0_.id as id1_0_, author0_.country_id as country_6_0_, author0_.first_name as first_na3_0_, author0_.last_name as last_nam4_0_, from librarydb.authors author0_
Hibernate: select country0_.id as id1_2_0_, country0_.country_name as country_2_2_0_ from librarydb.countries country0_ where country0_.id=?
Hibernate: select country0_.id as id1_2_0_, country0_.country_name as country_2_2_0_ from librarydb.countries country0_ where country0_.id=?
Hibernate: select country0_.id as id1_2_0_, country0_.country_name as country_2_2_0_ from librarydb.countries country0_ where country0_.id=?
Hibernate: select country0_.id as id1_2_0_, country0_.country_name as country_2_2_0_ from librarydb.countries country0_ where country0_.id=?
Hibernate: select country0_.id as id1_2_0_, country0_.country_name as country_2_2_0_ from librarydb.countries country0_ where country0_.id=?

对我来说似乎有些事情真的错了,好像我会使用Query with Join我只会使用一个SELECT而不是多个。 任何人都可以澄清这是否是Jpa的正常行为?有什么方法可以让它正常工作而不是垃圾邮件SELECT查询?

0 个答案:

没有答案