2

I`ve got 2 entities, where one is a field of another.

Country

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

Author

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

So when I call authorRepository.getAll(); method from my controller I get multiple selects like:

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=?

For me it seems that something goes realy wrong as if I would use Query with Join I would only use one SELECT and not multiple. Anyone can clarify if this is a normal behavoir for Jpa? Any way to make it work correctly not spamming SELECT queries?

0 Answers0