[英]Spring data jpa repository findBy returning null even if data present in DB
I am using spring data JPA, facing issue as result is always coming NULL even if record present into DB.我正在使用 spring 数据 JPA,面临问题,因为结果总是出现 NULL,即使记录存在于 DB 中。
I tried debugging spring code and checked whether statement created correctly or not.我尝试调试 spring 代码并检查语句是否正确创建。 Statement is created correctly and parameter is also populated correctly.语句创建正确,参数也正确填充。
Configuration: - Spring-boot = 2.0.5.RELEASE配置:- Spring-boot = 2.0.5.RELEASE
Oracle DB = Oracle Database 11g Release 11.2.0.2.0 - 64bit Oracle DB = Oracle 数据库 11g 版本 11.2.0.2.0 - 64 位
POM聚甲醛
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0</version>
</dependency>
</dependencies>
Entity class:实体 class:
@Entity()
public class User {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "user_Sequence")
@SequenceGenerator(name = "user_Sequence", sequenceName = "USER_SEQ")
private Long id;
@Column(unique=true)
private String userName;
private String password;
//getter & setter
Repository class存储库 class
@Repository
public interface UserRepository extends
org.springframework.data.repository.CrudRepository<User, Long>{
User findByUserName(String userName);
User save(User user);
}
Service class服务 class
@Service("loginService")
public class LoginService implements ILoginService {
@Autowired
UserRepository userRepository;
@Override
public boolean isValidUser(String userName, String password) {
User user = userRepository.findByUserName(userName);
if(user != null && user.getUserName().equals(userName) && user.equals(password)){
return true;
} else {
return false;
}
}
}
Debug Screenshot: - Debug Screenshot parameter value is "admin" and SQL formed Debug Screenshot: - Debug Screenshot 参数值为“admin”,SQL 形成
SQL Formed: - SQL 形成:-
select user0_."id" as id1_2_, user0_."password" as password2_2_, user0_."user_name" as user_name3_2_ from "user" user0_ where user0_."user_name"=?
If I run same query in DB, result is: - Result Screenshot如果我在数据库中运行相同的查询,结果是: -结果截图
Since you are not using your column name as your field names it needs to know what to map to in your entity.由于您没有使用列名作为字段名,因此需要知道要映射到实体中的内容。
Change your field to either be user_name
or change:将您的字段更改为user_name
或更改:
@Column(unique=true)
private String userName;
to到
@Column(name="user_name", unique=true)
private String userName;
thus it will be able to know what column it references;因此它将能够知道它引用的列;
https://www.concretepage.com/spring-5/spring-data-crudrepository-example https://www.concretepage.com/spring-5/spring-data-crudrepository-example
在存储库中使用 findByNameEqualsAndStatus
Below are things that work for me.以下是对我有用的东西。 I am using Oracle 19C with org.hibernate.dialect.Oracle12cDialect.我正在使用 Oracle 19C 和 org.hibernate.dialect.Oracle12cDialect。
grant all on <schema>.<table> to <user>;
grant all on <schema>.<sequence> to <user>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.