- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 spring boot 项目,我使用 spring JPA。我有这个模型 Receipt
,它有一列 TransactionDate
,它的数据库列名称是 transaction_date
。我正在调用 findAll(Predicate predicate, Pageable pageable)
来获取所需的数据页。
当我使用我的 ReceiptService 中的 BooleanBuilder
创建我的谓词并使用 PageRequest
传递它时,它工作得很好,但当我添加一个对其进行排序,它将抛出以下异常:
org.springframework.data.mapping.PropertyReferenceException:未找到 Receipt 类型的属性事务!
这就是我创建 PageRequest 的方式
org.springframework.data.domain.Pageable pageable = new PageRequest(
0,
10,
Direction.DESC,
"transaction_date");
这就是我在 Debug模式下查看分页时的样子
Page request [number: 0, size 10, sort: transaction_date: DESC]
但是从异常来看,它似乎是从 _
这是我的存储库函数:(因为条件来自搜索框,它可能有一些其他字段为空或有值,我意识到使用谓词很容易做到这一点)
public Page<Receipt> findAll(Predicate predicate, org.springframework.data.domain.Pageable pageable);
这是收据实体
@Entity
public class Receipt extends Base {
@Column(name="email")
private String Email;
@Column(name="bsid")
private String Bsid;
@Column(name="snap_shot")
private String SnapShot;
@Column(name="transaction_date")
private Date TransactionDate;
@Column(name="reference_number")
private String ReferenceNumber;
@Column(name="total_amount")
private Double TotalAmount;
@Column(name="submitted_by_user")
private boolean SubmittedByUser;
@Column(name="approval_date")
private Date ApprovalDate;
@ManyToOne
@JoinColumn(name="user_id")
private User User;
@ManyToOne
@JoinColumn(name="store_id")
private Store Store;
@Column(name="pending")
private boolean Pending;
@Column(name="declined")
private boolean Declined;
@Column(name="approved")
private boolean Approved;
public String getEmail() {
return Email;
}
public void setEmail(String email) {
Email = email.trim();
}
public String getBsid() {
return Bsid;
}
public void setBsid(String bsid) {
Bsid = bsid.trim();
}
public String getSnapShot() {
return SnapShot;
}
public void setSnapShot(String snapShot) {
SnapShot = snapShot.trim();
}
public Store getStore() {
return Store;
}
public void setStore(Store store) {
Store = store;
}
public Date getTransactionDate() {
return TransactionDate;
}
public void setTransactionDate(Date transactionDate) {
TransactionDate = transactionDate;
}
public String getReferenceNumber() {
return ReferenceNumber;
}
public void setReferenceNumber(String referenceNumber) {
ReferenceNumber = referenceNumber.trim();
}
public Double getTotalAmount() {
return TotalAmount;
}
public void setTotalAmount(Double totalAmount) {
TotalAmount = totalAmount;
}
public boolean isSubmittedByUser() {
return SubmittedByUser;
}
public void setSubmittedByUser(boolean submittedByUser) {
SubmittedByUser = submittedByUser;
}
public User getUser() {
return User;
}
public void setUser(User user) {
User = user;
}
public boolean isPending() {
return Pending;
}
public void setPending(boolean pending) {
Pending = pending;
}
public boolean isDeclined() {
return Declined;
}
public void setDeclined(boolean declined) {
Declined = declined;
}
public boolean isApproved() {
return Approved;
}
public void setApproved(boolean approved) {
Approved = approved;
}
public Date getApprovalDate() {
return ApprovalDate;
}
public void setApprovalDate(Date approvalDate) {
ApprovalDate = approvalDate;
}
}
最佳答案
一般来说,JPA(除了当您强制执行 native SQL 命令时),期望接收 java 属性名称,在本例中为 TransactionDate,而不是真正的数据库列名称 transaction_date .
因此,要修复您的代码,基本上,您应该像这样调用:
org.springframework.data.domain.Pageable pageable = new PageRequest(
0,
10,
Direction.DESC,
"TransactionDate");
也就是说,java 命名约定规定属性和变量应该采用小驼峰式,您可以查看 oracle documentation page。 .
我不确定 JPA(在本例中为 spring-data)是否希望您也遵循命名约定。因此,如果只是按照我告诉你的那样传递名称,但没有奏效,你可能需要将你的实体更改为:
@Entity
public class Receipt extends Base {
@Column(name="email")
private String email;
@Column(name="bsid")
private String bsid;
@Column(name="snap_shot")
private String snapShot;
@Column(name="transaction_date")
private Date transactionDate;
@Column(name="reference_number")
private String referenceNumber;
@Column(name="total_amount")
private Double totalAmount;
@Column(name="submitted_by_user")
private boolean submittedByUser;
@Column(name="approval_date")
private Date approvalDate;
@ManyToOne
@JoinColumn(name="user_id")
private User user;
@ManyToOne
@JoinColumn(name="store_id")
private Store store;
@Column(name="pending")
private boolean pending;
@Column(name="declined")
private boolean declined;
@Column(name="approved")
private boolean approved;
// getters, setters, equals, hashcode, tostring, ...
}
然后,你的电话应该是这样的:
org.springframework.data.domain.Pageable pageable = new PageRequest(
0,
10,
Direction.DESC,
"transactionDate");
干杯,尼古拉斯
关于java - spring jpa 可分页排序 PropertyReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40960707/
我是一名优秀的程序员,十分优秀!