-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HHH-13103 - Allow Hibernate Types to get access to the current configuration properties #2649
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
HHH-13103 - Allow Hibernate Types to get access to the current config…
…uration properties
- Loading branch information
commit 9ad9873979efb5d043c957b2754c0f84ab7bdd89
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
33 changes: 33 additions & 0 deletions
33
hibernate-core/src/main/java/org/hibernate/type/spi/TypeBootstrapContext.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,33 @@ | ||
/* | ||
* 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.type.spi; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Provide a way to customize the {@link org.hibernate.type.Type} instantiation process. | ||
* <p/> | ||
* If a custom {@link org.hibernate.type.Type} defines a constructor which takes the | ||
* {@link TypeBootstrapContext} argument, Hibernate will use this instead of the | ||
* default constructor. | ||
* | ||
* @author Vlad Mihalcea | ||
* | ||
* @since 5.4 | ||
*/ | ||
public class TypeBootstrapContext { | ||
|
||
private final Map<String, Object> configurationSettings; | ||
|
||
public TypeBootstrapContext(Map<String, Object> configurationSettings) { | ||
this.configurationSettings = configurationSettings; | ||
} | ||
|
||
public Map<String, Object> getConfigurationSettings() { | ||
return configurationSettings; | ||
} | ||
} |
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
131 changes: 131 additions & 0 deletions
131
...rnate-core/src/test/java/org/hibernate/test/type/contributor/ArrayTypePropertiesTest.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,131 @@ | ||
/* | ||
* 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.test.type.contributor; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.transaction.Transactional; | ||
|
||
import org.hibernate.Session; | ||
import org.hibernate.annotations.Type; | ||
import org.hibernate.annotations.TypeDef; | ||
import org.hibernate.boot.spi.MetadataBuilderContributor; | ||
import org.hibernate.engine.spi.EntityEntry; | ||
import org.hibernate.engine.spi.PersistenceContext; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; | ||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; | ||
import org.hibernate.persister.entity.EntityPersister; | ||
import org.hibernate.query.Query; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
import org.junit.Test; | ||
|
||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; | ||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* @author Vlad Mihalcea | ||
*/ | ||
@TestForIssue( jiraKey = "HHH-13103" ) | ||
public class ArrayTypePropertiesTest extends BaseEntityManagerFunctionalTestCase { | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class[] { CorporateUser.class }; | ||
} | ||
|
||
@Override | ||
protected void addConfigOptions(Map options) { | ||
options.put( "hibernate.type.array.config", new Integer[]{1, 2, 3} ); | ||
} | ||
|
||
@Override | ||
protected void afterEntityManagerFactoryBuilt() { | ||
doInJPA( this::entityManagerFactory, entityManager -> { | ||
CorporateUser user = new CorporateUser(); | ||
user.setUserName( "Vlad" ); | ||
entityManager.persist( user ); | ||
|
||
user.getEmailAddresses().add( "vlad@hibernate.info" ); | ||
user.getEmailAddresses().add( "vlad@hibernate.net" ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
doInJPA( this::entityManagerFactory, entityManager -> { | ||
List<CorporateUser> users = entityManager.createQuery( | ||
"select u from CorporateUser u where u.emailAddresses = :address", CorporateUser.class ) | ||
.unwrap( Query.class ) | ||
.setParameter( "address", new Array(), ArrayType.INSTANCE ) | ||
.getResultList(); | ||
|
||
assertTrue( users.isEmpty() ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testNativeSQL() { | ||
doInJPA( this::entityManagerFactory, entityManager -> { | ||
List<Array> emails = entityManager.createNativeQuery( | ||
"select u.emailAddresses from CorporateUser u where u.userName = :name" ) | ||
.setParameter( "name", "Vlad" ) | ||
.getResultList(); | ||
|
||
assertEquals( 1, emails.size() ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testConfigurationSettings() { | ||
doInJPA( this::entityManagerFactory, entityManager -> { | ||
SharedSessionContractImplementor session = entityManager.unwrap( SharedSessionContractImplementor.class ); | ||
|
||
CorporateUser corporateUser = entityManager.find( CorporateUser.class, "Vlad" ); | ||
PersistenceContext persistenceContext = session.getPersistenceContext(); | ||
EntityPersister entityPersister = persistenceContext.getEntry( corporateUser ).getPersister(); | ||
ArrayType arrayType = (ArrayType) entityPersister.getPropertyType( "emailAddresses" ); | ||
|
||
Map<String, Object> settings = arrayType.getSettings(); | ||
Integer[] arrayConfig = (Integer[]) settings.get( "hibernate.type.array.config" ); | ||
assertNotNull( arrayConfig ); | ||
assertArrayEquals( new Integer[]{1, 2, 3}, arrayConfig ); | ||
} ); | ||
} | ||
|
||
@Entity(name = "CorporateUser") | ||
@TypeDef( typeClass = ArrayType.class, defaultForType = Array.class ) | ||
public static class CorporateUser { | ||
|
||
@Id | ||
private String userName; | ||
|
||
private Array emailAddresses = new Array(); | ||
|
||
public String getUserName() { | ||
return userName; | ||
} | ||
|
||
public void setUserName(String userName) { | ||
this.userName = userName; | ||
} | ||
|
||
public Array getEmailAddresses() { | ||
return emailAddresses; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of creating a new
TypeBootstrapContext
every time. I'd pass it theTypeConfiguration
and be done with it, or haveTypeConfiguration
implementTypeBootstrapContext
(after making it an interface - which it ought to be anyway imo)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or better yet, have TypeFactory be the TypeBootstrapContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can work on this change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.