-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HHH-16593 allow mappedBy to refer to a non-association property
(get rid of a totally unnecessary error)
- Loading branch information
Showing
2 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
70 changes: 70 additions & 0 deletions
70
...ore/src/test/java/org/hibernate/orm/test/mapping/mappedBy/MappedByNonAssociationTest.java
This file contains 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,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; | ||
} | ||
|
||
} |