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 @@ -5,7 +5,6 @@
package org.hibernate.metamodel.mapping.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -246,7 +245,15 @@ public void verifyFlushState(Object id, Object[] currentState, Object[] loadedSt

@Override
public boolean areEqual(Object one, Object other, SharedSessionContractImplementor session) {
return Arrays.equals( (Object[]) one, (Object[]) other );
final Object[] one1 = (Object[]) one;
final Object[] other1 = (Object[]) other;
final List<SingularAttributeMapping> naturalIdAttributes = getNaturalIdAttributes();
for ( int i = 0; i < naturalIdAttributes.size(); i++ ) {
if ( !naturalIdAttributes.get( i ).areEqual( one1[i], other1[i], session ) ) {
return false;
}
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.annotations.naturalid;

import org.hibernate.annotations.NaturalId;

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.Test;

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

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;


@DomainModel(
annotatedClasses = {
ByteArrayNaturalIdTest.TestEntity.class,
}
)
@SessionFactory
@JiraKey("HHH-18409")
public class ByteArrayNaturalIdTest {

private static final String NATURAL_ID_1 = "N1";

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

@Test
public void testSelectByNaturalId(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
TestEntity entity = new TestEntity( 1L, new byte[] { 1, 2 }, NATURAL_ID_1 );
session.persist( entity );
TestEntity testEntity = session.byNaturalId( TestEntity.class )
.using( "naturalId2", NATURAL_ID_1 )
.using( "naturalId1", new byte[] { 1, 2 } )
.load();

assertThat( testEntity ).as( "Loading the entity by its natural id failed" ).isNotNull();
TestEntity testEntity2 = session.byNaturalId( TestEntity.class )
.using( "naturalId2", NATURAL_ID_1 )
.using( "naturalId1", new byte[] { 1, 3 } )
.load();
assertThat( testEntity2 ).as( "Loading the entity using wrong natural id failed" ).isNull();

}
);
}

@Entity(name = "TestEntity")
public static class TestEntity {
@Id
private Long id;

@NaturalId
private byte[] naturalId1;

@NaturalId
private String naturalId2;

public TestEntity() {
}

public TestEntity(Long id, byte[] naturalId1, String naturalId2) {
this.id = id;
this.naturalId1 = naturalId1;
this.naturalId2 = naturalId2;
}


}
}
Loading