spring - 能够使用 entitymanager.persist() 持久化数据,但无法在 spring hibernate 应用程序中使用 entitymanager.find()
问题描述
这是我的客户实体
@Entity
public class Customer
{
@Id
private String account_no;
private String customer_name,father_name,gender, phone, email, aadhaar; // phone no is varchar because I'll do validation in angular.
private double salary;
// one customer can have one loan. Relation is unidirectional
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="account_no")
private Loan loan;
public Customer() {}
@Override
public String toString() {
return "CustomerInfo [account_no=" + account_no + ", customer_name=" + customer_name + ", phone=" + phone
+ ", email=" + email + ", aadhaar=" + aadhaar + ", salary="
+ salary + ", loan=" + loan + "]";
}
}
现在这是我的贷款实体
@Entity
public class Loan
{
@Id
@Column(name="account_no")
private String account_no; // making this as foreign key will not allow any holder to take loan again.
private String type;
private String issue_date;
private String last_payment;
private double loan_amount;
private double emi_amount;
private int emi_balance;
public Loan() {
}
@Override
public String toString() {
return "LoanInfo [account_no=" + account_no + ", type=" + type + ", loan_amount=" + loan_amount
+ ", emi_amount=" + emi_amount + ", emi_balance=" + emi_balance + ", issue_date=" + issue_date + "]";
}
}
现在 Dao 层的代码
findCustomer() 函数返回 null。在 dao findLoan() 和 addCustomer() 工作正常。即使我检查过 database ,它也会将数据持久化到数据库中。
我使用的数据库是mysql,hibernate用于实现应用程序。使用 Spring mvc。
@Repository("bankDao")
@Transactional
public class BankDao {
@PersistenceContext
EntityManager em;
// It add customer details and loan details like emi while sanctioning any loan.
public void addCustomer(Customer customer) throws RevokeLoanException
{
try {
em.persist(customer);
}
catch(Exception e) {
throw new RevokeLoanException();
}
}
// for customer details
public Customer findCustomer(String accNo) throws LoanNotFoundException{
System.out.println(em.find(Customer.class, accNo));
Customer customer=em.find(Customer.class, accNo);
if(customer==null)
throw new LoanNotFoundException();
return customer;
}
// useful while paying emi
public Loan findLoan(String acc_no) throws LoanNotFoundException{
// TODO Auto-generated method stub
Loan loan=em.find(Loan.class, acc_no);
if(loan==null)
throw new LoanNotFoundException();
return loan;
}
控制器出现问题。控制器没有以特定方式工作 id=2020-04-30T23:22:00.210
Api call = http://localhost:8082/LoanBackEnd/loan/loanStatus/2020-04-30T23:22:00.210
@GetMapping("/loanStatus/{id}")
public ResponseEntity<Map<String, String>> loanStatus(@PathVariable String id) throws LoanNotFoundException{
System.out.println(id+" which got");
Map<String,String> response=bankService.loanStatus(id);
return new ResponseEntity<Map<String,String>>(response,HttpStatus.OK);
}
那条打印线返回我 2020-04-30T23:22:00 代替 2020-04-30T23:22:00.210
解决方案
推荐阅读
- javascript - 使用 Jest 模拟用于前端测试的 BigNumber 库
- javascript - JQuery animate scrollTop 在一页中不起作用
- javascript - 为什么以相同方式绘制的两条线呈现不同
- php - PHP Xdebug 覆盖 __Dir__ 和 __File__ 常量
- javascript - 需要根据开关乘数值
- c# - Windows 窗体应用程序中未显示的组件
- c++ - 错误:在 C++ 中从“double”转换为非标量类型“请求”结构?
- python - For循环不适用于在python的docxtpl中生成docx文件
- python - 如何使用 pytest 忽略所有标记运行所有测试
- sql - Laravel API Eloquent Where 子句与 Vue 不工作