简体   繁体   中英

Hibernate query returns null, but entity gets loaded on object anyway

This will take some explaining. So, I have an entity called Invoice and a related table called Errors, which is used to store some processing errors.

In a DAO class, I have a query for fetch the errors with some specific criteria:

public Errors loadLastError(Invoice i) {
        try (Session session = factory.openSession()) {
            Query query = session.createQuery("select er from Errors er" +
                    " inner join er.invoice i" +
                    " where er.invoice = :invoice" +
                    " and i.status <> :code" +
                    " and i.proccessStatus = :status" +
                    " order by er.id desc");
            query.setParameter("invoice", invoice);
            query.setParameter("code", "001");
            query.setParameter("status", "form_error");
            var result = query.getSingleResult();
            return (Errors) result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

This works fine: will only get results when the conditions match. When they don't, I get the expected null result where this method is called:

this.invoice.setError(loadLastError(this.invoice);

When inspecting the code, I can see that the this.invoice object was updated correctly with a null result.

But, as soon as I pass this object invoice to another class in order to do some proccessing (send notifications basically by JSON), it gets there with a Errors object loaded, as if my original query had actually found something, which it didn't.

The following are a shortened example of my entity classes:

The Invoice:

@Entity
@DynamicUpdate
@Table(name = "data.invoice")
@TypeDef(
        name = "pgsql_enum",
        typeClass = PostgreSQLEnumType.class
)
public class Invoice implements Serializable {

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

@JsonIgnore
@Column(name = "proccessStatus")
private String proccessStatus;

@JsonIgnore
@Column(name = "status")
private String status;

@JsonIgnore
@OneToOne(mappedBy = "invoice", fetch = FetchType.LAZY)
private Errors errors;

    public Integer getId() {
        return id;
    }

    public String getProccessStatus() {
        return proccessStatus;
    }

    public void setProccessStatus(String proccessStatus) {
        this.proccessStatus= proccessStatus;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status= status;
    }

    public Errors getErrosr() {
        return errors;
    }

public void setErrors(Errorserrors) {
        this.errors= errors;
    }

The Errors entity:

@Entity
@Table(name = "data.invoice_errors")
public class Errors implements Serializable {

    public Errors() {
    }

    public Errors(Invoice invoice, String error) {
        this.invoice= invoice;
        this.error = error;
    }

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

    @OneToOne
    @JoinColumn(name = "id_invoice")
    private Invoice invoice;

    private String error;

    @Column(name = "created_at")
    private LocalDateTime createdAt;

    public Integer getId() {
        return id;
    }

    public Invoice getInvoice() {
        return invoice;
    }

    public void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

This behaviour seems very strange and I don't know how to diagnose it and what may be wrong. Any input would be very appreciated.

What I'm expecting is that the entity don't get updated out of nowhere with a result that wasn't found initially because it simply didn't match the search criteria in the first place.

I'm a colossal idiot. The issue was that the notification class was refreshing the model. Changed the database search to go after the refresh and fixed the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM