-
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.
- Loading branch information
Showing
1 changed file
with
136 additions
and
0 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
hibernate-core/src/test/java/org/hibernate/orm/test/cid/EmbeddedInsideEmbeddedIdTest.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,136 @@ | ||
package org.hibernate.orm.test.cid; | ||
|
||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.JiraKey; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.Embeddable; | ||
import jakarta.persistence.EmbeddedId; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@DomainModel( | ||
annotatedClasses = { | ||
EmbeddedInsideEmbeddedIdTest.DomainEntity.class | ||
} | ||
) | ||
@SessionFactory | ||
@JiraKey("HHH-16560") | ||
public class EmbeddedInsideEmbeddedIdTest { | ||
|
||
@BeforeAll | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
DomainEntity entity = new DomainEntity( InnerWrappingId.of( 1L ), "key" ); | ||
session.persist( entity ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testQuery(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
session.createQuery( "select d.id from DomainEntity d" ).getSingleResult(); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testGet(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
DomainEntity domainEntity = session.get( | ||
DomainEntity.class, | ||
new OuterWrappingId( InnerWrappingId.of( 1L ) ) | ||
); | ||
assertThat( domainEntity ).isNotNull(); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testQuery2(SessionFactoryScope scope) { | ||
scope.inTransaction( | ||
session -> { | ||
session.createQuery( "select id from DomainEntity" ).getSingleResult(); | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "DomainEntity") | ||
@Table(name = "domain_entity") | ||
public static class DomainEntity { | ||
@EmbeddedId | ||
private OuterWrappingId id; | ||
|
||
private String name; | ||
|
||
protected DomainEntity() { | ||
} | ||
|
||
public DomainEntity(InnerWrappingId artifactId, String name) { | ||
this(); | ||
this.id = new OuterWrappingId( artifactId ); | ||
this.name = name; | ||
} | ||
|
||
public OuterWrappingId getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
|
||
@Embeddable | ||
public static class OuterWrappingId { | ||
private InnerWrappingId artifactId; | ||
private String idType; | ||
|
||
public static OuterWrappingId of(InnerWrappingId id) { | ||
return new OuterWrappingId( id ); | ||
} | ||
|
||
protected OuterWrappingId() { | ||
} | ||
|
||
public OuterWrappingId(InnerWrappingId artifactId) { | ||
this(); | ||
this.artifactId = artifactId; | ||
this.idType = artifactId.getClass().getSimpleName(); | ||
} | ||
|
||
public String getIdType() { | ||
return idType; | ||
} | ||
} | ||
|
||
@Embeddable | ||
public static class InnerWrappingId { | ||
private Long id; | ||
|
||
protected InnerWrappingId() { | ||
} | ||
|
||
protected InnerWrappingId(Long id) { | ||
this(); | ||
this.id = id; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public static InnerWrappingId of(Long id) { | ||
return new InnerWrappingId( id ); | ||
} | ||
} | ||
} |