Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OGM-717 Adding support for @type(type = "true_false")
- Loading branch information
1 parent
2bfcf3f
commit a2b310e
Showing
7 changed files
with
212 additions
and
9 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
core/src/main/java/org/hibernate/ogm/type/descriptor/impl/CharMappedGridTypeDescriptor.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,35 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* 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.ogm.type.descriptor.impl; | ||
|
||
import org.hibernate.ogm.model.spi.Tuple; | ||
import org.hibernate.type.descriptor.WrapperOptions; | ||
import org.hibernate.type.descriptor.java.JavaTypeDescriptor; | ||
|
||
/** | ||
* Maps a field to a {@link Character} value. | ||
* | ||
* @author Gunnar Morling | ||
*/ | ||
public class CharMappedGridTypeDescriptor implements GridTypeDescriptor { | ||
public static final CharMappedGridTypeDescriptor INSTANCE = new CharMappedGridTypeDescriptor(); | ||
|
||
@Override | ||
public <X> GridValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) { | ||
return new BasicGridBinder<X>(javaTypeDescriptor, this) { | ||
@Override | ||
protected void doBind(Tuple resultset, X value, String[] names, WrapperOptions options) { | ||
resultset.put( names[0], javaTypeDescriptor.unwrap( value, Character.class, options ) ); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public <X> GridValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) { | ||
return new BasicGridExtractor<X>( javaTypeDescriptor, true ); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
core/src/main/java/org/hibernate/ogm/type/impl/TrueFalseType.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,35 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* 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.ogm.type.impl; | ||
|
||
import org.hibernate.MappingException; | ||
import org.hibernate.engine.spi.Mapping; | ||
import org.hibernate.ogm.type.descriptor.impl.CharMappedGridTypeDescriptor; | ||
|
||
/** | ||
* Maps {@link Boolean} to {@code T} or {@code F} characters. | ||
* | ||
* @author Gunnar Morling | ||
*/ | ||
public class TrueFalseType extends AbstractGenericBasicType<Boolean> { | ||
|
||
public static final TrueFalseType INSTANCE = new TrueFalseType(); | ||
|
||
public TrueFalseType() { | ||
super( CharMappedGridTypeDescriptor.INSTANCE, org.hibernate.type.TrueFalseType.INSTANCE.getJavaTypeDescriptor() ); | ||
} | ||
|
||
@Override | ||
public int getColumnSpan(Mapping mapping) throws MappingException { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "true_false"; | ||
} | ||
} |
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
107 changes: 107 additions & 0 deletions
107
...rc/test/java/org/hibernate/ogm/datastore/mongodb/test/mapping/BuiltinTypeMappingTest.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,107 @@ | ||
/* | ||
* Hibernate OGM, Domain model persistence for NoSQL datastores | ||
* | ||
* 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.ogm.datastore.mongodb.test.mapping; | ||
|
||
import static org.hibernate.ogm.datastore.mongodb.utils.MongoDBTestHelper.assertDbObject; | ||
|
||
import org.hibernate.Transaction; | ||
import org.hibernate.ogm.OgmSession; | ||
import org.hibernate.ogm.backendtck.type.Bookmark; | ||
import org.hibernate.ogm.utils.OgmTestCase; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for the mappings of built-in types into MongoDB. | ||
* | ||
* @author Gunnar Morling | ||
*/ | ||
public class BuiltinTypeMappingTest extends OgmTestCase { | ||
|
||
private String bookmarkId; | ||
|
||
@Before | ||
public void setUpTestData() { | ||
OgmSession session = openSession(); | ||
Transaction transaction = session.beginTransaction(); | ||
|
||
Bookmark bookmark = new Bookmark(); | ||
bookmark.setFavourite( Boolean.TRUE ); | ||
bookmark.setPrivate( true ); | ||
|
||
session.persist( bookmark ); | ||
transaction.commit(); | ||
session.close(); | ||
|
||
this.bookmarkId = bookmark.getId(); | ||
} | ||
|
||
@After | ||
public void removeTestData() { | ||
OgmSession session = openSession(); | ||
Transaction transaction = session.beginTransaction(); | ||
|
||
session.delete( session.get( Bookmark.class, bookmarkId ) ); | ||
|
||
transaction.commit(); | ||
session.close(); | ||
} | ||
|
||
@Test | ||
public void booleanMapping() { | ||
OgmSession session = openSession(); | ||
Transaction transaction = session.beginTransaction(); | ||
|
||
assertDbObject( | ||
session.getSessionFactory(), | ||
// collection | ||
"Bookmark", | ||
// query | ||
"{ '_id' : '" + bookmarkId + "' }", | ||
// fields | ||
"{ 'favourite' : '1' }", | ||
// expected | ||
"{ " + | ||
"'_id' : '" + bookmarkId + "', " + | ||
"'favourite' : true" + | ||
"}" | ||
); | ||
|
||
transaction.commit(); | ||
session.close(); | ||
} | ||
|
||
@Test | ||
public void trueFalseTypeMapping() { | ||
OgmSession session = openSession(); | ||
Transaction transaction = session.beginTransaction(); | ||
|
||
assertDbObject( | ||
session.getSessionFactory(), | ||
// collection | ||
"Bookmark", | ||
// query | ||
"{ '_id' : '" + bookmarkId + "' }", | ||
// fields | ||
"{ 'isPrivate' : '1' }", | ||
// expected | ||
"{ " + | ||
"'_id' : '" + bookmarkId + "', " + | ||
"'isPrivate' : 'T'" + | ||
"}" | ||
); | ||
|
||
transaction.commit(); | ||
session.close(); | ||
} | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { Bookmark.class }; | ||
} | ||
} |
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