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
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,11 @@ public Object createJdbcValue(Object domainValue, WrapperOptions options) throws
public Object[] extractJdbcValues(Object rawJdbcValue, WrapperOptions options) throws SQLException {
assert embeddableMappingType != null;
final Object[] array = new Object[embeddableMappingType.getJdbcValueCount()];
deserializeStruct( getRawStructFromJdbcValue( rawJdbcValue ), 0, 0, array, true, options );
final String struct = getRawStructFromJdbcValue( rawJdbcValue );
if ( struct == null ) {
return null;
}
deserializeStruct( struct, 0, 0, array, true, options );
if ( inverseOrderMapping != null ) {
StructHelper.orderJdbcValues( embeddableMappingType, inverseOrderMapping, array.clone(), array );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ public <X> X unwrap(T value, Class<X> type, WrapperOptions options) {

@Override
public <X> T wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( getJavaTypeClass().isInstance( value ) ) {
//noinspection unchecked
return (T) value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,16 @@ protected <X> X getArray(BasicExtractor<X> extractor, java.sql.Array array, Wrap
final Object rawArray = array.getArray();
final Object[] domainObjects = new Object[Array.getLength( rawArray )];
for ( int i = 0; i < domainObjects.length; i++ ) {
final Object[] aggregateRawValues = aggregateJdbcType.extractJdbcValues( Array.get( rawArray, i ), options );
final StructAttributeValues attributeValues =
StructHelper.getAttributeValues( embeddableMappingType, aggregateRawValues, options );
domainObjects[i] = instantiate( embeddableMappingType, attributeValues );
final Object rawJdbcValue = Array.get( rawArray, i );
if ( rawJdbcValue == null ) {
domainObjects[i] = null;
}
else {
final Object[] aggregateRawValues = aggregateJdbcType.extractJdbcValues( rawJdbcValue, options );
final StructAttributeValues attributeValues =
StructHelper.getAttributeValues( embeddableMappingType, aggregateRawValues, options );
domainObjects[i] = instantiate( embeddableMappingType, attributeValues );
}
}
return extractor.getJavaType().wrap( domainObjects, options );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.array;

import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.Struct;
import org.hibernate.testing.orm.junit.DialectFeatureChecks.SupportsStructAggregate;
import org.hibernate.testing.orm.junit.DialectFeatureChecks.SupportsTypedArrays;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

@SessionFactory
@DomainModel(annotatedClasses = {
StructArrayWithNullElementTestDemoTest.Book.class,
StructArrayWithNullElementTestDemoTest.Author.class
})
@RequiresDialectFeature(feature = SupportsStructAggregate.class)
@RequiresDialectFeature(feature = SupportsTypedArrays.class)
class StructArrayWithNullElementTestDemoTest {

@Test
void test(SessionFactoryScope scope) {
scope.inTransaction( session -> {
var book = new Book();
book.id = 1;
book.authors = Arrays.asList(
new Author( "John", "Smith" ),
null
);
session.persist( book );
} );

scope.inSession( session -> {
final var book = session.find( Book.class, 1 );
assertEquals( 2, book.authors.size() );
assertEquals( new Author( "John", "Smith" ), book.authors.get( 0 ) );
assertNull( book.authors.get( 1 ) );
} );
}

@Entity(name = "Book")
@Table(name = "books")
static class Book {
@Id
int id;
List<Author> authors;
}

@Embeddable
@Struct(name = "Author")
record Author(String firstName, String lastName) {
}
}