从Spring Boot的JPA查询中获取null值。

huangapple go评论168阅读模式
英文:

Getting null from JPA query in Spring Boot

问题

我在这个Spring Boot项目中的JPA查询中一直得到null值。以下是该项目的代码部分:

实体(Entity):

  1. @Entity
  2. public class Admin {
  3. @Id
  4. private String login;
  5. @Column
  6. private String Password;
  7. public String getLogin() {
  8. return login;
  9. }
  10. public void setLogin(String login) {
  11. this.login = login;
  12. }
  13. public String getPassword() {
  14. return Password;
  15. }
  16. public void setPassword(String password) {
  17. Password = password;
  18. }
  19. }

仓库(Repository):

  1. @Repository
  2. public interface AdminRepository extends JpaRepository<Admin, String> {
  3. @Query(value = "SELECT Password FROM Admin WHERE login = :login", nativeQuery = true)
  4. String getp(@Param("login") String login);
  5. }

控制器(Controller):

  1. @Controller
  2. public class AdminController {
  3. @Autowired
  4. AdminRepository adminRepo;
  5. @RequestMapping("/")
  6. public String home() {
  7. return "index";
  8. }
  9. @RequestMapping("/adminlogin")
  10. public String adminlogin(Model m) {
  11. m.addAttribute("command", new Admin());
  12. return "adminlogin";
  13. }
  14. @RequestMapping("/alogin")
  15. public String login(@RequestParam String login, @RequestParam String password, Model m){
  16. String p = adminRepo.getp(login);
  17. if(password.equals(p)) {
  18. return "admin";
  19. }
  20. else {
  21. return "afail";}
  22. }
  23. }

我之前在许多项目中使用了相同的结构和查询,但这是第一次出现这种情况。所有内置查询和我编写的本地查询都返回null值,但在数据库中相同的查询显示已经填充了数据。

我不知道为什么会发生这种情况,如果有人有任何想法,请随时分享。

英文:

I have been getting null for my JPA queries in this Spring Boot Project. This is the project:

Entity:

  1. @Entity
  2. public class Admin {
  3. @Id
  4. private String login;
  5. @Column
  6. private String Password;
  7. public String getLogin() {
  8. return login;
  9. }
  10. public void setLogin(String login) {
  11. this.login = login;
  12. }
  13. public String getPassword() {
  14. return Password;
  15. }
  16. public void setPassword(String password) {
  17. Password = password;
  18. }

Repository:

  1. @Repository
  2. public interface AdminRepository extends JpaRepository&lt;Admin, String&gt; {
  3. @Query(value = &quot;SELECT Password FROM Admin WHERE login = :login &quot;, nativeQuery = true)
  4. String getp(@Param(&quot;login&quot;) String login);

Controller:

  1. @Controller
  2. public class AdminController {
  3. @Autowired
  4. AdminRepository adminRepo;
  5. @RequestMapping(&quot;/&quot;)
  6. public String home() {
  7. return &quot;index&quot;;
  8. }
  9. @RequestMapping(&quot;/adminlogin&quot;)
  10. public String adminlogin(Model m) {
  11. m.addAttribute(&quot;command&quot;, new Admin());
  12. return &quot;adminlogin&quot;;
  13. }
  14. @RequestMapping(&quot;/alogin&quot;)
  15. public String login(@RequestParam String login,@RequestParam String password,Model m){
  16. String p = adminRepo.getp(login);
  17. if(password.equals(p)) {
  18. return &quot;admin&quot;;
  19. }
  20. else {
  21. return &quot;afail&quot;;}
  22. }
  23. }

I have used same structure and query in many projects before but this is happening for first time. All the queries both inbuilt queries and the native queries that I wrote are giving null but the same queries in the database shows that it is populated.

I have no idea why this is happening, If any one have any thought, please, feel free to share.

答案1

得分: 0

我解决了这个问题。正如我之前提到的,我在许多其他项目中都使用了相同的方法,它通常都能正常工作,因此,如果我没有做错什么,这次也应该能正常工作。我的通常做法是首先创建一个实体,然后创建存储库,然后运行应用程序,这将在数据库中创建表,只有在此之后我才创建控制器和用户界面。

这次发生的是,我不小心在创建实体后立即运行了应用程序,这导致在数据库中创建了表。当时我认为没问题,没有特别注意,直到现在才意识到表和存储库没有连接。然后我删除了表格,再次运行应用程序创建表格,这次它像魔法一样正常工作。

所以结论是:如果在创建表之后创建存储库,查询将无法执行。只有在创建存储库之后才能创建表。

英文:

I cracked the problem. As I mentioned before that I have used same method in many other projects and it always used to work, so, there wasn't a reason that it wouldn't work this time, that is, if I didn't do something wrong, something different this time.

My usual practice is to first create a entity then the repository then I run the application which creates the table in the database, only after which I create the controller and the user Interface.

What happened this time was that I accidentally ran the application right after I created the entity, which resulted in the creation of the table in database. At the time I thought it was fine and didn't paid any attention to it, only now I realize the table and the repository were not connected. I then dropped the table and ran the app to create it again and it worked like charm.

So here is the conclusion:If the repository is created after the table, the queries will not go through. Only after the repository is created, the table must be created.

huangapple
  • 本文由 发表于 2023年3月7日 20:33:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定