问题描述
应用程序:Spring + Hibernate + Apache CXF-简单的打个招呼服务,它采用一个参数-数据库中对象的ID,并返回文本"Hello:" + person.getName();
Application: Spring + Hibernate + Apache CXF - idea simple say hello service which is taking one param - id of object in database and return text "Hello: " + person.getName();
以下是一些代码:
@Entity
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String login;
private String email;
//getters and setters
}
PersonDAO:公共接口PersonDAO { public void savePerson(Person person); public Person getPerson(long id);}
PersonDAO:public interface PersonDAO { public void savePerson(Person person); public Person getPerson(long id);}
PersonDAOImpl:
PersonDAOImpl:
@Repository
@Transactional
public class PersonDAOImpl implements PersonDAO {
@Autowired
private SessionFactory sessionFactory;
public void savePerson(Person person) {
sessionFactory.getCurrentSession().save(person);
}
public Person getPerson(long id){
Hibernate.initialize(sessionFactory);
return (Person) sessionFactory.getCurrentSession().load(Person.class, id);
}
}
PersonWebService:
PersonWebService:
@WebService
public interface PersonWebService {
public String sayHello(long id);
}
PersonWebServiceImpl:
PersonWebServiceImpl:
@WebService(endpointInterface="com.robert.example.testowy.webservices.PersonWebService")
public class PersonWebServiceImpl implements PersonWebService{
@Autowired
private PersonDAO personDAO;
public String sayHello(long id) {
Person person = personDAO.getPerson(id);
return "Hello: " + person.getLogin();
}
}
以及来自xml(dataSurce,sessionFactory)的数据源conf:
And data source conf from xml (dataSurce, sessionFactory):
<context:annotation-config />
<context:component-scan base-package="com.robert.example.testowy" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/edyplom"
p:username="root"
p:password="sedes" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<value>com.robert.example.testowy.domain</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
我找到了一些解决方案,但对我不起作用.感谢您的帮助建议和解决方案.
I have found some solutions but it does not work for me. I appreciate any help suggestions and of course solutions.
推荐答案
好吧,我错了地方.在我的情况下,WebService方法sayHello应该是事务性的.在sayHello上添加注解@Transactional之后效果很好.
Ok I got the place where I have a mistake... WebService method in my case sayHello, should be transactional... after adding annotation @Transactional over sayHello works fine.
这篇关于调用WebService期间发生错误-无法初始化代理-没有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!