Skip to content

Commit

Permalink
HHH-16766: Load lazy hierarchical IdClass entities
Browse files Browse the repository at this point in the history
  • Loading branch information
JBodkin authored and dreab8 committed Aug 2, 2023
1 parent 91b17b8 commit fe89b0b
Show file tree
Hide file tree
Showing 10 changed files with 637 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private boolean handleIdType(EntityPersister persister, LoadEvent event, LoadTyp
return false;
}
else {
return true;
return !idClass.isInstance( event.getEntityId() );
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping;
import org.hibernate.metamodel.mapping.SelectableMappings;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverter;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.ast.Clause;
Expand Down Expand Up @@ -218,6 +220,10 @@ public String getAttributeName() {
@Override
public Object getIdentifier(Object entity) {
if ( hasContainingClass() ) {
final LazyInitializer lazyInitializer = HibernateProxy.extractLazyInitializer( entity );
if ( lazyInitializer != null ) {
return lazyInitializer.getIdentifier();
}
final Object id = identifierValueMapper.getRepresentationStrategy().getInstantiator().instantiate(
null,
sessionFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.hibernate.orm.test.annotations.cid;

import jakarta.persistence.*;

import java.util.List;

@Entity
@Table(name = "flight")
public class Flight {

@Id
@Column(name = "id")
private Integer id;

@OneToMany(mappedBy = "flight", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<FlightSegment> segments;

public Integer getId() {
return id;
}

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

public List<FlightSegment> getSegments() {
return segments;
}

public void setSegments(List<FlightSegment> segments) {
this.segments = segments;
}

public void addSegment(FlightSegment segment) {
segment.setFlight(this);
segments.add(segment);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.hibernate.orm.test.annotations.cid;

import jakarta.persistence.*;

@Entity
@IdClass(FlightSegmentId.class)
@Table(name = "flight_segment")
public class FlightSegment {

@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "flight_id", nullable = false)
private Flight flight;

@Id
@Column(name = "segment_number")
private Integer segmentNumber;

@OneToOne(mappedBy = "segment", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private FlightSegmentConfiguration configuration;

public Flight getFlight() {
return flight;
}

public void setFlight(Flight flight) {
this.flight = flight;
}

public Integer getSegmentNumber() {
return segmentNumber;
}

public void setSegmentNumber(Integer segmentNumber) {
this.segmentNumber = segmentNumber;
}

public FlightSegmentConfiguration getConfiguration() {
return configuration;
}

public void setConfiguration(FlightSegmentConfiguration configuration) {
configuration.setSegment(this);
this.configuration = configuration;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.hibernate.orm.test.annotations.cid;

import jakarta.persistence.*;

@Entity
@IdClass(FlightSegmentConfigurationId.class)
@Table(name = "flight_segment_configuration")
public class FlightSegmentConfiguration {

@Id
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "flight_id", referencedColumnName = "flight_id", nullable = false)
@JoinColumn(name = "segment_number", referencedColumnName = "segment_number", nullable = false)
private FlightSegment segment;

public FlightSegment getSegment() {
return segment;
}

public void setSegment(FlightSegment segment) {
this.segment = segment;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.hibernate.orm.test.annotations.cid;

import java.io.Serializable;
import java.util.Objects;
import java.util.StringJoiner;

public class FlightSegmentConfigurationId implements Serializable {

private FlightSegmentId segment;

public FlightSegmentConfigurationId() {
}

public FlightSegmentConfigurationId(FlightSegmentId segment) {
this.segment = segment;
}

public FlightSegmentId getSegment() {
return segment;
}

public void setSegment(FlightSegmentId segment) {
this.segment = segment;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FlightSegmentConfigurationId that = (FlightSegmentConfigurationId) o;
return Objects.equals(segment, that.segment);
}

@Override
public int hashCode() {
return Objects.hash(segment);
}

@Override
public String toString() {
return new StringJoiner(", ", FlightSegmentConfigurationId.class.getSimpleName() + "[", "]")
.add("segment=" + segment)
.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.hibernate.orm.test.annotations.cid;

import java.io.Serializable;
import java.util.Objects;
import java.util.StringJoiner;

public class FlightSegmentId implements Serializable {

private Integer flight;
private Integer segmentNumber;

public FlightSegmentId() {
}

public FlightSegmentId(Integer flight, Integer segmentNumber) {
this.flight = flight;
this.segmentNumber = segmentNumber;
}

public Integer getFlight() {
return flight;
}

public void setFlight(Integer flight) {
this.flight = flight;
}

public Integer getSegmentNumber() {
return segmentNumber;
}

public void setSegmentNumber(Integer segmentNumber) {
this.segmentNumber = segmentNumber;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FlightSegmentId that = (FlightSegmentId) o;
return Objects.equals(flight, that.flight) && Objects.equals(segmentNumber, that.segmentNumber);
}

@Override
public int hashCode() {
return Objects.hash(flight, segmentNumber);
}

@Override
public String toString() {
return new StringJoiner(", ", FlightSegmentId.class.getSimpleName() + "[", "]")
.add("flight=" + flight)
.add("segmentNumber=" + segmentNumber)
.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.hibernate.orm.test.annotations.cid;

import jakarta.persistence.*;

@Entity
@Table(name = "freight")
public class Freight {

@Id
@Column(name = "freight_number")
private Integer freightNumber;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "flight_id", referencedColumnName = "flight_id")
@JoinColumn(name = "segment_number", referencedColumnName = "segment_number")
private FlightSegment flightSegment;

public Integer getFreightNumber() {
return freightNumber;
}

public void setFreightNumber(Integer freightNumber) {
this.freightNumber = freightNumber;
}

public FlightSegment getFlightSegment() {
return flightSegment;
}

public void setFlightSegment(FlightSegment flightSegment) {
this.flightSegment = flightSegment;
}

}

0 comments on commit fe89b0b

Please sign in to comment.