问题
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();
Here are some code:
@Entity
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String login;
private String email;
//getters and setters
}
PersonDAO: public interface 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();
}
}
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.
回答1:
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.
来源:https://stackoverflow.com/questions/9592583/error-during-invoking-webservice-could-not-initialize-proxy-no-session