Fix TypeDefinitionBinder registering TypeDefinition

This commit is contained in:
Andrea Boriero 2021-11-26 13:06:37 +01:00 committed by Andrea Boriero
parent 1f541170c7
commit f83fb54614
7 changed files with 152 additions and 142 deletions

View File

@ -45,7 +45,7 @@ public class TypeDefinitionBinder {
);
}
context.getTypeDefinitionRegistry().register( definition );
context.getMetadataCollector().getTypeDefinitionRegistry().register( definition );
}
}

View File

@ -4,7 +4,7 @@
* 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.typeparameters;
package org.hibernate.orm.test.typeparameters;
import java.io.Serializable;
import java.sql.PreparedStatement;

View File

@ -0,0 +1,145 @@
/*
* 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.typeparameters;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.jdbc.Work;
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.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Test for parameterizable types.
*
* @author Michael Gloegl
*/
@DomainModel(
xmlMappings = {
"org/hibernate/orm/test/typeparameters/Typedef.hbm.xml",
"org/hibernate/orm/test/typeparameters/Widget.hbm.xml"
}
)
@SessionFactory
public class TypeParameterTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session ->
session.createQuery( "delete from Widget" ).executeUpdate()
);
}
@Test
public void testSave(SessionFactoryScope scope) {
final Integer id = (Integer) scope.fromTransaction(
session -> {
Widget obj = new Widget();
obj.setValueThree( 5 );
return session.save( obj );
}
);
scope.inSession(
session ->
doWork( id, session )
);
}
private void doWork(final Integer id, final Session s) {
s.doWork(
new Work() {
@Override
public void execute(Connection connection) throws SQLException {
PreparedStatement statement = ( (SessionImplementor) s ).getJdbcCoordinator()
.getStatementPreparer()
.prepareStatement( "SELECT * FROM STRANGE_TYPED_OBJECT WHERE ID=?" );
statement.setInt( 1, id.intValue() );
ResultSet resultSet = ( (SessionImplementor) s ).getJdbcCoordinator()
.getResultSetReturn()
.extract( statement );
assertTrue( "A row should have been returned", resultSet.next() );
assertTrue(
"Default value should have been mapped to null",
resultSet.getObject( "VALUE_ONE" ) == null
);
assertTrue(
"Default value should have been mapped to null",
resultSet.getObject( "VALUE_TWO" ) == null
);
assertEquals( "Non-Default value should not be changed", resultSet.getInt( "VALUE_THREE" ), 5 );
assertTrue(
"Default value should have been mapped to null",
resultSet.getObject( "VALUE_FOUR" ) == null
);
}
}
);
}
@Test
public void testLoading(SessionFactoryScope scope) throws Exception {
initData( scope );
scope.inTransaction(
session -> {
Widget obj = (Widget) session.createQuery( "from Widget o where o.string = :string" ).setParameter(
"string",
"all-normal"
).uniqueResult();
assertEquals( "Non-Default value incorrectly loaded", obj.getValueOne(), 7 );
assertEquals( "Non-Default value incorrectly loaded", obj.getValueTwo(), 8 );
assertEquals( "Non-Default value incorrectly loaded", obj.getValueThree(), 9 );
assertEquals( "Non-Default value incorrectly loaded", obj.getValueFour(), 10 );
obj = (Widget) session.createQuery( "from Widget o where o.string = :string" )
.setParameter( "string", "all-default" )
.uniqueResult();
assertEquals( "Default value incorrectly loaded", obj.getValueOne(), 1 );
assertEquals( "Default value incorrectly loaded", obj.getValueTwo(), 2 );
assertEquals( "Default value incorrectly loaded", obj.getValueThree(), -1 );
assertEquals( "Default value incorrectly loaded", obj.getValueFour(), -5 );
}
);
}
public void initData(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Widget obj = new Widget();
obj.setValueOne( 7 );
obj.setValueTwo( 8 );
obj.setValueThree( 9 );
obj.setValueFour( 10 );
obj.setString( "all-normal" );
session.save( obj );
obj = new Widget();
obj.setValueOne( 1 );
obj.setValueTwo( 2 );
obj.setValueThree( -1 );
obj.setValueFour( -5 );
obj.setString( "all-default" );
session.save( obj );
}
);
}
}

View File

@ -9,7 +9,7 @@
<hibernate-mapping>
<typedef name="nullToMinusOne"
class="org.hibernate.test.typeparameters.DefaultValueIntegerType">
class="org.hibernate.orm.test.typeparameters.DefaultValueIntegerType">
<param name="default">-1</param>
</typedef>

View File

@ -7,10 +7,10 @@
-->
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.typeparameters">
<hibernate-mapping package="org.hibernate.orm.test.typeparameters">
<typedef name="nullToTwo"
class="org.hibernate.test.typeparameters.DefaultValueIntegerType">
class="org.hibernate.orm.test.typeparameters.DefaultValueIntegerType">
<param name="default">2</param>
</typedef>
@ -19,7 +19,7 @@
<generator class="increment"/>
</id>
<property name="valueOne" column="VALUE_ONE">
<type name="org.hibernate.test.typeparameters.DefaultValueIntegerType">
<type name="org.hibernate.orm.test.typeparameters.DefaultValueIntegerType">
<param name="default">1</param>
</type>
</property>

View File

@ -4,7 +4,7 @@
* 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.typeparameters;
package org.hibernate.orm.test.typeparameters;
/**

View File

@ -1,135 +0,0 @@
/*
* 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.typeparameters;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.jdbc.Work;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* Test for parameterizable types.
*
* @author Michael Gloegl
*/
public class TypeParameterTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] {
"typeparameters/Typedef.hbm.xml",
"typeparameters/Widget.hbm.xml"
};
}
@Test
public void testSave() {
deleteData();
Session s = openSession();
s.beginTransaction();
Widget obj = new Widget();
obj.setValueThree(5);
final Integer id = (Integer) s.save(obj);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
doWork(id, s);
s.getTransaction().commit();
s.close();
deleteData();
}
private void doWork(final Integer id, final Session s) {
s.doWork(
new Work() {
@Override
public void execute(Connection connection) throws SQLException {
PreparedStatement statement = ((SessionImplementor)s).getJdbcCoordinator().getStatementPreparer().prepareStatement( "SELECT * FROM STRANGE_TYPED_OBJECT WHERE ID=?" );
statement.setInt(1, id.intValue());
ResultSet resultSet = ((SessionImplementor)s).getJdbcCoordinator().getResultSetReturn().extract( statement );
assertTrue("A row should have been returned", resultSet.next());
assertTrue("Default value should have been mapped to null", resultSet.getObject("VALUE_ONE") == null);
assertTrue("Default value should have been mapped to null", resultSet.getObject("VALUE_TWO") == null);
assertEquals("Non-Default value should not be changed", resultSet.getInt("VALUE_THREE"), 5);
assertTrue("Default value should have been mapped to null", resultSet.getObject("VALUE_FOUR") == null);
}
}
);
}
@Test
public void testLoading() throws Exception {
initData();
Session s = openSession();
Transaction t = s.beginTransaction();
Widget obj = (Widget) s.createQuery("from Widget o where o.string = :string").setParameter("string", "all-normal").uniqueResult();
assertEquals("Non-Default value incorrectly loaded", obj.getValueOne(), 7);
assertEquals("Non-Default value incorrectly loaded", obj.getValueTwo(), 8);
assertEquals("Non-Default value incorrectly loaded", obj.getValueThree(), 9);
assertEquals("Non-Default value incorrectly loaded", obj.getValueFour(), 10);
obj = (Widget) s.createQuery("from Widget o where o.string = :string").setParameter("string", "all-default").uniqueResult();
assertEquals("Default value incorrectly loaded", obj.getValueOne(), 1);
assertEquals("Default value incorrectly loaded", obj.getValueTwo(), 2);
assertEquals("Default value incorrectly loaded", obj.getValueThree(), -1);
assertEquals("Default value incorrectly loaded", obj.getValueFour(), -5);
t.commit();
s.close();
deleteData();
}
private void initData() {
Session s = openSession();
Transaction t = s.beginTransaction();
Widget obj = new Widget();
obj.setValueOne(7);
obj.setValueTwo(8);
obj.setValueThree(9);
obj.setValueFour(10);
obj.setString("all-normal");
s.save(obj);
obj = new Widget();
obj.setValueOne(1);
obj.setValueTwo(2);
obj.setValueThree(-1);
obj.setValueFour(-5);
obj.setString("all-default");
s.save(obj);
t.commit();
s.close();
}
private void deleteData() {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery( "delete from Widget" ).executeUpdate();
t.commit();
s.close();
}
}