我想构建简单的spring安全性hibernate登录应用程序。但是我有一个数据库与休眠的geting结果问题。 这是我的服务类,将查询。Spring,Hibernate数据库查询
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);
}
}
我的问题是,该代码将达到“测试1”,但它不会达到“测试2”,它不会给我任何错误。我已经尝试了原生查询,然后我将得到一个hibernate的awnser来控制台,但仍然没有结果返回(给出0,但应该返回一个UserAccount对象)。随着本地查询台演出是我这个“休眠:从USER_ACCOUNT其中username =‘东西’” 这里有一个数据库我的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未映射[FROM UserAccount UA WHERE ua.username =:用户名]”,不知道现在该怎么解决这个错误寿:d – Kapaacius 2013-05-10 14:30:55