question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ManyToOne lazy association is not loaded with Mutiny.fetch method

See original GitHub issue

Hi, as mentioned in the title of the issue, when you try to load with Mutiny.fetch method an ManyToOne association, the linked entity is instantiated with null fields. The issue is not reproducing when trying to fetch OneToMany association.

Here is reported a minimum code to reproduce the problem


@Entity
@Table(schema = "iot_service", name = "telemetry_data")
public class TelemetryData {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "value", nullable = false)
    private Float value;

    @Column(name = "stored_at", nullable = false)
    private Long storedAt;

    @Column(name = "probed_at", nullable = false)
    private Long probedAt;

    @JoinColumn(name = "feature_id", nullable = false)
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = Feature.class)
    private Feature feature;

    public TelemetryData() {
    }

    public TelemetryData(Float value, Long storedAt, Long probedAt, Feature feature) {
        this.value = value;
        this.storedAt = storedAt;
        this.probedAt = probedAt;
        this.feature = feature;
    }

    // Getters and Setters...
}

@Entity
@Table(schema = "iot_service", name = "features")
public class Feature {

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

    @Column(name = "feature_id", nullable = false, unique = true)
    private UUID featureId;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "topic_path", nullable = false, unique = true)
    private String topicPath;

    @Enumerated(value = EnumType.STRING)
    @Column(name = "feature_type", nullable = false)
    private FeatureType featureType;

    @Column(name = "is_running")
    private Boolean isRunning;

    @Column(name = "source_type")
    private SourceType sourceType;

    @Column(name = "running_reference_id")
    private String runningReferenceId;

    @OneToMany(fetch = FetchType.LAZY, targetEntity = TelemetryData.class, mappedBy = "feature")
    private List<TelemetryData> telemetryData;

    // Constructors, getters, setters...
}

@ApplicationScoped
public class TelemetryDataRepository {

    @Inject
    Mutiny.SessionFactory rxSessionFactory;
    
    public Uni<TelemetryData> getLatestDataByFeature(Long featureId) {
        var session = this.rxSessionFactory.openSession();
        return session.createQuery(
                        "from TelemetryData t where t.feature.id = :featureId order by t.probedAt desc",
                        TelemetryData.class
                )
                .setParameter("featureId", featureId)
                .setMaxResults(1)
                .getSingleResultOrNull()
                .call(telemetryData ->
                        Mutiny.fetch(telemetryData.getFeature())
                )
                .call(ignored -> session.flush())
                .call(ignored -> session.close());
    }
}

Edit: Add a sample code to retrieve data fetched by the query

  this.telemetryDataRepository.getLatestDataByFeature(1L)
                .map(telemetryData -> {
                    var telemetryCalendar = Calendar.getInstance();
                    telemetryCalendar.setTimeInMillis(telemetryData.getProbedAt());
                    var telemetryUpdateMessage = new TelemetryUpdateMessage(
                            telemetryData.getFeature().getDevice().getId(), // <----- exception due null pointer exception on  feature.getDevice()
                            telemetryData.getFeature().getId(),
                            telemetryCalendar,
                            telemetryData.getValue()
                    );
                    return telemetryUpdateMessage;
                });

The example below is applicable also if i print the data in a logger

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:28 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
gavinkingcommented, Jun 29, 2022

Fixed now: was easier than I has estimated.

1reaction
DavideDcommented, Jun 13, 2022

Yeah, no worries. Please, just add a comment when if start to work on this

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fetch lazy associations in Hibernate reactive?
The problem is that the default equals and hashcode for the entities include the associations. So, when you run the first select query, ......
Read more >
How to Decide Between JOIN and JOIN FETCH
Typically, JOIN and JOIN FETCH come into play when the application has lazy associations but some data must be fetched eagerly.
Read more >
Hibernate Reactive 1.0.0.Alpha11 Reference Documentation
In Hibernate ORM, a lazy association is fetched transparently when the association is first accessed within a session. In Hibernate Reactive, on ...
Read more >
Hibernate Reactive - Getting Started Guide
But there is one important difference. Hibernate Reactive doesn't transparently fetch lazy associations. You either need to fetch them as part of your...
Read more >
FetchMode in Hibernate
In this short tutorial, we'll take a look at different FetchMode values we can use in the @org.hibernate.annotations.Fetch annotation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found