带注释的Hibernate Spring没有正确返回对象 - 模板免费,无弹簧dao

时间:2012-03-07 22:35:16

标签: 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.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 = currentSession()。createQuery(“来自com.gasx.model.Vote,其中id =?”); 返回一个对象,但它似乎无法正常运行(我的日志记录停止)。 Config看起来像这样。第一次使用注释。

<?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.impl;

import org.apache.log4j.Logger;

import com.client.dao.VoteDao;
import com.client.model.Vote;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class VoteDaoImpl implements VoteDao{
private static Logger logger=Logger.getLogger(VoteDaoImpl.class);
//private Vote voteBean;
private SessionFactory sessionFactory;

/**
 * @return the sessionFactory
 */

private Session currentSession(){
    return sessionFactory.getCurrentSession();
}

@Autowired
public VoteDaoImpl(SessionFactory sessionFactory){
    this.sessionFactory=sessionFactory;
}

public Vote getVote(int id){
    logger.info("getVote");
    //Query query=currentSession().createQuery("from com.client.model.Vote where id=?");
    //setParameter(0, id).list().get(0);

    Session session = currentSession();
    Vote vote=(Vote)session.get(Vote.class, id);


/*      if (vote==null){
        logger.info("Object was null");
    }else{
        logger.info("Object isn't null!");
    }
    */
    logger.info("Vote total: " + String.valueOf(vote.getVoteTotal()));
    return vote;
}

public void updateVote(Vote vote){
    currentSession().update(vote);      
    currentSession().flush();
}

/*
 * 
 * incements the vote.id=id 
 * 
 */
public void incrementVote(int id){
    logger.info(" increment vote");
    Vote vote=getVote(id);
    vote.setVoteTotal(vote.getVoteTotal()+1);
    updateVote(vote);

}
}

模型

package com.client.model;
import com.client.dao.*;
import javax.persistence.Column;
import javax.persistence.Entity;
//import org.hibernate.annotations.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="vote")
public class Vote {

@Id
@Column(name="id")
private int id;

@Column(name="vote_total")
private int voteTotal;

/**
 * @return the id
 */

public int getId() {
    return id;
}

/**
 * @param id the id to set
 */
public void setId(int id) {
    this.id = id;
}

/**
 * @return the voteTotal
 */

public int getVoteTotal() {
    return voteTotal;
}

/**
 * @param vote1Total
 *            the vote1Total to set
 */
public void setVoteTotal(int voteTotal) {
    this.voteTotal = voteTotal;
}
}

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);
}

有什么想法吗?我很难过。如果您需要任何信息,请告诉我。

2 个答案:

答案 0 :(得分:1)

项目没问题......问题是我忘记了在oracle中你必须承诺。我正在插入我的种子数据,但应用程序无法看到它,因为它从未提交过。简直不敢相信我做到了......自从我与甲骨文合作以来已经有几年了。

答案 1 :(得分:0)

欢迎,

String XML配置文件会覆盖Spring注释配置,因此您必须使用以下命令编辑Spring XML文件:

<bean id="voteDao" class="com.client.dao.impl.VoteDaoImpl">
   <constructor-arg><ref bean="sessionFactory"/></constructor-arg>
</bean>