Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -5774,4 +5774,26 @@ public FunctionalDependencyAnalysisSupport getFunctionalDependencyAnalysisSuppor
return FunctionalDependencyAnalysisSupportImpl.NONE;
}

/**
* Does this dialect support binding {@link Types#NULL} for {@link PreparedStatement#setNull(int, int)}?
* if it does, then call of {@link PreparedStatement#getParameterMetaData()} could be eliminated for better performance.
*
* @return {@code true} indicates it does; {@code false} indicates it does not;
* @see org.hibernate.type.descriptor.jdbc.ObjectNullResolvingJdbcType
*/
public boolean supportsBindingNullSqlTypeForSetNull() {
return false;
}

/**
* Does this dialect support binding {@code null} for {@link PreparedStatement#setObject(int, Object)}?
* if it does, then call of {@link PreparedStatement#getParameterMetaData()} could be eliminated for better performance.
*
* @return {@code true} indicates it does; {@code false} indicates it does not;
* @see org.hibernate.type.descriptor.jdbc.ObjectNullResolvingJdbcType
*/
public boolean supportsBindingNullForSetObject() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1028,4 +1028,9 @@ public String getDual() {
return "dual";
}

@Override
public boolean supportsBindingNullSqlTypeForSetNull() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1577,4 +1577,9 @@ public boolean supportsFromClauseInUpdate() {
public String getDual() {
return "dual";
}

@Override
public boolean supportsBindingNullSqlTypeForSetNull() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1609,4 +1609,9 @@ public DmlTargetColumnQualifierSupport getDmlTargetColumnQualifierSupport() {
public boolean supportsFromClauseInUpdate() {
return true;
}

@Override
public boolean supportsBindingNullSqlTypeForSetNull() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1202,4 +1202,9 @@ public CallableStatementSupport getCallableStatementSupport() {
return SQLServerCallableStatementSupport.INSTANCE;
}


@Override
public boolean supportsBindingNullForSetObject() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,27 @@ public <X> ValueBinder<X> getBinder(JavaType<X> javaType) {
@Override
protected void doBindNull(PreparedStatement st, int index, WrapperOptions options)
throws SQLException {
st.setNull( index, st.getParameterMetaData().getParameterType( index ) );
if ( options.getDialect().supportsBindingNullForSetObject() ) {
st.setObject( index, null );
}
else {
final int sqlType = options.getDialect().supportsBindingNullSqlTypeForSetNull() ? Types.NULL
: st.getParameterMetaData().getParameterType( index );
st.setNull( index, sqlType );
}
}

@Override
protected void doBindNull(CallableStatement st, String name, WrapperOptions options)
throws SQLException {
st.setNull( name, Types.JAVA_OBJECT );
if ( options.getDialect().supportsBindingNullForSetObject() ) {
st.setObject( name, null );
}
else {
final int sqlType = options.getDialect().supportsBindingNullSqlTypeForSetNull() ? Types.NULL
: Types.JAVA_OBJECT;
st.setNull( name, sqlType );
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.typedescriptor;

import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
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.BeforeEach;
import org.junit.jupiter.api.Test;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @author Yanming Zhou
*/
@DomainModel(
annotatedClasses = NullTest.SimpleEntity.class
)
@SessionFactory
public class NullTest {

@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> session.persist( new SimpleEntity() )
);
}

@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session ->
session.createMutationQuery( "delete from SimpleEntity" ).executeUpdate()
);
}

@Test
@JiraKey("HHH-18581")
public void passingNullAsParameterOfNativeQuery(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
SimpleEntity persisted = session.createNativeQuery(
"select * from SimpleEntity where name is null or name=:name",
SimpleEntity.class
).setParameter( "name", null ).uniqueResult();

assertNotNull( persisted );
}
);
}

@Test
public void passingNullAsParameterOfQuery(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
SimpleEntity persisted = session.createQuery(
"from SimpleEntity where name is null or name=:name",
SimpleEntity.class
).setParameter( "name", null ).uniqueResult();

assertNotNull( persisted );
}
);
}

@Entity(name = "SimpleEntity")
static class SimpleEntity {
@Id
@GeneratedValue
private Integer id;

private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}
Loading