간단한 봄 보안 최대 절전 모드 로그인 응용 프로그램을 구축하려고합니다. 하지만 최대 절전 모드로 데이터베이스에서 결과를 가져 오는 데 문제가 있습니다. 다음은 쿼리 할 Service 클래스입니다.봄, 최대 절전 모드 데이터베이스 쿼리
UserAccountService :
package main.java.services;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import main.java.model.UserAccount;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("userAccountService")
@Transactional
public class UserAccountService {
private EntityManager entityManager;
@PersistenceContext
public void setEntityManager(EntityManager entityManager){
this. entityManager = entityManager;
}
public UserAccount get(Integer id)
{
String sql = "FROM UserAccount as ua WHERE ua.id="+id;
Query query = entityManager.createQuery(sql);
return (UserAccount) query.getSingleResult();
}
public UserAccount get(String username)
{
String sql = "FROM UserAccount WHERE username='"+username+"'";
System.out.println("test1");
Query query = entityManager.createQuery(sql);
System.out.println("test2");
System.out.println(query.getSingleResult());
return (UserAccount) query.getSingleResult();
}
public void add(UserAccount userAccount)
{
entityManager.persist(userAccount);
}
}
내 문제는 코드가 "TEST1"를 도달 할 것입니다, 그러나 "TEST2"를 도달 할 것이며, 그것이 나에게 오류를 표시하지 않습니다. 나는 네이티브 질의를 시도했지만, 그때까지 콘솔에 최대 절전 모드를 가져올 것이다.하지만 여전히 내 반환 값은 0이다.하지만 나에게 UserAccount 객체를 리턴해야한다. 기본 쿼리 콘솔 쇼와 함께이 "최대 절전 모드 : USER_ACCOUNT FROM = '뭔가'이름 WHERE"나야 다음은 데이터베이스 내 conf의 파일입니다 :
HibernateContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:property-placeholder location="/WEB-INF/jdbc.properties" />
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Declare a datasource that has pooling capabilities-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="${jdbc.driverClassName}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}"
p:acquireIncrement="5"
p:idleConnectionTestPeriod="60"
p:maxPoolSize="100"
p:maxStatements="50"
p:minPoolSize="10" />
<!-- Declare a JPA entityManagerFactory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
<property name="persistenceUnitName" value="hibernatePersistenceUnit" />
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" >
<property name="databasePlatform">
<value>${jdbc.dialect}</value>
</property>
<property name="showSql" value="true"/>
</bean>
</property>
</bean>
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
try catch 블록을 추가하면 "UserAccount가 [UserAccount uu.username = : username]에 매핑되지 않았습니다."라는 오류 메시지가 표시됩니다.이 오류를 지금 어떻게 수정해야할지 확실하지 않습니다. D – Kapaacius