-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-12997 - @OneToMany does not work with @JoinFormula #2547
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
Closed
dominikbielinski
wants to merge
1
commit into
hibernate:5.3
from
dominikbielinski:one_to_many_join_formula
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
...core/src/test/java/org/hibernate/test/annotations/onetomany/OneToManyJoinFormulaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package org.hibernate.test.annotations.onetomany; | ||
|
|
||
| import org.hibernate.annotations.JoinFormula; | ||
| import org.hibernate.cfg.AvailableSettings; | ||
| import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; | ||
| import org.hibernate.testing.TestForIssue; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import javax.persistence.*; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| @TestForIssue( jiraKey = "HHH-12997" ) | ||
| public class OneToManyJoinFormulaTest | ||
| extends BaseEntityManagerFunctionalTestCase { | ||
|
|
||
| @Override | ||
| protected Class<?>[] getAnnotatedClasses() { | ||
| return new Class<?>[] { | ||
| Invoice.class | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| protected void addConfigOptions(Map options) { | ||
| options.put( AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, Boolean.TRUE ); | ||
| } | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| doInJPA( this::entityManagerFactory, entityManager -> { | ||
| Invoice rootInvoice = new Invoice(); | ||
| rootInvoice.setId(1L); | ||
| rootInvoice.setHierarchyPath("1/"); | ||
| entityManager.persist(rootInvoice); | ||
|
|
||
| Invoice childInvoice1 = new Invoice(); | ||
| childInvoice1.setId(2L); | ||
| childInvoice1.setHierarchyPath("1/2/"); | ||
| entityManager.persist(childInvoice1); | ||
|
|
||
| Invoice childInvoice2 = new Invoice(); | ||
| childInvoice2.setId(3L); | ||
| childInvoice2.setHierarchyPath("1/3/"); | ||
| entityManager.persist(childInvoice2); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBulkUpdate() { | ||
| doInJPA( this::entityManagerFactory, entityManager -> { | ||
| Invoice rootInvoice = entityManager.createQuery( | ||
| "select inv from Invoice inv " + | ||
| "left join fetch inv.childInvoices " + | ||
| "where inv.id = 1", Invoice.class).getSingleResult(); | ||
|
|
||
| assertEquals(2, rootInvoice.getChildInvoices().size()); | ||
| }); | ||
| } | ||
|
|
||
| @Entity(name = "Invoice") | ||
| public static class Invoice { | ||
|
|
||
| @Id | ||
| private Long id; | ||
|
|
||
| @Column(name = "hierarchy_path") | ||
| private String hierarchyPath; | ||
|
|
||
| @OneToMany(fetch = FetchType.LAZY) | ||
| @JoinFormula(value = "hierarchy_path like id || '/_%'", referencedColumnName = "hierarchy_path") | ||
| private Set<Invoice> childInvoices = new HashSet<>(); | ||
|
|
||
| public String getHierarchyPath() { | ||
| return hierarchyPath; | ||
| } | ||
|
|
||
| public void setHierarchyPath(String hierarchyPath) { | ||
| this.hierarchyPath = hierarchyPath; | ||
| } | ||
|
|
||
| public Set<Invoice> getChildInvoices() { | ||
| return childInvoices; | ||
| } | ||
|
|
||
| public void setChildInvoices(Set<Invoice> childInvoices) { | ||
| this.childInvoices = childInvoices; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public void setId(Long id) { | ||
| this.id = id; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you annotate the inverse relation? I.e.
private Invoice parentInvoice? Not sure whether evenJoinColumnis allowed here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that as well:
but there is same exception as described here https://discourse.hibernate.org/t/how-to-use-the-hibernate-joinformula-annotation-with-a-onetomany-jpa-association/1461 (i.e. ManyToOne works with JoinFormula, but OneToMany doesn't - even when "mappedBy" is added and JoinFormula is on ManyToOne side).
I guess the question is just whether JoinFormula works with OneToMany - but if it doesn't, how is anyone supposed to know that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an existing test with a
@JoinFormula @ManyToOnerelation here: https://github.com/hibernate/hibernate-orm/blob/e5dc635a52362f69b69acb8d5b166b69b165dbbd/hibernate-core/src/test/java/org/hibernate/test/annotations/manytoonewithformula/ManyToOneWithFormulaTest.java . So what you're saying is that when this example is extended with an inverse mapping (a@OneToMany(mappedBy = ""), Hibernate fails to boot?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's exactly what I was trying to say. For instance, in the test that you mentioned, if you add following to Language entity:
the test fails with exception
Caused by: java.lang.ClassCastException: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column