The remaining elements of the array are returned as null when querying the collection after applying the batch size. For example, if the batch size is 100 and only 1 id is entered, 99 nulls are bound.Below is the example code
{code:java}@Entity public class Member {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id;
@ManyToOne @JoinColumn(name = "team_id") private Team team;
protected Member() { }
public void setTeam(Team team) { this.team = team; } } {code}
{code:java}@Entity public class Team {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id;
@OneToMany(mappedBy = "team") private List<Member> members = new ArrayList<>();
public List<Member> members() { return members; } } {code}
{code:java}@Transactional @SpringBootTest class MemberTest {
@Autowired private EntityManager em;
@Test void error() { Team team = new Team(); em.persist(team); em.flush();
Member member = new Member(); member.setTeam(team); em.persist(member); em.flush(); em.clear();
Team team1 = em.find(Team.class, 1L); System.out.println(team1.members().size()); } } {code}
h3. application.yml
{code:yaml} logging: level: root: info org.hiberante.SQL: debug org.hibernate.orm.jdbc.bind: trace
org.apache.coyote.http11: debug #Http ???? ?? ???
spring: datasource: driver-class-name: org.h2.Driver url: jdbc:h2:mem:testdb username: sa password:
h2: console: enabled: true path: /h2-console
jpa: show-sql: true open-in-view: false database-platform: org.hibernate.dialect.H2Dialect properties: hibernate: format_sql: true use_sql_comments: true highlight_sql: true default_batch_fetch_size: 100
hibernate: ddl-auto: create {code}
and below is error log image.
!스크린샷 2023-06-28 오후 7.00.04.png|width=1388,height=979!
This is likely because the CollectionBatchLoaderArrayParam class, the return value of the StandardBatchLoaderFactory.createCollectionBatchLoader() method, is assigned to a reusablecollectionLoader inside the AbstractCollectionPersister.createCollectionLoader() method and then reused.
In order to use the CollectionBatchLoaderArrayParam in any case inside StandardBatchLoaderFactory.createCollectionBatchLoader(), it seems that we need to do something about the logs. |
|