英文:
How to handle nullPointer in Spring Data JPA projections
问题
以下是翻译好的部分:
有没有想法是否有一种方法来处理在投影创建过程中发生的自定义方法以处理空值或任何其他异常?
在下面的JPQL查询中,我试图组合一个自定义对象,但是当列updatedBy下没有数据(在数据库中为null)时,由于无法继续处理空对象(getUpdatedBy -> getProfile -> getName),它会在内部失败。
@Query("select new john.home.com.demo.repository.projections.AgreementType(agreement.name, " +
"agreement.type, " +
"agreement.audit.createdBy.profile.name, " +
"agreement.audit.updateDate, " +
"agreement.audit.updatedBy.profile.name) from Agreement agreement")
List<AgreementType> findAgreementTypes();
这是我的实体:
Agreement:
@Setter
@Getter
@NoArgsConstructor
@Entity
@Table(name = "AGREEMENT")
public class Agreement {
@Id
@Column(name = "AGREEMENT_ID")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "TYPE")
private String type;
@Embedded
Audit audit;
}
Embeddable Audit:
@Getter
@Setter
@Embeddable
public class Audit {
@OneToOne
@JoinColumn(name = "CREATED_BY")
private SystemProcess createdBy;
@Column(name = "CREATION_DATE")
private LocalDate creationDate;
@Column(name = "UPDATE_DATE")
private LocalDate updateDate;
@OneToOne
@JoinColumn(name = "UPDATED_BY")
private SystemProcess updatedBy;
}
SystemProcess:
@Getter
@Setter
@Entity
@Table(name = "sys_process")
public class SystemProcess {
@Id
@Column(name = "PROCESS_ID")
private Integer id;
@Column(name = "PROCESS_MSG_TXT")
private String messageText;
@OneToOne
@JoinColumn(name = "PROFILE_ID")
private SystemProfile profile;
}
SystemProfile:
@Getter
@Setter
@Entity
@Table(name = "sys_profile")
public class SystemProfile {
@Id
@Column(name = "PROFILE_ID")
private Integer id;
@Column(name = "PROFILE_NM")
private String name;
}
这是我使用的JPQL查询,但由于未知原因而无法工作:
@Query("select new dan.home.pl.demo.repository.projections.AgreementType(" +
"agreement.name, " +
"agreement.audit.createdBy.profile.name, " +
"case when (agreement.audit.updatedBy is null) then 'Dummy Name' else agreement.audit.updatedBy.profile.name end" +
") from Agreement agreement")
List<AgreementType> findAgreementTypes();
英文:
Any thoughts if there's a way to have a custom method for handling null or any other exceptions happening during projection creation?
In the below JPQL query I'm trying to compose a custom object, but when there's no data under column updatedBy (it's null in database) - then it fails internally since cannot proceed with null object any further (getUpdatedBy -> getProfile -> getName).
@Query("select new john.home.com.demo.repository.projections.AgreementType(agreement.name, " +
"agreement.type, " +
"agreement.audit.createdBy.profile.name, " +
"agreement.audit.updateDate, " +
"agreement.audit.updatedBy.profile.name) from Agreement agreement")
List<AgreementType> findAgreementTypes();
Here are my entities:
Agreement:
@Setter
@Getter
@NoArgsConstructor
@Entity
@Table(name = "AGREEMENT")
public class Agreement {
@Id
@Column(name = "AGREEMENT_ID")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "TYPE")
private String type;
@Embedded
Audit audit;
Embeddable Audit
@Getter
@Setter
@Embeddable
public class Audit {
@OneToOne
@JoinColumn(name = "CREATED_BY")
private SystemProcess createdBy;
@Column(name = "CREATION_DATE")
private LocalDate creationDate;
@Column(name = "UPDATE_DATE")
private LocalDate updateDate;
@OneToOne
@JoinColumn(name = "UPDATED_BY")
private SystemProcess updatedBy;
SystemProcess:
@Getter
@Setter
@Entity
@Table(name = "sys_process")
public class SystemProcess {
@Id
@Column(name = "PROCESS_ID")
private Integer id;
@Column(name = "PROCESS_MSG_TXT")
private String messageText;
@OneToOne
@JoinColumn(name = "PROFILE_ID")
private SystemProfile profile;
SystemProfile:
@Getter
@Setter
@Entity
@Table(name = "sys_profile")
public class SystemProfile {
@Id
@Column(name = "PROFILE_ID")
private Integer id;
@Column(name = "PROFILE_NM")
private String name;
}
Here's the JPQL query I've used, however it doesn't work due to unknown reason
@Query("select new dan.home.pl.demo.repository.projections.AgreementType(" +
"agreement.name, " +
"agreement.audit.createdBy.profile.name, " +
"case when (agreement.audit.updatedBy is null) then 'Dummy Name' else agreement.audit.updatedBy.profile.name end" +
") from Agreement agreement")
List<AgreementType> findAgreementTypes();
答案1
得分: 1
你可以使用 case 语句来实现这一点
查阅文档,链接在这里
https://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html/ch11.html
类似于
case when agreement.audit is not null then agreement.audit.updatedBy.profile.name end
英文:
You can use the case statement to achieve this
Check the documentation here
https://docs.jboss.org/hibernate/orm/4.3/devguide/en-US/html/ch11.html
Something like
case when agreement.audit is not null then agreement.audit.updatedBy.profile.name end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论