forked from hibernate/hibernate-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--wip-- small tweaks, added joined inheritance test
Still needs: - interface redesign - test and make sure it's working across all dbs / dialects
- Loading branch information
Showing
6 changed files
with
259 additions
and
42 deletions.
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
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
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
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
221 changes: 221 additions & 0 deletions
221
.../hibernate/orm/test/mapping/generated/delegate/MutationDelegateJoinedInheritanceTest.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,221 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.orm.test.mapping.generated.delegate; | ||
|
||
import java.util.Date; | ||
|
||
import org.hibernate.annotations.ColumnDefault; | ||
import org.hibernate.annotations.Generated; | ||
import org.hibernate.annotations.SourceType; | ||
import org.hibernate.annotations.UpdateTimestamp; | ||
import org.hibernate.generator.EventType; | ||
import org.hibernate.generator.values.GeneratedValuesMutationDelegate; | ||
import org.hibernate.persister.entity.mutation.EntityMutationTarget; | ||
import org.hibernate.sql.model.MutationType; | ||
|
||
import org.hibernate.testing.jdbc.SQLStatementInspector; | ||
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.AfterAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Inheritance; | ||
import jakarta.persistence.InheritanceType; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Marco Belladelli | ||
*/ | ||
@DomainModel( annotatedClasses = { | ||
MutationDelegateJoinedInheritanceTest.BaseEntity.class, | ||
MutationDelegateJoinedInheritanceTest.ChildEntity.class, | ||
} ) | ||
@SessionFactory( useCollectingStatementInspector = true ) | ||
public class MutationDelegateJoinedInheritanceTest { | ||
@AfterAll | ||
public void tearDown(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> session.createMutationQuery( "delete from BaseEntity" ).executeUpdate() ); | ||
} | ||
|
||
@Test | ||
public void testInsertBaseEntity(SessionFactoryScope scope) { | ||
final GeneratedValuesMutationDelegate delegate = getDelegate( | ||
scope, | ||
BaseEntity.class, | ||
MutationType.INSERT | ||
); | ||
final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); | ||
inspector.clear(); | ||
|
||
scope.inTransaction( session -> { | ||
final BaseEntity entity = new BaseEntity(); | ||
session.persist( entity ); | ||
session.flush(); | ||
|
||
assertThat( entity.getId() ).isNotNull(); | ||
assertThat( entity.getName() ).isEqualTo( "default_name" ); | ||
|
||
inspector.assertIsInsert( 0 ); | ||
inspector.assertExecutedCount( | ||
delegate != null && delegate.supportsArbitraryValues() ? 1 : 2 | ||
); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testInsertChildEntity(SessionFactoryScope scope) { | ||
final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); | ||
inspector.clear(); | ||
|
||
scope.inTransaction( session -> { | ||
final ChildEntity entity = new ChildEntity(); | ||
session.persist( entity ); | ||
session.flush(); | ||
|
||
assertThat( entity.getId() ).isNotNull(); | ||
assertThat( entity.getName() ).isEqualTo( "default_name" ); | ||
assertThat( entity.getChildName() ).isEqualTo( "default_child_name" ); | ||
|
||
inspector.assertIsInsert( 0 ); | ||
inspector.assertIsInsert( 1 ); | ||
// Note: this is a current restriction, mutation delegates only retrieve generated values | ||
// on the "root" table, and we expect other values to be read through a subsequent select | ||
inspector.assertIsSelect( 2 ); | ||
inspector.assertExecutedCount( 3 ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testUpdateBaseEntity(SessionFactoryScope scope) { | ||
final GeneratedValuesMutationDelegate delegate = getDelegate( | ||
scope, | ||
BaseEntity.class, | ||
MutationType.UPDATE | ||
); | ||
final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); | ||
final Integer id = scope.fromTransaction( session -> { | ||
final BaseEntity entity = new BaseEntity(); | ||
session.persist( entity ); | ||
session.flush(); | ||
return entity.getId(); | ||
} ); | ||
|
||
inspector.clear(); | ||
|
||
scope.inTransaction( session -> { | ||
final BaseEntity entity = session.find( BaseEntity.class, id ); | ||
entity.setData( "changed" ); | ||
session.flush(); | ||
|
||
assertThat( entity.getUpdateDate() ).isNotNull(); | ||
|
||
inspector.assertIsSelect( 0 ); | ||
inspector.assertIsUpdate( 1 ); | ||
inspector.assertExecutedCount( | ||
delegate != null && delegate.supportsArbitraryValues() ? 2 : 3 | ||
); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testUpdateChildEntity(SessionFactoryScope scope) { | ||
final SQLStatementInspector inspector = scope.getCollectingStatementInspector(); | ||
final Integer id = scope.fromTransaction( session -> { | ||
final ChildEntity entity = new ChildEntity(); | ||
session.persist( entity ); | ||
session.flush(); | ||
return entity.getId(); | ||
} ); | ||
|
||
inspector.clear(); | ||
|
||
scope.inTransaction( session -> { | ||
final ChildEntity entity = session.find( ChildEntity.class, id ); | ||
entity.setData( "changed" ); | ||
session.flush(); | ||
|
||
assertThat( entity.getUpdateDate() ).isNotNull(); | ||
assertThat( entity.getChildUpdateDate() ).isNotNull(); | ||
|
||
inspector.assertIsSelect( 0 ); | ||
inspector.assertIsUpdate( 1 ); | ||
// Note: this is a current restriction, mutation delegates only retrieve generated values | ||
// on the "root" table, and we expect other values to be read through a subsequent select | ||
inspector.assertIsSelect( 2 ); | ||
inspector.assertExecutedCount( 3 ); | ||
} ); | ||
} | ||
|
||
private static GeneratedValuesMutationDelegate getDelegate( | ||
SessionFactoryScope scope, | ||
Class<?> entityClass, | ||
MutationType mutationType) { | ||
final EntityMutationTarget entityDescriptor = (EntityMutationTarget) scope.getSessionFactory() | ||
.getMappingMetamodel().findEntityDescriptor( entityClass ); | ||
return entityDescriptor.getMutationDelegate( mutationType ); | ||
} | ||
|
||
@Entity( name = "BaseEntity" ) | ||
@Inheritance( strategy = InheritanceType.JOINED ) | ||
@SuppressWarnings( "unused" ) | ||
public static class BaseEntity { | ||
@Id | ||
@GeneratedValue( strategy = GenerationType.IDENTITY ) | ||
private Integer id; | ||
|
||
@Generated( event = EventType.INSERT ) | ||
@ColumnDefault( "'default_name'" ) | ||
private String name; | ||
|
||
@UpdateTimestamp( source = SourceType.DB ) | ||
private Date updateDate; | ||
|
||
@SuppressWarnings( "FieldCanBeLocal" ) | ||
private String data; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Date getUpdateDate() { | ||
return updateDate; | ||
} | ||
|
||
public void setData(String data) { | ||
this.data = data; | ||
} | ||
} | ||
|
||
@Entity( name = "ChildEntity" ) | ||
@SuppressWarnings( "unused" ) | ||
public static class ChildEntity extends BaseEntity { | ||
@Generated( event = EventType.INSERT ) | ||
@ColumnDefault( "'default_child_name'" ) | ||
private String childName; | ||
|
||
@UpdateTimestamp( source = SourceType.DB ) | ||
private Date childUpdateDate; | ||
|
||
public String getChildName() { | ||
return childName; | ||
} | ||
|
||
public Date getChildUpdateDate() { | ||
return childUpdateDate; | ||
} | ||
} | ||
} |
Oops, something went wrong.