0

I am new to Java and Spring, I was doing simple CRUD project for one of the table in my Sql Server. But it does not return any results in get query. Following are the code files:

Following is the 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>

Following is the CruddemoApplication.java file which is the starter file:

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();
 }
}

Following is the application.properties in which I am adding MS Sql server details.

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
user2746466
  • 361
  • 5
  • 23
  • 1) Why are you using Hibernate and not Spring Data JPA? But that's not the problem. Add logging.level.SQL=debug to application.properties and have a look at the generated SQL – Simon Martinelli Mar 29 '19 at 13:14
  • Following are the queries after adding logs: 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 as city2_0_, studentdet0_.name as name3_0_, studentdet0_.surname as surname4_0_ from student_details studentdet0_ – user2746466 Mar 29 '19 at 13:20
  • Why it changing my table name to 'student_details' ???? It should be 'StudentDetails' :( – user2746466 Mar 29 '19 at 13:25
  • I got the answer in following link: https://stackoverflow.com/questions/29087626/entity-class-name-is-transformed-into-sql-table-name-with-underscores – user2746466 Mar 29 '19 at 13:36
  • So please post this as answer – Simon Martinelli Mar 29 '19 at 13:47
  • I got the answer in following link:
    https://stackoverflow.com/questions/29087626/entity-class-name-is-transformed-into-sql-table-name-with-underscores
    – user2746466 Mar 29 '19 at 14:13
  • Possible duplicate of [Entity Class name is transformed into SQL table name with underscores](https://stackoverflow.com/questions/29087626/entity-class-name-is-transformed-into-sql-table-name-with-underscores) – Simon Martinelli Mar 29 '19 at 14:23

1 Answers1

0

add Autowired annotation to private StudentDetailDAO studentDetailDAO; in the StudentDetailsController class, so will have:

@Autowired
private StudentDetailDAO studentDetailDAO;
Ismail
  • 2,322
  • 1
  • 12
  • 26