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

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

Getting null from JPA query in Spring Boot

问题

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

实体(Entity):

@Entity
public class Admin {
	
	@Id
	private String login;
	
	@Column
	private String Password;

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return Password;
	}

	public void setPassword(String password) {
		Password = password;
	}
}

仓库(Repository):

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

控制器(Controller):

@Controller
public class AdminController {
	
	@Autowired
	AdminRepository adminRepo;
	
	@RequestMapping("/")
    public String home() {
        return "index";
    }
	
	@RequestMapping("/adminlogin")
	public String adminlogin(Model m) {
		m.addAttribute("command", new Admin());
		return "adminlogin";
	}
	
	@RequestMapping("/alogin")
    public String login(@RequestParam String login, @RequestParam String password, Model m){    
    	String p = adminRepo.getp(login);
    	
    	if(password.equals(p)) {
    		return "admin";
    	}
    	else {
        return "afail";}
    }
}

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

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

英文:

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

Entity:

@Entity
public class Admin {
	
	@Id
	private String login;
	
	@Column
	private String Password;

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return Password;
	}

	public void setPassword(String password) {
		Password = password;
	}

Repository:

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

Controller:

@Controller
public class AdminController {
	
	@Autowired
	AdminRepository adminRepo;
	
	@RequestMapping(&quot;/&quot;)                     
    public String home() {
        return &quot;index&quot;;           
    }
	
	@RequestMapping(&quot;/adminlogin&quot;)
	public String adminlogin(Model m) {
		m.addAttribute(&quot;command&quot;, new Admin());
		return &quot;adminlogin&quot;;
	}
	
	@RequestMapping(&quot;/alogin&quot;)    
    public String login(@RequestParam String login,@RequestParam String password,Model m){    
    	String p = adminRepo.getp(login);
    	
    	if(password.equals(p)) {
    		return &quot;admin&quot;;
    	}
    	else {
        return &quot;afail&quot;;}
    }

}

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:

确定