Skip to content

Hhh 19543 result transformers #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion orm/hibernate-orm-6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.0</version>
<configuration>
<release>11</release>
<release>21</release>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package org.hibernate.bugs;

import jakarta.persistence.criteria.CriteriaBuilder;
import org.assertj.core.api.Assertions;
import org.hibernate.bugs.application.InstrumentData;
import org.hibernate.bugs.application.InstrumentQueryService;
import org.hibernate.bugs.domain.model.*;
import org.hibernate.cfg.AvailableSettings;

import org.hibernate.testing.orm.junit.DomainModel;
Expand All @@ -24,6 +29,11 @@
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.Test;

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

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

/**
* This template demonstrates how to develop a test case for Hibernate ORM, using its built-in unit test framework.
* Although ORMStandaloneTestCase is perfectly acceptable as a reproducer, usage of this class is much preferred.
Expand All @@ -36,8 +46,8 @@
@DomainModel(
annotatedClasses = {
// Add your entities here.
// Foo.class,
// Bar.class
Instrument.class,
OrdinaryShare.class
},
// If you use *.hbm.xml mappings, instead of annotations, add the mappings here.
xmlMappings = {
Expand All @@ -62,9 +72,45 @@ class ORMUnitTestCase {

// Add your tests, using standard JUnit 5.
@Test
void hhh123Test(SessionFactoryScope scope) throws Exception {
void hhh19542Test(SessionFactoryScope scope) throws Exception {
scope.inTransaction( session -> {
// Do stuff...
List<Instrument> instruments = session.createQuery("from Instrument ").list();
System.out.println(instruments);
} );
}

@Test
void hhh19543Test(SessionFactoryScope scope) throws Exception {
final InstrumentQueryService instrumentQueryService = new InstrumentQueryService();

scope.inTransaction( session -> {
Instrument instrument = new OrdinaryShare(
new InstrumentCode("AVGO"),
"Equity",
"Broadcom",
Arrays.asList(
new InstrumentLine(
new InstrumentLineKey("AVGO:XPAR"),
new CurrencyCode("EUR"),
"Broadcom Equity Paris"),
new InstrumentLine(
new InstrumentLineKey("AVGO:XMIL"),
new CurrencyCode("EUR"),
"Broadcom Equity Milan"),
new InstrumentLine(
new InstrumentLineKey("AVGO:NYSE"),
new CurrencyCode("USD"),
"Broadcom Equity New York Stock Exchange")));

// Add any instruments you want here, purpose is to show aggregation by "code" (JoinOn)

session.persist(instrument);

List<InstrumentData> instruments = instrumentQueryService.allInstruments(session);

assertThat( instruments ).isNotEmpty();
assertThat( instruments.getFirst().lineKeys() ).isNotEmpty();
assertThat( instruments.getFirst().lineKeys().size() == 3 ).isTrue();
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.hibernate.bugs.application;

import java.util.List;

public record InstrumentData(
String code,
String category,
String description,
List<LineKeyData> lineKeys) {

public record LineKeyData(
String id,
String currencyCode,
String description) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.hibernate.bugs.application;

import jakarta.transaction.Transactional;
import org.hibernate.Session;
import org.hibernate.bugs.domain.model.Instrument;
import org.hibernate.bugs.domain.model.InstrumentLine;
import org.hibernate.bugs.port.adapter.persistence.hibernate.HibernateResultListTransformer;
import org.hibernate.bugs.port.adapter.persistence.hibernate.JoinOn;
import org.hibernate.jpa.spi.NativeQueryTupleTransformer;

import java.util.List;

public class InstrumentQueryService {

@Transactional
public List<InstrumentData> allInstruments(final Session session) {
return session.createQuery("""
select instrument.instrumentCode.id as code,
instrument.category as category,
instrument.description as description,
instrumentLines.key.id as line_keys_id,
instrumentLines.currencyCode.code as line_keys_currency_code,
instrumentLines.description as line_keys_description
from Instrument instrument
join instrument.instrumentLines instrumentLines
order by code""")
.setTupleTransformer(new NativeQueryTupleTransformer())
.setResultListTransformer(new HibernateResultListTransformer(new JoinOn("code"), InstrumentData.class))
.list();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hibernate.bugs.domain.model;

import jakarta.persistence.AttributeOverride;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;

@Embeddable
public record CreditDerivative(
@Column(name = "INDEX_SUB_FAMILY", table = "INSTRUMENT_CREDIT_DERIVATIVE")
String indexSubFamily,
CurrencyCode currencyCode) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.hibernate.bugs.domain.model;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;

@Embeddable
public record CurrencyCode(
@Column(name = "CURRENCY_CODE")
String code) {

public CurrencyCode {
if (code == null || code.trim().isEmpty()) throw new IllegalArgumentException("Currency code must be provided.");
if (code.trim().length() != 3) throw new IllegalArgumentException("Currency code must have 3 characters.");

code = code.trim().toUpperCase().intern();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.hibernate.bugs.domain.model;

public interface Identity {

String id();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.hibernate.bugs.domain.model;

import jakarta.persistence.*;
import org.hibernate.annotations.NaturalId;

import java.util.List;

@Entity
@Table(name = "INSTRUMENT")
@DiscriminatorColumn(name = "INSTRUMENT_TYPE")
@SecondaryTable(name = "INSTRUMENT_CREDIT_DERIVATIVE", pkJoinColumns = @PrimaryKeyJoinColumn(name = "INSTRUMENT_ID", referencedColumnName = "ID"))
public abstract class Instrument {

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NaturalId
private InstrumentCode instrumentCode;

@Column(name = "CATEGORY")
private String category;

@Column(name = "DESCRIPTION")
private String description;

private CreditDerivative creditDerivative;

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "INSTRUMENT_LINE", joinColumns = @JoinColumn(name = "INSTRUMENT_ID", referencedColumnName = "ID"))
private List<InstrumentLine> instrumentLines;

protected Instrument(final InstrumentCode instrumentCode, final String category, final String description, final List<InstrumentLine> instrumentLines) {
this.instrumentCode = instrumentCode;
this.category = category;
this.description = description;
this.instrumentLines = instrumentLines;
}

protected Instrument() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hibernate.bugs.domain.model;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;

import java.io.Serializable;

@Embeddable
public record InstrumentCode(
@Column(name = "INSTRUMENT_CODE")
String id) implements Identity, Serializable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hibernate.bugs.domain.model;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;

@Embeddable
public record InstrumentLine(
InstrumentLineKey key,
CurrencyCode currencyCode,
@Column(name = "DESCRIPTION")
String description) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.hibernate.bugs.domain.model;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;

import java.io.Serializable;

@Embeddable
public record InstrumentLineKey(
@Column(name = "LINE_KEY")
String id) implements Identity, Serializable {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.hibernate.bugs.domain.model;

import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;

import java.util.List;

@Entity
@DiscriminatorValue("ORD")
public class OrdinaryShare extends Instrument {

public OrdinaryShare(
final InstrumentCode instrumentCode,
final String category,
final String description,
final List<InstrumentLine> instrumentLines) {
super(instrumentCode, category, description, instrumentLines);
}

protected OrdinaryShare() {
super();
}
}
Loading