2013-04-27 3 views
0

봄, 최대 절전 모드 응용 프로그램에 문제가 있습니다. 나는 봄 보안과 인스턴트 메신저로 DB를 작업하는 데 내 사용자 계정 쿼리를 얻는 데 약간의 어려움을 겪고 로그인하려고 노력하고있어.spring, hibernate, postgres 데이터베이스 쿼리

문제는 내 코드가 "test1"에 도달하지만 "test2"에 도달하지 않으며 콘솔에 오류가 발생하지 않고 앱이 계속 실행되므로 문제가 무엇인지 알 수 없습니다.

"로그인"버튼을 누르면 로그인 실패 페이지로 이동합니다. 또한 나는 봄과 겨울잠에 새로운 것을 지적 할 것이다.

누구나 내가 뭘 잘못하고 있는지 아이디어가 있습니까?

UserAccountService.java

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) 
    { 
     Query query = entityManager.createQuery("FROM user_account as ua WHERE ua.id="+id); 
     return (UserAccount) query.getSingleResult(); 
    } 

    public UserAccount get(String username) 
    { 
     System.out.println("test1"); 
     Query query = entityManager.createQuery("FROM user_account as ua WHERE ua.username='"+username+"'"); 
     System.out.println("test2"); 
     return (UserAccount) query.getSingleResult(); 
    } 

    public void add(UserAccount userAccount) 
    { 
     entityManager.persist(userAccount); 
    } 
} 

쿼리가 잘못되었다는 내가 감지 할 수 있습니다 귀하의 게시물을 읽는 중 처음으로 LoginService.java

package main.java.services; 

import javax.annotation.Resource; 

import main.java.model.UserAccount; 
import main.java.security.CustomUserDetails; 
import main.java.security.UserGrantedAuthority; 

import org.springframework.security.core.GrantedAuthority; 
import org.springframework.security.core.userdetails.UserDetails; 
import org.springframework.security.core.userdetails.UserDetailsService; 

public class LoginService implements UserDetailsService{ 

    @Resource(name="userAccountService") 
    private UserAccountService userAccountService; 

    public LoginService(){ } 

    public UserDetails loadUserByUsername(String username){ 
    if (username != null && !username.equals("")){ 
     UserAccount userAccount = userAccountService.get(username); 
     System.out.println(userAccount); 
     if (userAccount == null) { 
      return null; 
     } 
     GrantedAuthority grantedAuth = new UserGrantedAuthority(userAccount.getAuthority()); 
     System.out.println(userAccount.getId() + userAccount.getAuthority()+userAccount.getPassword()); 
     return new CustomUserDetails(userAccount.getId(), userAccount.getUsername(), userAccount.getPassword(), new GrantedAuthority[]{ grantedAuth }); 
    } else { 
     return null; 
    } 
} 
} 

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> 

답변

2

... 쿼리는 SQL 쿼리입니다. 이 경우 createQuery() 대신 createNativeQuery() 을 사용해야합니다. 적절한 쿼리 (나는 당신에게 수업을 모르는 가정)한다 : UserAccount 클래스 테이블의 이름이 아닌의 이름입니다

Query query = entityManager.createQuery("SELECT us FROM UserAccount as ua WHERE ua.username='"+username+"'"); 

합니다. 또한 쿼리에 인수를 전달하기 위해 준비된 명령문 (google it)을 사용하는 것이 더 좋습니다.

+0

"FRA UserAccount as ua where ua.username = '"+ username + "'"쿼리 결과가 동일하지만 "test2"를 지나치지 않았습니다. 내가 nativeQuery를 tryed했을 때, 나는 최대 절전 모드에서 결과를 얻었지만, DB로부터 어떠한 결과도 얻지 못했다. (그래서 나는 DB conf 에러가있을 수있다?). 네이티브 쿼리를 작동 시키더라도, 나는 그것들을 고수하고 싶지 않을 것입니다. EntityManager에서 preparedStatemtn을 올바르게 사용하는 방법은 아직 확실하지 않지만 계속 작업 할 것입니다. – Kapaacius