0
應用程序:Spring + Hibernate + Apache CXF - 想法簡單的說出hello服務,它將數據庫中的一個參數 - 對象的id和返回文本「Hello:」+ person。的getName();在調用WebService期間出錯 - 無法初始化代理 - 沒有會話
下面是一些代碼:
@Entity
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String login;
private String email;
//getters and setters
}
的PersonDAO: 公共接口的PersonDAO { 公共無效savePerson(人的人); 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:從XML
@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();
}
}
和數據源配置(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>
我找到了一些解決方案,但它不適用於我。我感謝任何幫助建議和當然的解決方案。
交易在哪裏開始?我沒有在PersonWebServiceImpl中看到與Transaction有關的註釋嗎? – Adi 2012-03-06 22:59:39
PaersonDAO被標記爲ttansactional。 – Robert 2012-03-07 05:24:49