Skip to content

Wrong result if copying set in the constructor #487

@pawelgosztyla

Description

@pawelgosztyla

Hi,
I'm not sure if it is related to #476, if yes then sorry for duplicate.
I am using JdbcMapper.stream to map one-to-many relation to my objects:

class A {
  private Set<B> bs;
}
class B {
}

I am using builder pattern to instantiate objects of A and I'm copying the set in the constructor to guarantee that A is immutable:
private A(Set<B> bs) { this.bs = Collections.unmodifiableSet(new HashSet<>(bs)); }

In the result there is an A with only one object of B in the set whilst there should be more of them.

After some of debugging I found that the sequence of actions is as follows:

  1. new set of Bs is created and one object is inserted
  2. instance of A is instantiated using builder, the set with one element inside is used
  3. the set is copied in the A's constructor
  4. more objects of B are added to the set

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions