Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

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 even JoinColumn is allowed here.

Copy link
Author

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:

@ManyToOne(fetch = FetchType.LAZY)
@JoinFormula(value = "SUBSTR(hierarchy_path, 0, INSTR(hierarchy_path, '/') - 1)", referencedColumnName = "id")
private Invoice rootInvoice;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "rootInvoice")
private Set<Invoice> childInvoices;

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?

Copy link
Contributor

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 @ManyToOne relation 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?

Copy link
Author

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:

private List<Message> messages;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "language")
public List<Message> getMessages() {
    return messages;
}

public void setMessages(List<Message> messages) {
    this.messages = messages;
}

the test fails with exception
Caused by: java.lang.ClassCastException: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column

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;
}
}
}