gpt4 book ai didi

java - 如何在 Spring boot 中使用 JPA 中的 Criteria?

转载 作者:行者123 更新时间:2023-11-30 05:22:54 25 4
gpt4 key购买 nike

我正在使用 JPA 和 Spring Boot。我是 JPA 新手。我想通过传递函数的值来检索对象。但在示例中,它是通过 hibernate 配置完成的。我没有在类路径中配置 sessionFactory bean 。我想使用 JPA 来检索对象。在示例中,它是:

// Transactional for Hibernate
@Transactional
public class AccountDAOImpl implements AccountDAO {

@Autowired
private EntityManager entityManager;

@Override
public Account findAccount(String userName) {
Session session = sessionFactory.getCurrentSession();
Criteria crit = session.createCriteria(Account.class);
crit.add(Restrictions.eq("userName", userName));
return (Account) crit.uniqueResult();
}
}

但是我在 JPA 中尝试过这样的:

// Transactional for Hibernate
@Transactional
public class AccountDAOImpl implements AccountDAO {

@Autowired
private EntityManager em;

@Override
public Account findAccount(String userName) {
CriteriaBuilder cm= em.getCriteriaBuilder();
cm.createQuery(Account.class);
cm.add(Restrictions.eq("userName", userName));
return (Account) crit.uniqueResult();
}
}

但它显示错误,我如何使用 JPA 传递用户名?

我的 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 https://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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ashwin</groupId>
<artifactId>vemployee</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>vemployee</name>
<description>Demo project for Spring Boot for offc</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>

<!-- Spring Security Core -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>

<!-- Spring Security Config -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>

<!-- Spring Security Web -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<!-- https://mvnrepository.com/artifact/jstl/jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- needed for jsp -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.27</version>
</dependency>

<!--bootsrap and jquery-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.webjars/bootstrap-datepicker -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap-datepicker</artifactId>
<version>1.7.1</version>
</dependency>

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

我也尝试过如下:

    TypedQuery<Account> query=em.createQuery("Select a from Account a where a.userName = :userName",Account.class);
query.setParameter(1,userName);
Account acc=query.getResultList().get(0);
return acc;

但是这里如何设置用户名呢?

最佳答案

你可以像这样使用JPQL

@Override
public Account findAccount(String userName) {
return em.createQuery("select account from Account account where account.username = :username", Account.class)
.setParameter("username", userName).getSingleResult();
}

关于java - 如何在 Spring boot 中使用 JPA 中的 Criteria?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59236620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com