0

我的 spring、hibernate 应用程序有问题。我正在尝试使用 Spring Security 进行登录,并且在让我的用户帐户查询到 DB 工作时遇到了一些问题。

问题是我的代码将到达“test1”,但它不会到达“test2”,并且由于我没有收到任何错误到控制台并且应用程序将继续运行,我不知道问题可能是什么。

当我按下“登录”按钮时,我将定向到登录失败页面。另外我会指出我是spring和hibernate的新手。

有人知道我做错了什么吗?

用户帐户服务.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);
    }
}

登录服务.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>
4

1 回答 1

2

第一次阅读您的帖子,我可以检测到您的查询是错误的......您的查询是一个 sql 查询。在这种情况下,您应该使用 createNativeQuery() 而不是 createQuery()。正确的查询是(假设我不知道你的类):

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

其中 UserAccount 是类的名称,而不是表的名称。此外,最好使用准备好的语句(google it)将参数传递给查询。

于 2013-04-27T17:51:07.577 回答