私は、多少のレベルのコードにある程度変更を加えました。迅速な対応のため、@ M.Deinumと@ v.ladynevに感謝します。 HibernateTemplateからのSessionFactoryに変更しながら、私が出会った
//IndexService
@Transactional
public class IndexService {
User user;
SessionFactory sf;
public IndexService(User user, SessionFactory sf) {
this.user = user;
this.sf = sf;
}
//This method is used to get the number of employees based on users.
public Object noOfEmployees() {
String u = user.getUserName();
final String query = "select count(*) from employee where job=:job";
Query q = sf.getCurrentSession().createSQLQuery(query);
if ("user1".equals(u)) {
q.setParameter("job", "System Analyst");
} else if ("user2".equals(u)) {
q.setParameter("job", "DBA");
}
return q.uniqueResult();
}
--------------------------------------------------------------------------------------
//Index
@Controller
@Transactional
public class Index {
@Autowired
User user;
@Autowired
SessionFactory sf;
@RequestMapping("/index")
public ModelAndView getIndex() {
System.out.println("user.getUserName() In Index = " + user.getUserName());
ModelAndView modelAndView = new ModelAndView("index");
IndexService indexService = new IndexService(user, sf);
modelAndView.addObject("noOfEmployees", indexService.noOfEmployees());
return modelAndView;
}
}
--------------------------------------------------------------------------------------
//spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url"
value="jdbc:mysql://localhost/database_name"></property>
<property name="username" value="user"></property>
<property name="password" value="password"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="basicDataSource"></property>
<property name="mappingResources" value="myBeans.hbm.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
問題:SessionFactoryの
ため
1.NullPointerExceptionを下記の
はSessionFactoryのへのHibernateTemplateから移動するための変更が記載されている唯一のスニペットです
インデックスクラス内、
@Autowired SessionFacto ry sf;
を引数として渡します。
IndexService indexService = new IndexService(user、sf);
2.No HibernateのSessionはスレッドにバインド、およびコンフィギュレーションは、私はまた、インデックスの上に@Transactionalを入れ、ここで、非トランザクション1
を作成することはできません。私のアプリケーションのコントローラです。
もう一度@ M.Deinumと@ v.ladynevありがとうございました!
あなたはquery.list()が立ち往生したと言ったときの意味を教えてください。 – Lexi
その後、コンソールは何も印刷しませんでした。 – user5776462