2016-03-08 40 views
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(); 
    } 
} 
+0

所以列「rawtype0_.rt_id」不存在 –

+0

是的,它不存在。我有列rtId,但不是rt_id。我不明白Hibernate如何生成這個rt_id。 – zigfridus

+0

好吧,你可以發佈創建hibernate查詢的方法嗎? –

回答

0

感謝誰提供了我這個link我發現我錯了@aribeiro。 列的名稱不應該是camelCase。因此,我改變了

@Column(name = 「RTID」,可爲空= false)來

@Column(name = 「RTID」,可爲空= FALSE)

,現在計劃的運作方式精細。

相關問題