我正在尝试构建简单的spring security hibernate登录应用程序。但是我有一个问题是使用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);
}
}
我的问题是,代码将达到“test1”,但它不会达到“test2”,也不会向我显示任何错误。我已尝试使用本机查询,然后我将获得一个hibernate awnser来控制,但仍然没有结果我返回(给0,但应该返回一个UserAccount对象)。使用本机查询控制台显示我这个“Hibernate:FROM user_account WHERE username ='something'” 这是我的数据库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>
答案 0 :(得分:0)
如果执行到达行x
但不到行x+2
,则行x+1
必须引发异常,无限制地阻塞/循环,强制终止线程,或调用vm退出例程
其中,最有可能是它引发了例外。如果您没有看到错误,则可能有代码吞噬了异常,例如:
try {
...
} catch {
// Ignore exception
}
要不然,或者它可能会被记录到某个地方并且你没有看到日志。检查应用服务器日志以获取详细信息你没有提到你正在使用的应用服务器,所以我不能更具体。
如果您无法以这种方式识别问题,请将调试器附加到您的应用程序(是的,当您的应用程序在容器/应用程序服务器中运行时,这是有效的)。在最后一个已知的OK语句中设置断点,然后逐步查看代码,看看会发生什么。