0
我的spring,hibernate app有問題。我正在嘗試使用spring security進行登錄,並且在將我的用戶帳戶查詢映射到數據庫時遇到了一些麻煩。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>
當我使用這個查詢「FROM UserAccount as ua WHERE ua.username ='」+ username +「'」時,結果waqs相同,它沒有經過「test2」。當我嘗試nativeQuery時,我確實從hibernate獲得了結果,但它沒有從DB獲得任何結果(所以我可能有DB conf錯誤?)。即使我使用本機查詢工作,我也不想堅持這些。還不確定如何在EntityManager中正確使用preparedStatemtn,但我會繼續努力。 – Kapaacius 2013-04-28 09:09:02