Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-15722 : @OneToMany mappedBy with a @Any #8109

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

boutss
Copy link

@boutss boutss commented Apr 4, 2024

https://hibernate.atlassian.net/browse/HHH-15722
https://discourse.hibernate.org/t/onetomany-with-interface-type-return-variable-role-column-any/7050

Since the collection, which is mapped by the return variable (mappedBy) of the referenced objects, knows its type, the identifying column of the foreign key is sufficient, without having to use the discrimination column.

@startuml

class Library {
    -Long id
    -Set<Book> books
    +void setId(Long)
    +Long getId()
    +Set<Book> getBooks()
    +void setBooks(Set<Book>)
}

class Book {
    -Long id
    -Store store
    +void setId(Long)
    +Long getId()
    +Store getStore()
    +void setStore(Store)
}

interface Store {
}

Library --|> Store : implements
Library "1" -- "*" Book : contains

@enduml

Since the collection, which is mapped by the return variable (mappedBy) of the referenced objects, knows its type, the identifying column of the foreign key is sufficient, without having to use the discrimination column.

@startuml

class Library {
    -Long id
    -Set<Book> books
    +void setId(Long)
    +Long getId()
    +Set<Book> getBooks()
    +void setBooks(Set<Book>)
}

class Book {
    -Long id
    -Store store
    +void setId(Long)
    +Long getId()
    +Store getStore()
    +void setStore(Store)
}

interface Store {
}

Library --|> Store : implements
Library "1" -- "*" Book : contains

@enduml
Copy link
Contributor

@beikov beikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the identifying column of the foreign key is sufficient, without having to use the discrimination column.

I'm not so sure about this. In simple cases like your test case this might work out, but there is no guarantee that an FK with value X always points to a certain subtype.

Imagine the following test data:

Book(id: 1, store_id: 1, store_role: 'library'), 
Book(id: 2, store_id: 1, store_role: 'shop'),
Library(id: 1),
Shop(id: 1) 

Now, the way the mapping is constructed, the collection in a library would contain both books, which is wrong.

You have to add an additional restriction to the collection e.g. store_role = 'library'. I tried annotating @SQLRestriction("store_role = 'library'") on the collection mapping of your test entity, and it produced the desired effect. Now you just need to ensure that you infer the correct restriction in the boot model of the collection. Also, please add a test based on the test data that I posted to ensure the correct behavior.

@boutss
Copy link
Author

boutss commented Apr 16, 2024

Thank you very much, I'll do this as quickly as possible!

Vincent Bouthinon added 2 commits April 18, 2024 16:05
…UMN_ROLE='roleName'") allows distinguishing instances belonging to the collection
…cate to be added to match the @SQLRestriction("COLUMN_ROLE='roleName'"), so that the collection loads only the elements that belong to it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants