Skip to content

Commit

Permalink
HHH-16593 allow mappedBy to refer to a non-association property
Browse files Browse the repository at this point in the history
(get rid of a totally unnecessary error)
  • Loading branch information
gavinking authored and beikov committed Dec 15, 2023
1 parent a394370 commit 89d0a57
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1083,9 +1083,13 @@ public static void checkMappedByType(
if ( targetValue instanceof Collection ) {
toOne = (ToOne) ( (Collection) targetValue ).getElement();
}
else {
else if ( targetValue instanceof ToOne ) {
toOne = (ToOne) targetValue;
}
else {
// Nothing to check, EARLY EXIT
return;
}
final String referencedEntityName = toOne.getReferencedEntityName();
final PersistentClass referencedClass = persistentClasses.get( referencedEntityName );
PersistentClass ownerClass = propertyHolder.getPersistentClass();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.hibernate.orm.test.mapping.mappedBy;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import org.hibernate.Hibernate;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static jakarta.persistence.CascadeType.PERSIST;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SessionFactory
@DomainModel(annotatedClasses = {MappedByNonAssociationTest.Loan.class, MappedByNonAssociationTest.Extensions.class})
public class MappedByNonAssociationTest {

@Test void test(SessionFactoryScope scope) {
Extensions ex = new Extensions();
ex.exExtensionDays = 3L;
ex.exNo = 2L;
ex.exLoanId = 4L;

Loan loan = new Loan();
loan.id = 4L;
loan.extensions.add(ex);

scope.inTransaction(s -> s.persist(loan));
Loan l1 = scope.fromTransaction(s -> {
Loan ll = s.get(Loan.class, loan.id);
Hibernate.initialize(ll.extensions);
return ll;
});
assertEquals( 1, l1.extensions.size() );
assertEquals( loan.id, l1.id );
assertEquals( ex.exLoanId, l1.extensions.get(0).exLoanId );
Loan l2 = scope.fromSession(s -> s.createQuery("from Loan join fetch extensions", Loan.class).getSingleResult());
assertEquals( 1, l2.extensions.size() );
assertEquals( loan.id, l2.id );
assertEquals( ex.exLoanId, l2.extensions.get(0).exLoanId );
}

@Entity(name="Loan")
static class Loan {
@Id
@Column(name = "LOAN_ID")
private Long id;

@OneToMany(cascade = PERSIST, mappedBy = "exLoanId")
private List<Extensions> extensions = new ArrayList<>();
}

@Entity(name="Extensions")
static class Extensions {
@Id
@Column(name = "EX_LOAN_ID")
private Long exLoanId;
@Id
@Column(name = "EX_NO")
private Long exNo;
@Column(name = "EX_EXTENSION_DAYS")
private Long exExtensionDays;
}

}

0 comments on commit 89d0a57

Please sign in to comment.