【问题标题】:Query.getResultList() does not return results even if there are rows in the sql tableQuery.getResultList() 即使sql表有行也不返回结果
【发布时间】:2019-08-20 09:54:42
【问题描述】:

我是 Java 和 Spring 的新手,我正在为我的 Sql Server 中的一个表做简单的 CRUD 项目。但它不会在 get 查询中返回任何结果。 以下是代码文件:

下面是pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.luv2code.springboot</groupId>
	<artifactId>cruddemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>cruddemo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.microsoft.sqlserver</groupId>
			<artifactId>mssql-jdbc</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

以下是 CruddemoApplication.java 文件,它是启动文件:

package com.luv2code.springboot.cruddemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CruddemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(CruddemoApplication.class, args);
	}

}

package com.luv2code.springboot.cruddemo.dao;

import java.util.List;

import com.luv2code.springboot.cruddemo.entity.StudentDetails;

public interface StudentDetailDAO {
    public List<StudentDetails> findAll(); 
}
	

package com.luv2code.springboot.cruddemo.dao;

import java.sql.Connection;
import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.luv2code.springboot.cruddemo.entity.StudentDetails;

@Repository
public class StudentDetailsDAOHibernateImpl implements StudentDetailDAO {
	
	private EntityManager entityManager;

	@Autowired
    public StudentDetailsDAOHibernateImpl(EntityManager theEntityManager) 
	{
		this.entityManager = theEntityManager;
	}
	
	@Override
	@Transactional
	public List<StudentDetails> findAll() {

		List<StudentDetails> students = null;
		
		try
		{
			
			Session currentSession = entityManager.unwrap(Session.class);
					
			Query<StudentDetails> theQuery = currentSession.createQuery("from StudentDetails",StudentDetails.class);
			
			students = theQuery.getResultList();
		}
		catch(Exception ex)
		{
		     ex.printStackTrace();	
		}
		
		
		return students;
	}

}

package com.luv2code.springboot.cruddemo.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "StudentDetails")
public class StudentDetails {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY) 
	@Column(name="StudentID")
	private Integer StudentID;
	
	@Column(name="Name")
	private String Name;
	
	@Column(name="Surname")
	private String Surname;
	
	@Column(name="City")
	private String City;
	
	public StudentDetails() {}

	public String getName() {
		return Name;
	}

	public void setName(String name) {
		Name = name;
	}

	public String getSurname() {
		return Surname;
	}

	public void setSurname(String surname) {
		Surname = surname;
	}

	public String getCity() {
		return City;
	}

	public void setCity(String city) {
		City = city;
	}

	public StudentDetails(String name, String surname, String city) {
		Name = name;
		Surname = surname;
		City = city;
	}

	@Override
	public String toString() {
		return "StudentDetails [Name=" + Name + ", Surname=" + Surname + ", City=" + City + "]";
	}
	
	

}

package com.luv2code.springboot.cruddemo.rest;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.luv2code.springboot.cruddemo.dao.StudentDetailDAO;
import com.luv2code.springboot.cruddemo.entity.StudentDetails;

@RestController
@RequestMapping("api")
public class StudentDetailsController {

	private StudentDetailDAO studentDetailDAO;
	
	public StudentDetailsController(StudentDetailDAO theStudentDetailDAO)
	{
		this.studentDetailDAO = theStudentDetailDAO;
	}
	
	@GetMapping(value ="/students")
	public List<StudentDetails> findAll() {
		return studentDetailDAO.findAll();
	}
}

以下是我在其中添加 MS Sql 服务器详细信息的 application.properties。

spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=India 
spring.datasource.username=sa
spring.datasource.password=Temp1234
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.properties.hibernate.format_sql = true
spring.jpa.hibernate.ddl-auto = update

【问题讨论】:

  • 1) 为什么使用 Hibernate 而不是 Spring Data JPA?但这不是问题。将 logging.level.SQL=debug 添加到 application.properties 并查看生成的 SQL
  • 添加日志后的查询如下: select studentdet0_.studentid as studenti1_0_, studentdet0_.city as city2_0_, studentdet0_.name as name3_0_, studentdet0_.surname as surname4_0_ from student_details studentdet0_ Hibernate: select studentdet0_.studentid as studenti1_0_, studentdet0_.city 作为 city2_0_, studentdet0_.name 作为 name3_0_, studentdet0_.surname 作为 surname4_0_ from student_details studentdet0_
  • 为什么将我的表名更改为 'student_details' ????它应该是 'StudentDetails' :(
  • 我在以下链接中得到了答案:stackoverflow.com/questions/29087626/…
  • 所以请将此作为答案发布

标签: hibernate spring-boot jpa spring-data-jpa


【解决方案1】:

StudentDetailsController 类中为private StudentDetailDAO studentDetailDAO; 添加Autowired 注解,所以会有:

@Autowired
private StudentDetailDAO studentDetailDAO;

【讨论】:

  • 那么在你的日志中没有异常?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多