-
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.
`@EmbeddableInstantiatorRegistration`
- Loading branch information
Showing
13 changed files
with
351 additions
and
6 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
29 changes: 29 additions & 0 deletions
29
...nate-core/src/main/java/org/hibernate/annotations/EmbeddableInstantiatorRegistration.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,29 @@ | ||
/* | ||
* 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.annotations; | ||
|
||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import org.hibernate.metamodel.spi.EmbeddableInstantiator; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.PACKAGE; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* Registers a custom instantiator implementation | ||
*/ | ||
@Target( {TYPE, ANNOTATION_TYPE, PACKAGE} ) | ||
@Retention( RUNTIME ) | ||
@Repeatable( EmbeddableInstantiatorRegistrations.class ) | ||
public @interface EmbeddableInstantiatorRegistration { | ||
Class<?> embeddableClass(); | ||
Class<? extends EmbeddableInstantiator> instantiator(); | ||
} |
24 changes: 24 additions & 0 deletions
24
...ate-core/src/main/java/org/hibernate/annotations/EmbeddableInstantiatorRegistrations.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,24 @@ | ||
/* | ||
* 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.annotations; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.PACKAGE; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
@Target( {TYPE, ANNOTATION_TYPE, PACKAGE} ) | ||
@Retention( RUNTIME ) | ||
public @interface EmbeddableInstantiatorRegistrations { | ||
EmbeddableInstantiatorRegistration[] value(); | ||
} |
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
44 changes: 44 additions & 0 deletions
44
...nate/orm/test/mapping/embeddable/strategy/instantiator/registered/InstantiationTests.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,44 @@ | ||
/* | ||
* 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.embeddable.strategy.instantiator.registered; | ||
|
||
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 static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
@DomainModel( annotatedClasses = { Person.class, Name.class } ) | ||
@SessionFactory | ||
public class InstantiationTests { | ||
@Test | ||
public void basicTest(SessionFactoryScope scope) { | ||
scope.inTransaction( (session) -> { | ||
final Person mick = new Person( 1, new Name( "Mick", "Jagger" ) ); | ||
session.persist( mick ); | ||
|
||
final Person john = new Person( 2, new Name( "John", "Doe" ) ); | ||
john.addAlias( new Name( "Jon", "Doe" ) ); | ||
session.persist( john ); | ||
} ); | ||
scope.inTransaction( (session) -> { | ||
final Person mick = session.createQuery( "from Person where id = 1", Person.class ).uniqueResult(); | ||
assertThat( mick.getName().getFirstName() ).isEqualTo( "Mick" ); | ||
} ); | ||
scope.inTransaction( (session) -> { | ||
final Person john = session.createQuery( "from Person p join fetch p.aliases where p.id = 2", Person.class ).uniqueResult(); | ||
assertThat( john.getName().getFirstName() ).isEqualTo( "John" ); | ||
assertThat( john.getAliases() ).hasSize( 1 ); | ||
final Name alias = john.getAliases().iterator().next(); | ||
assertThat( alias.getFirstName() ).isEqualTo( "Jon" ); | ||
} ); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...java/org/hibernate/orm/test/mapping/embeddable/strategy/instantiator/registered/Name.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,31 @@ | ||
/* | ||
* 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.embeddable.strategy.instantiator.registered; | ||
|
||
//tag::embeddable-instantiator-registration[] | ||
public class Name { | ||
private final String first; | ||
private final String last; | ||
|
||
private Name() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public Name(String first, String last) { | ||
this.first = first; | ||
this.last = last; | ||
} | ||
|
||
public String getFirstName() { | ||
return first; | ||
} | ||
|
||
public String getLastName() { | ||
return last; | ||
} | ||
} | ||
//end::embeddable-instantiator-registration[] |
36 changes: 36 additions & 0 deletions
36
...ernate/orm/test/mapping/embeddable/strategy/instantiator/registered/NameInstantiator.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,36 @@ | ||
/* | ||
* 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.embeddable.strategy.instantiator.registered; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.hibernate.engine.spi.SessionFactoryImplementor; | ||
import org.hibernate.metamodel.spi.EmbeddableInstantiator; | ||
|
||
/** | ||
* @author Steve Ebersole | ||
*/ | ||
public class NameInstantiator implements EmbeddableInstantiator { | ||
@Override | ||
public Object instantiate(Supplier<Object[]> valuesAccess, SessionFactoryImplementor sessionFactory) { | ||
final Object[] values = valuesAccess.get(); | ||
// alphabetical | ||
final String first = (String) values[0]; | ||
final String last = (String) values[1]; | ||
return new Name( first, last ); | ||
} | ||
|
||
@Override | ||
public boolean isInstance(Object object, SessionFactoryImplementor sessionFactory) { | ||
return object instanceof Name; | ||
} | ||
|
||
@Override | ||
public boolean isSameClass(Object object, SessionFactoryImplementor sessionFactory) { | ||
return object.getClass().equals( Name.class ); | ||
} | ||
} |
Oops, something went wrong.