Skip to content

Commit

Permalink
HHH-18957 - Add test for issue
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Schatteman <[email protected]>
  • Loading branch information
jrenaat committed Jan 16, 2025
1 parent a920bf1 commit 683bbe8
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.records;

import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OrderColumn;
import jakarta.persistence.OrderBy;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

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

/**
* @author Jan Schatteman
*/
@SessionFactory
@DomainModel(annotatedClasses = {ElementCollectionOfRecordsTest.MainEntity.class})
public class ElementCollectionOfRecordsTest {

@Test
@Jira( "https://hibernate.atlassian.net/browse/HHH-18957" )
public void testInsertOrderOfRecordsInElementCollection(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
MainEntity me = new MainEntity();
me.setId( 1L );
me.addRecord( new Record( "c", "a", "b", 2L ) );
me.addRecord( new Record( "2c", "a2", "bb", 22L ) );
session.persist( me );
}
);
scope.inTransaction(
session -> {
MainEntity me = session.find( MainEntity.class, 1L );
List<Record> records = me.getRecords();
assertEquals(2, records.size());
Record r = records.get( 0 );
assertEquals("a", r.aField);
assertEquals("b", r.bField);
assertEquals("c", r.cField);
assertEquals(2L, r.longField);
r = records.get( 1 );
assertEquals("a2", r.aField);
assertEquals("bb", r.bField);
assertEquals("2c", r.cField);
assertEquals(22L, r.longField);
}
);
}

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

@OrderColumn
@ElementCollection(fetch = FetchType.EAGER)
@OrderBy("longField")
List<Record> records = new ArrayList<>();

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

public void addRecord(Record r) {
this.records.add( r );
}

public List<Record> getRecords() {
return records;
}
}

@Embeddable
public record Record(String cField, String aField, String bField, Long longField) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;

@SessionFactory
@DomainModel(annotatedClasses = {GenericEnbeddedIdRecordTest.MainEntity.class, GenericEnbeddedIdRecordTest.ReferencedEntity.class})
class GenericEnbeddedIdRecordTest {
@DomainModel(annotatedClasses = {GenericEmbeddedIdRecordTest.MainEntity.class, GenericEmbeddedIdRecordTest.ReferencedEntity.class})
class GenericEmbeddedIdRecordTest {

@Test
void testOverrideJoinColumn(SessionFactoryScope scope) {
Expand Down

0 comments on commit 683bbe8

Please sign in to comment.