0
我決定測試Spring Boot。我的項目有下一個依賴項:JPA,MySql,WEB。我創建了簡單的MySql數據庫。下面是它的表:獲取com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列
CREATE TABLE `rawtype` (
`rtId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`rtName` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`rtId`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
以下是此表域名:
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name="rawtype")
public class Rawtype implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="rtId", nullable = false)
@GeneratedValue
private int rtId;
@Column(name="rtName", nullable = false)
private String rtName;
protected Rawtype() {
}
public Rawtype(int rtId, String rtName) {
this.rtId = rtId;
this.rtName = rtName;
}
public int getRtId() {
return rtId;
}
public void setRtId(int rtId) {
this.rtId = rtId;
}
public String getRtName() {
return rtName;
}
public void setRtName(String rtName) {
this.rtName = rtName;
}
@Override
public String toString() {
return "Rawtype{" +
"rtId=" + rtId +
", rtName='" + rtName + '\'' +
'}';
}
}
試圖用一個JpaRepository方法
List<T> findAll();
獲得從該表中的所有行數我看到Hibernate執行這個查詢:
select rawtype0_.rt_id as rt_id1_0_, rawtype0_.rt_name as rt_name2_0_ from rawtype rawtype0_
而我得到這個錯誤:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'rawtype0_.rt_id' in 'field list'
任何人都可以建議我該怎麼辦? 謝謝。
P.S.
RawtypeRepository.java
import org.springframework.data.jpa.repository.JpaRepository;
import domain.Rawtype;
public interface RawtypeRepository extends JpaRepository<Rawtype,Integer> {
}
RawtypeServiceImpl.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import domain.Rawtype;
import java.util.List;
@Service
@Transactional
public class RawtypeServiceImpl implements RawtypeService{
@Autowired
RawtypeRepository rawtypeRepository;
public List<Rawtype> findAll() {
return rawtypeRepository.findAll();
}
}
所以列「rawtype0_.rt_id」不存在 –
是的,它不存在。我有列rtId,但不是rt_id。我不明白Hibernate如何生成這個rt_id。 – zigfridus
好吧,你可以發佈創建hibernate查詢的方法嗎? –