JDBC OneToOne Relationship with JoinColumn generates bad save and fetch queries.
Expected Behavior
I have two entities with a one to one relationship with each other that both have complex keys. When attempting to model that with micronaut-data-jdbc without using a join table, bad queries are generated for both saving a new entity and loading an entity with a join.
@Embeddable
public record AssetId (@MappedProperty("container_id") UUID containerId, @MappedProperty("asset_id") int assetId) { }
@MappedEntity("asset")
public record Asset (
@EmbeddedId
AssetId id,
String title,
@Nullable
@Relation(value = Relation.Kind.ONE_TO_ONE, cascade = Relation.Cascade.NONE)
@JoinColumn(name="container_id", referencedColumnName = "container_id")
@JoinColumn(name="asset_id", referencedColumnName = "asset_id")
AssetMetadata metadata
) { }
@MappedEntity("assetmetadata")
public record AssetMetadata(
@EmbeddedId
AssetId id,
String author
){}
When attempting to create a new Asset and save, the following error is generated.
Error executing PERSIST: ERROR: column "metadata_id_container_id" of relation "asset" does not exist
Position: 30
io.micronaut.data.exceptions.DataAccessException: Error executing PERSIST: ERROR: column "metadata_id_container_id" of relation "asset" does not exist
Position: 30
Additionally, when loading a record with a join the following error is produced.
Error executing SQL Query: ERROR: column asset_.metadata_id_container_id does not exist
Position: 63
io.micronaut.data.exceptions.DataAccessException: Error executing SQL Query: ERROR: column asset_.metadata_id_container_id does not exist
Position: 63
If I change my relationship to be One-To-Many the generated queries work fine.
@Relation(value = Relation.Kind.ONE_TO_MANY, cascade = Relation.Cascade.NONE)
@JoinColumn(name="container_id", referencedColumnName = "container_id")
@JoinColumn(name="asset_id", referencedColumnName = "asset_id")
List<AssetMetadata> metadata
Actual Behaviour
No response
Steps To Reproduce
I've created a small example of this behaviour here.
Environment Information
micronaut 4.8.2 micronaut-data 4.12.1
Example Application
https://github.com/cutz/mn-data-one2one
Version
4.8.2
@radovanradic I'm not sure if this is related to https://github.com/micronaut-projects/micronaut-data/pull/3410 or if this is completely unrelated. If you have a chance to look at the example issue that would be great. I'm open to work arounds. I've resorted to modeling this as a OneToMany relationship for now, but I'd like to not have to do that.
It's not related to that issue. Looks like this is not supported in Micronaut currently and it might need @MapsId which is not available in micronaut-data currently.
I wouldn’t expect the embedded ids to be prefixed when the custom name is present
It is coming from metadata which is one-to-one
@MappedEntity("asset")
public record Asset (
@EmbeddedId
AssetId id,
String title,
@Nullable
@Relation(value = Relation.Kind.ONE_TO_ONE, cascade = Relation.Cascade.NONE)
@JoinColumn(name="container_id", referencedColumnName = "container_id")
@JoinColumn(name="asset_id", referencedColumnName = "asset_id")
AssetMetadata metadata
) { }
and it forms column name metadata_id_asset_id and metadata_id_container_id
Not sure why should it modify the value provided
It's coming from NamingStrategy
default @NonNull String mappedName(@NonNull List<Association> associations, @NonNull PersistentProperty property)
because it gets two associations in this list and there is probably a bug with this configuration/mapping.