micronaut-data: DATA-JDBC / Postgres / ONE_TO_MANY Invalid Mapping

I have a model that looks like the following:

@MappedEntity("answers")
public class CategoryAnswer implements Serializable {
	@Id
	@AutoPopulated
	private UUID id;
	private String category;
	private String answer;

	// ...

	@Relation(value = Relation.Kind.ONE_TO_MANY, mappedBy = "answer")
	Set<AnswerVote> votes = new HashSet<>();
}
@MappedEntity("votes")
public class AnswerVote {
	@Id
	@AutoPopulated
	UUID id;

	@Relation(value = Relation.Kind.MANY_TO_ONE, cascade = Relation.Cascade.ALL)
	@MappedProperty(value = "answer_id")
	CategoryAnswer answer;

	int vote;
}
@JdbcRepository(dialect = Dialect.POSTGRES)
public abstract class CategoryAnswerRepository implements CrudRepository<Answer, UUID> {
    @NonNull
    @Join("votes")
    public abstract List<CategoryAnswer> findAll();
}

Then given the following data:

answer 1 => vote1, vote2 answer 2 => vote3, vote4 answer 3 => vote5, vote6 answer 4 => vote7, vote8

Expected Behaviour

Calling categoryAnswersRepository.findAll() should have returned:

CategoryAnswer(id=answer1, votes=[AnswerVote(id=vote1, vote=accept), AnswerVote(id=vote2, vote=accept)])
CategoryAnswer(id=answer2, votes=[AnswerVote(id=vote3, vote=accept), AnswerVote(id=vote4 vote=accept)])
CategoryAnswer(id=answer3, votes=[AnswerVote(id=vote5, vote=accept), AnswerVote(id=vote6, vote=accept)])
CategoryAnswer(id=answer4, votes=[AnswerVote(id=vote7, vote=accept), AnswerVote id=vote8,vote=accept)])

Actual Behaviour

What I got instead:

CategoryAnswer(id=answer1, votes=[AnswerVote(id=vote1, vote=accept)])
CategoryAnswer(id=answer2, votes=[AnswerVote(id=vote3, vote=accept), AnswerVote(id=vote4 vote=accept)])
CategoryAnswer(id=answer1, votes=[AnswerVote(id=vote2, vote=accept)])
CategoryAnswer(id=answer3, votes=[AnswerVote(id=vote5, vote=accept)])
CategoryAnswer(id=answer4, votes=[AnswerVote(id=vote7, vote=accept)])
CategoryAnswer(id=answer3, votes=[AnswerVote(id=vote6, vote=accept)])
CategoryAnswer(id=answer4, votes=[AnswerVote id=vote8,vote=accept)])

This issue is very similar to https://github.com/micronaut-projects/micronaut-data/issues/192#issue-502140146

It only happens when you run a query that returns a list. I’m not seeing the issue with findById

Environment Information

  • Operating System: MacOS 10.15.5 (Catalina)
  • Micronaut Version: 2.0.0
  • JDK Version: JDK 11
  • Micronaut Data Version: 1.1.1
  • Postgres Version: 12.3

Workaround

Seem to work fine if I include ordering in the SQL. e.g. findByOrderById

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 20 (15 by maintainers)

Commits related to this issue

Most upvoted comments

@emmanuj Just FYI this only worked in 2.0.1 and 2.0.2. There was a change made for 2.0.1 that allowed this code to work but it had to be reverted because it caused a regression. This issue in itself is not a regression, but it is a bug we should fix. I think the issue is due to how Groovy decides to dispatch methods. I don’t think the issue would happen if executed from Java code.