应用程序:Spring + Hibernate + Apache CXF - 想法简单说出问候服务,它在数据库中取一个param-id对象并返回文本“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); }
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:
@WebService
public interface PersonWebService {
public String sayHello(long id);
}
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的数据源conf(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>
我找到了一些解决方案,但它对我不起作用。我感谢任何帮助建议,当然还有解决方案。
答案 0 :(得分:0)
好的,我得到了一个错误的地方...我的案例中的WebService方法sayHello,应该是事务性的......在添加注释@Transactional over sayHello后工作正常。