spring,hibernate,postgres数据库查询

时间:2013-04-27 16:30:19

标签: spring hibernate postgresql

我的spring,hibernate app有问题。我尝试使用spring security进行登录,并且我很难将我的用户帐户查询发送到DB工作。

问题是我的代码将达到" test1"但它无法达到" test2"因为我没有得到任何错误的控制台,而且应用程序将继续运行我不知道问题可能是什么。

当我按下#34;登录"按钮,我将指示登录失败的页面。我还指出我是春天和冬眠的新手。

任何人都有任何想法,我做错了什么?

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>

1 个答案:

答案 0 :(得分:2)

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

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

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