简体   繁体   中英

I am getting an error (HttpMessageNotWritableException: Could not write JSON: Invalid property '' of bean class) when using getter method

I am using an interface LoanInterface.java to display values upon request. My problem is I am encountering this error:

Invalid property 'accountNumber' of bean class [com.meteor.coral.portfolio.modules.loanaccount.domain.Loan$HibernateProxy$Vm2InzHB]: 
Getter for property 'accountNumber' threw exception; nested exception is java.lang.reflect.InvocationTargetException; 
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid property 'accountNumber' of bean class 
[com.meteor.coral.portfolio.modules.loanaccount.domain.Loan$HibernateProxy$Vm2InzHB]: Getter for property 'accountNumber' threw exception; 
nested exception is java.lang.reflect.InvocationTargetException 
(through reference chain: org.springframework.data.domain.PageImpl["content"]
->java.util.Collections$UnmodifiableRandomAccessList[0]->com.sun.proxy.$Proxy394["loanId"]->com.sun.proxy.$Proxy395["accountNumber"])]

This is my LoanInterface.java

@JsonPropertyOrder({
    "id",
    "accountNumber"
})
public interface LoanInterface {
    
    public Long getId();

    public String getAccountNumber();
}

This is the model, Loan.java , where I want to get the fields:

@Entity
@Component
@Data
@EqualsAndHashCode(callSuper = false)
@Table(name = "m_loan", uniqueConstraints = { @UniqueConstraint(columnNames = { "account_no" }, name = "loan_account_no_UNIQUE"),
        @UniqueConstraint(columnNames = { "external_id" }, name = "loan_externalid_UNIQUE") })
public class Loan extends AbstractPersistableCustom<Long> {

    @Column(name = "account_no", length = 20, unique = true, nullable = false)
    private String accountNumber;
}

The id is inside the AbstractPersistableCustom class which is being extended inside Loan.java .

This is BillingProcessInterface.java where I call the LoanInterface.java

@JsonPropertyOrder({
    "id",
    "loanId"
})
public interface BillingProcessInterface {
    
    public Long getId();
    
    public LoanInterface getLoanId();
}

BillingProcess.java model, where I call the Loan.java

@Entity
@Table(name = "m_billing")
@Data
@EqualsAndHashCode(callSuper = true)
public class BillingProcess extends AbstractPersistableCustom<Long> {
    
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "loan_id", nullable = false)
    private Loan loanId;
}

The getId from LoanInterface.java works: 在此处输入图像描述

But when I try to get other fields from Loan.java like the accountNumber , I am getting the error.

Does anyone have an idea how to resolve this? Have I overused or lacked something regards the usage of annotation? Thank you.

Looks like you're trying to do projection from lazy loaded field.

@ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "loan_id", nullable = false)
    private Loan loanId;

Projection happens outside transaction so you have to have loaded fields before you use them. You can achieve it by changing fetch type to EAGER or use '@EntityGraph' annotation in repository to specify which lazy fields should be loaded.

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