Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
142 additions
and
0 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
...rc/test/java/org/hibernate/orm/test/onetoone/embeddedid/OneToOneMultiLevelEmbeddedId.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,142 @@ | ||
package org.hibernate.orm.test.onetoone.embeddedid; | ||
|
||
import java.util.List; | ||
|
||
import org.hibernate.query.Query; | ||
import org.hibernate.query.criteria.HibernateCriteriaBuilder; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
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.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.Embeddable; | ||
import jakarta.persistence.Embedded; | ||
import jakarta.persistence.EmbeddedId; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.JoinColumns; | ||
import jakarta.persistence.OneToOne; | ||
import jakarta.persistence.Table; | ||
import jakarta.persistence.criteria.CriteriaQuery; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
@DomainModel( | ||
annotatedClasses = { | ||
OneToOneMultiLevelEmbeddedId.Primary.class, | ||
OneToOneMultiLevelEmbeddedId.Secondary.class | ||
} | ||
) | ||
@SessionFactory | ||
@TestForIssue(jiraKey = "HHH-15279") | ||
public class OneToOneMultiLevelEmbeddedId { | ||
|
||
@BeforeEach | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
final TopId id = new TopId( 1, 2 ); | ||
final Secondary secondary = new Secondary( id ); | ||
final Primary primary = new Primary( secondary ); | ||
|
||
session.persist( secondary ); | ||
session.persist( primary ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testIt(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
final HibernateCriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); | ||
|
||
final CriteriaQuery<Primary> query = criteriaBuilder.createQuery( Primary.class ); | ||
|
||
query.select( query.from( Primary.class ) ); | ||
|
||
final Query<Primary> typedQuery = session.createQuery( query ); | ||
final List<Primary> results = typedQuery.getResultList(); | ||
assertThat( results.size(), is( 1 ) ); | ||
} | ||
); | ||
} | ||
|
||
@Embeddable | ||
public static class NestedId { | ||
|
||
private int nid; | ||
|
||
public NestedId() { | ||
} | ||
|
||
public NestedId(int nid) { | ||
this.nid = nid; | ||
} | ||
} | ||
|
||
@Embeddable | ||
public static class TopId { | ||
|
||
private int tid; | ||
|
||
@Embedded | ||
private NestedId nestedId; | ||
|
||
public TopId() { | ||
} | ||
|
||
public TopId(int tid, int nid) { | ||
this.tid = tid; | ||
this.nestedId = new NestedId( nid ); | ||
} | ||
} | ||
|
||
@Entity(name = "Secondary") | ||
@Table(name = "secondary_table") | ||
public static class Secondary { | ||
|
||
@EmbeddedId | ||
private TopId id; | ||
|
||
public Secondary() { | ||
} | ||
|
||
public Secondary(TopId id) { | ||
this.id = id; | ||
} | ||
|
||
public TopId getId() { | ||
return id; | ||
} | ||
} | ||
|
||
@Entity(name = "Primary") | ||
@Table(name = "primary_table") | ||
public static class Primary { | ||
|
||
@Id | ||
@OneToOne | ||
@JoinColumns({ | ||
@JoinColumn(name = "ptid", referencedColumnName = "tid"), | ||
@JoinColumn(name = "pnid", referencedColumnName = "nid") | ||
}) | ||
private Secondary secondary; | ||
|
||
public Primary() { | ||
} | ||
|
||
public Primary(Secondary secondary) { | ||
this.secondary = secondary; | ||
} | ||
|
||
public Secondary getSecondary() { | ||
return secondary; | ||
} | ||
} | ||
} |