原因:java.sql.SQLException:Column';id';找不到
我想获取一些字段,然后将其设置为Test.entity。我的SQL查询:原因:java.sql.SQLException:Column';id';找不到,java,sql,hibernate,entity,Java,Sql,Hibernate,Entity,我想获取一些字段,然后将其设置为Test.entity。我的SQL查询: 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
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 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();
}
但是,在执行此查询后,我得到了以下验证:
Caused by: java.sql.SQLException: Column 'id' not found.
DAO.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 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();
}
我做错了什么?您的查询没有返回名为
id
的字段。它的字段名为aId、qId和tId
您需要在实体中使用正确的列名。例如,在您的测试实体中,您声明了一个名为id的列。除了您的查询不返回名为id的列外,它还返回一个名为tId的列。有关需要更改的内容的示例,请参见下文
public class Test {
@tId
@Column(name = "tId")
@GeneratedValue(strategy = GenerationType.AUTO)
private long tId;
....
请确保您的所有表都有名为id的列。我如何将其映射到实体中的id字段?我的意思是,我有3个名为id(test、question、aswer)的字段,但我无法通过SQL查询获得3个同名“id”的字段。我明白这一点。这就是为什么你给这3个字段一个别名。从而更改字段的名称,使其唯一。但这也意味着您的代码看不到名为id的字段。您需要通过其新别名引用该字段。如果我应该使用SqlResultSetMappings,我应该使用SqlResultSetMappings还是重命名每个实体中的字段(我不喜欢此变体),我如何为答案和问题设置字段,因为它们是子实体。您需要在实体中使用正确的列名。例如,在测试实体中,您声明了一个名为
id
的列。除了查询不返回名为id
的列外,它还返回名为tId
的列。