带注释的Hibernate Spring未正确返回对象-无模板,无Spring dao
我花了大约8个小时研究,但我找不到解决方案,所以我在这里创建了我的第一篇帖子 我在点击getVote函数时遇到以下错误,因为对象没有返回任何内容带注释的Hibernate Spring未正确返回对象-无模板,无Spring dao,spring,hibernate,annotations,Spring,Hibernate,Annotations,我花了大约8个小时研究,但我找不到解决方案,所以我在这里创建了我的第一篇帖子 我在点击getVote函数时遇到以下错误,因为对象没有返回任何内容 java.lang.NullPointerException at com.client.dao.impl.VoteDaoImpl.getVote(VoteDaoImpl.java:51) at com.client.dao.impl.VoteDaoImpl.incrementVote(VoteDaoImpl.java:67) at sun.refle
java.lang.NullPointerException
at com.client.dao.impl.VoteDaoImpl.getVote(VoteDaoImpl.java:51)
at com.client.dao.impl.VoteDaoImpl.incrementVote(VoteDaoImpl.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy125.incrementVote(Unknown Source)
at com.client.action.VoteActionImpl.execute(VoteActionImpl.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
这样做:
Query Query=currentSession().createQuery(“来自com.gasx.model.Vote,其中id=?”;
返回一个对象,但它似乎无法正常工作(我的日志记录停止)。
配置看起来像这样。第一次使用注释
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcldb" />
<property name="username" value="client" />
<property name="password" value="client" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--property name="packagesToScan" value="com.client.model" /-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<!--prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop-->
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.client.model.Vote</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="voteDao" class="com.client.dao.impl.VoteDaoImpl" />
<bean id="voteAction" class="com.client.action.VoteActionImpl">
<property name="voteDao" ref="voteDao" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config />
<context:component-scan base-package="com.client.dao.impl" />
</beans>
模型
dao有一个接口
package com.client.dao;
import org.hibernate.classic.Session;
import com.client.model.Vote;
public interface VoteDao {
public Vote getVote(int id);
public void updateVote(Vote vote);
public void incrementVote(int id);
}
有什么想法吗?我被难住了。如果您需要任何信息,请告诉我。欢迎
字符串XML配置文件覆盖Spring注释配置,因此您必须使用以下内容编辑Spring XML文件:
<bean id="voteDao" class="com.client.dao.impl.VoteDaoImpl">
<constructor-arg><ref bean="sessionFactory"/></constructor-arg>
</bean>
项目还可以。。。问题是我忘记了在oracle中,您必须提交。我正在插入种子数据,但应用程序无法看到它,因为它从未提交。真不敢相信我这么做了。。。我与oracle合作已有两年了。欢迎光临。哪一行是第51行(com.client.dao.impl.VoteDaoImpl.getVote(VoteDaoImpl.java:51)
)?我认为在dao中使用HibernateTemplate比使用SessionFactory更容易Hibernate模板不再需要了。不过我确实尝试过使用hibernateTemplates。谢谢你的回复。还是不走运。我尝试了hibernate模板,xml配置。即使hibernate生成将在数据库中实际运行的SQL,我也会返回0个对象。您是否仍然获得NullPointerException?我重写了整个应用程序,尝试了所有可能的方法,最后我认为问题在于,插入种子数据后我没有运行“提交”。。。
package com.client.dao;
import org.hibernate.classic.Session;
import com.client.model.Vote;
public interface VoteDao {
public Vote getVote(int id);
public void updateVote(Vote vote);
public void incrementVote(int id);
}
<bean id="voteDao" class="com.client.dao.impl.VoteDaoImpl">
<constructor-arg><ref bean="sessionFactory"/></constructor-arg>
</bean>