0

I am newbie in Spring.I am trying to get some data from a MYSQL database. Unfortunately spring data jpa returns empty list. But one record exist in db: How can I access this record?

Mysql mysql2

libraryRepository.java

public interface LibraryRepository extends JpaRepository<Library, String>,LibraryRespositoryCustom{

}

LibraryController.java

@RestController
public class LibraryController {

@Autowired
LibraryRepository repository;

@Autowired
LibraryService libraryService;

@GetMapping("/getAllBooks")
public ResponseEntity<List<Library>> getAllBooks(){
    return ResponseEntity.ok(repository.findAll());
}

application.properties

# Datasource
spring.datasource.url=jdbc:mysql://localhost:3306/APIDevelopSpringBoot?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#com.mysql.jdbc.Driver




spring.jpa.generate-ddl=true

# Jpa/Hibernate :
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

#spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto = update
spring.test.database.replace=none



#Generate Logs
logging.file.name=log/application.log

#spring.profiles.active=dev

spring.main.allow-circular-references: true

Library.java

@Data
@Entity
@Table(name="LibraryDemo")
public class Library {
    
    @Column(name="book_name")
    private String book_name;
    @Id
    @Column(name="id")
    private String id;
    @Column(name="isbn")
    private String isbn;
    @Column(name="aisle")
    private int aisle;
    @Column(name="author")
    private String author;

}
4
  • Double check your table to make sure it really has data. Commented Feb 27, 2022 at 11:47
  • Select query gives one record table has data @Tim Biegeleisen Commented Feb 27, 2022 at 14:11
  • You have spring.jpa.show-sql=true, so the real query that JPA sends to db must be in the log. Please try to run this query directly in the db, do you see your record there? Commented Feb 27, 2022 at 15:04
  • Hibernate: select library0_.id as id1_0_, library0_.aisle as aisle2_0_, library0_.author as author3_0_, library0_.book_name as book_nam4_0_, library0_.isbn as isbn5_0_ from library_demo library0_ Yes, @Vladimir.V.Bvn Commented Feb 27, 2022 at 16:35

2 Answers 2

2
select library0_.id as id1_0_, library0_.aisle as aisle2_0_, library0_.author as author3_0_, library0_.book_name as book_nam4_0_, library0_.isbn as isbn5_0_ from library_demo library0_

As we can see in this query, it is querying on the table library_demo but you have a table named LibraryDemo. So, you have to change the name to library_demo.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to have the table name as library_demo for that to work. Whatever name you give, it gets replaced with _ format.

Try this

@Entity
@Table(name = "library_demo")
public class Library {...}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.