question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DATA-JDBC / Postgres / ONE_TO_MANY Invalid Mapping

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jameskleehcommented, Nov 9, 2020

@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.

0reactions
dstepanovcommented, Mar 5, 2021

This is fixed in 2.3.0. Thanks for reporting and analysis @issmo @PiotrBaczkowski

Read more comments on GitHub >

github_iconTop Results From Across the Web

hibernate postgres many to one mapping issue - Stack Overflow
I got the issue with hibernate mapping when generate ...
Read more >
The best way to map a @OneToMany relationship with JPA ...
This is the most natural way of mapping a database one-to-many database association, and, usually, the most efficient alternative too.
Read more >
JPA / Hibernate One to Many Mapping Example with Spring ...
In this article, you'll learn how to map a one to many bidirectional relationship using JPA, Hibernate and Spring Boot.
Read more >
Hibernate One To Many Mapping Example Annotation
In simple terms, one to many mapping means that one row in a table can be mapped to multiple rows in another table....
Read more >
Spring Data JDBC - Reference Documentation
Microsoft SQL Server. MySQL. Oracle. Postgres. If you use a different database then your application won't startup ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found