私は約8時間の研究を費やしており、解決策を見つけることができないため、ここで私の最初の投稿を作成しています!注釈付きHibernate Springはオブジェクトを正しく返さない - テンプレートフリー、スプリングフリーdao
オブジェクトが何も返さないため、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)
:。 クエリクエリを= 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.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);
}
任意のアイデアはありますか?私は困惑している。情報が必要な場合は、私に知らせてください。
ようこそ。どの行が51行目( 'com.client.dao.impl.VoteDaoImpl.getVote(VoteDaoImpl.java:51)')ですか? – Ralph
DAOでSessionFactoryを使うのではなく、HibernateTemplateを使う方が簡単だと思います。 –
Hibernateテンプレートは必要ありません。私はhibernateTemplatesを使ってみました。 – JasonG