2015-12-08 53 views
0

我想獲取一些字段,然後將其設置爲我的Test.entity。我的SQL查詢:引起:java.sql.SQLException:找不到列'id'

   query = "SELECT t.id as tId, t.test_name, t.duration, q.id as qId, " + 
        "q.question as question, q.is_multichoice as is_multichoice, " + 
        "q.is_open as is_open, a.id as aId, a.answer_text as answer_text FROM result r " + 
        "JOIN test t ON r.test_id = t.id " + 
        "JOIN user u ON r.user_id = u.id " + 
        "JOIN question q ON t.id = q.test_id JOIN answer a ON q.id = a.question_id " + 
        "WHERE t.id = :testId AND u.id = :userId AND r.permission = :permissionId " + 
        "AND q.archived = false AND a.archived = false", resultClass = com.bionic.entities.Test.class) 

測試實體:

public class Test { 
@Id 
@Column(name = "id") 
@GeneratedValue(strategy = GenerationType.AUTO) 
private long id; 
@Column(name = "duration", nullable = false) 
private int duration; 
@Column(name = "test_name", nullable = false, unique = true) 
private String testName; 
@Column(name = "archived", nullable = false) 
private boolean archived; 
@OneToMany(mappedBy = "test", fetch = FetchType.EAGER) 
private Set<Question> questions; 
@ManyToMany(mappedBy = "tests") 
private Set<User> users; 

問題實體

public class Question { 
@Id 
@Column(name = "id") 
@GeneratedValue(strategy = GenerationType.AUTO) 
private long id; 
@Column(name = "is_multichoice", nullable = false) 
private boolean isMultichoice; 
@Column(name = "is_open", nullable = false) 
private boolean isOpen; 
@Column(name = "picture") 
private String picture; 
@Column(name = "question") 
private String question; 
@ManyToOne 
@JoinColumn(name = "test_id", nullable = false) 
private Test test; 
@Column(name = "archived", nullable = false) 
private boolean isArchived; 
@OneToMany(mappedBy = "question", fetch = FetchType.EAGER) 
private Set<Answer> answers; 

答實體:

public class Answer { 
@Id 
@Column(name = "id") 
@GeneratedValue(strategy = GenerationType.AUTO) 
private long id; 
@Column(name = "answer_text", nullable = false) 
private String answerText; 
@Column(name = "mark", nullable = false) 
private int mark; 
@ManyToOne 
@JoinColumn(name = "question_id") 
private Question question; 
@Column(name = "picture") 
private String picture; 
@Column(name = "archived", nullable = false) 
private boolean isArchived; 

然而,執行這個查詢後,我得到exeption:

Caused by: java.sql.SQLException: Column 'id' not found. 

DAO.class:

public Test getCurrentTest(long id, long testId, long permissionId) { 
    Query query = em.createNamedQuery("getCurrentTestById"); 
    query.setParameter("userId", id); 
    query.setParameter("testId", testId); 
    query.setParameter("permissionId", permissionId); 
    return (Test) query.getSingleResult(); 
} 

我到底做錯了什麼?

+0

請確保您所有的表格都有名爲__id__的列名稱 – gauee

回答

2

您的查詢不會返回名爲id的字段。它具有名爲aId, qId, and tId的字段。

您需要在實體中使用正確的列名稱。例如,在您的Test實體中,您聲明瞭一個名爲id的列。除了你的查詢沒有返回一個名爲id的列,它將返回一個名爲tId的列。請參閱下面的示例,瞭解需要更改的內容。

public class Test { 
@tId 
@Column(name = "tId") 
@GeneratedValue(strategy = GenerationType.AUTO) 
private long tId; 
.... 
+0

我怎樣才能將它映射到實體中的我的ID? – quento

+0

我的意思是我有3個字段稱爲id(測試,問題,aswer),但我不能通過SQL查詢3字段具有相同的名稱「ID」 – quento

+0

我明白這一點。這就是爲什麼你給這三個字段別名的原因。由此改變字段的名稱以使它們是唯一的。但是這也意味着你的代碼看不到一個名爲id的字段。您需要通過新的別名來引用該字段。 – Rabbit

相關問題