@Entity
public class Student implements Serializable {
@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "name")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public interface StudentRepo extends JpaRepository<Student, Long> {
@Query(value = "select id from student", nativeQuery = true)
Student findCostum();
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {JpatestingApplication.class}, initializers = ConfigFileApplicationContextInitializer.class)
@Profile("default")
public class JpatestingApplicationTests {
@Autowired
StudentRepo repo;
@Test
public void contextLoads() {
Student st = repo.findCostum();
System.out.println(st);
/*Student st = new Student();
st.setName("nabeel");
repo.save(st);*/
}
}
引起:java.sql.SQLException:找不到列'name'。在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)at at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)at at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)at at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)at at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1077)at at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5174)at at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor $ 2.doExtract(VarcharTypeDescriptor.java:62) 在 org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) 在 org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238) 在 org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234) 在 org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224) 在 org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:300) 在 org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2790) 在org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1729) 在org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1655) 在org.hibernate.loader.Loader.getRow(Loader.java:1544)at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727)at org.hibernate.loader.Loader.processResultSet(Loader.java:972)at at org.hibernate.loader.Loader.doQuery(Loader.java:930)at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) 在org.hibernate.loader.Loader.doList(Loader.java:2617)... 72更多
答案 0 :(得分:0)
您的查询:
@Query(value = "select id from student", nativeQuery = true)
Student findCostum();
没有任何意义。来自学生的查询选择ID将返回一个Longs列表(所有学生的ID)而不是单个学生本身。因此它抱怨名称列丢失,因为在此查询中只返回一列,即id。将此更改为:
@Query(value = "select id from student", nativeQuery = true)
List<Long> findCostum();
将修复您的错误,但我不相信这是您想要做的事情。我假设你只是想通过他们的身份找到一个给定的学生?在这种情况下,您可以在JpaRepository https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html#getOne-ID-
中使用getOne(id)方法Student st = repo.getOne(id);