micronaut-projects / micronaut-projects/micronaut-data

Allow @Join to be specified for @ElementCollection for JPA

Open
#217 2 comments 5 reactions 0 assignees View on GitHub
status: validated type: enhancement
Dominant language
Java
Stars
482
Forks
229
Avg merge
1d 7h
Merged PRs (30d)
32

Description

I have the following 3 fields on my entity:
```
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "build_dependency", joinColumns = {
@JoinColumn(name = "build_id", referencedColumnName = "id")})
@Column(name = "component_name")
private Set dependencies;

@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "build_label", joinColumns = {
@JoinColumn(name = "build_id", referencedColumnName = "id")})
@MapKeyEnumerated(EnumType.STRING)
@MapKeyColumn(name = "label_type")
@Column(name = "label_name")
private Map labels;

@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "build_property", joinColumns = {
@JoinColumn(name = "build_id", referencedColumnName = "id")})
@MapKeyColumn(name = "property_key")
@Column(name = "property_name")
private Map properties;
```
In my repo I have:
```
@Override
@Join(value = "dependencies", type = Join.Type.FETCH)
@Join(value = "properties", type = Join.Type.FETCH)
@Join(value = "labels", type = Join.Type.FETCH)
List findAll();
```
But the compile fails with:
```
/home/darren/git/micronaut/micronaut-test/src/main/java/org/belldj/BuildsDao.java:15: error:
Unable to implement Repository method: BuildsDao.findAll().
Invalid join spec [dependencies]. Property is not an association!
List findAll();
```
The problem is that when removing the `@Join` annotations, the N+1 select problem occurs and you see:
```
Hibernate: select builde0_.id as id1_3_, builde0_.name as name2_3_ from BuildE builde0_
Hibernate: select properties0_.build_id as build_id1_2_0_, properties0_.property_name as property2_2_0_, properties0_.property_key as property3_0_ from build_property properties0_ where properties0_.build_id=?
Hibernate: select labels0_.build_id as build_id1_1_0_, labels0_.label_name as label_na2_1_0_, labels0_.label_type as label_ty3_0_ from build_label labels0_ where labels0_.build_id=?
Hibernate: select dependenci0_.build_id as build_id1_0_0_, dependenci0_.component_name as componen2_0_0_ from build_dependency dependenci0_ where dependenci0_.build_id=?
```
in the log.
Can `@Join` be implemented for `@ElementCollection`?

Contributor guide

Open the contributing guide

Research direction

Start with the repository method shown in BuildsDao.java and trace how @Join validates properties against associations. Reproduce the failure with the @ElementCollection fields and the logged N+1 selects; done means @Join accepts these collections and the repository can fetch them without the reported invalid-join error.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.