1. 程式人生 > >對於JPA實現的hibernate實體的下劃線無法轉換問題

對於JPA實現的hibernate實體的下劃線無法轉換問題

這個問題困擾了我很久,最後終於解決了,廢話不多說。


User這個實體類裡面有如下

 @ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "accounts_id")
	public Accounts getAccounts() {
		return this.accounts;
	}

這時候如果我在action裡面呼叫了
User user = userService.getByProperties("accounts_id", account.getId());

就會報錯

could not resolve property: accounts_id of: com.pwq.entity.User [select o from com.pwq.entity.User o where 1=1 and o.accounts_id=:accounts_id]

但是改成下面的就OK了
User user = userService.getByProperties("accounts.id", account.getId());

如果實體裡面帶下劃線的欄位是一個基本物件如Integer、String等,如

@Column(name = "test_id")
	public Integer getTestId() {
		return this.testId;
	}
因為和hibernate的對映機制有關的,所以把abc_def改成abcDef就可以了
List<Testtable> tb0 = testtablesr.criteriaEqeals("testId", 321);