集成Struts和Hibernate

时间:2014-08-31 16:16:29

标签: hibernate struts2

我正在尝试集成Hibernate和Struts2

这是applicationContext.xml

     <bean id="user" class="com.caveofprogramming.service.imp.UserImp" /> 
     <bean id="userSpringAction" class="com.caveofprogramming.actions.UserSpringAction"> 
    <property name="user" ref="user" /> </bean>

    <bean id="customerDAO" class="com.caveofprogramming.entity.CustomerDAO">
    <property name="transactionManager" ref="transactionManager" /> </bean>

    <bean id="customerEntity" class="com.caveofprogramming.entity.Customer" />

     <bean id="customerAction" class="com.caveofprogramming.actions.CustomerAction"> 
    <property name="customerEntity" ref="customerEntity" /> 
    <property name="customerDAO" ref="customerDAO" /> 
    </bean>

    <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
            p:location="/WEB-INF/jdbc.properties" />

        <bean id="dataSource"
            class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
            p:driverClassName="${jdbc.driverClassName}"
            p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
            p:password="${jdbc.password}" />

        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation">
                <value>classpath:hibernate.cfg.xml</value>
            </property>
            <property name="configurationClass">
                <value>org.hibernate.cfg.AnnotationConfiguration</value>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
        </bean>
        <tx:annotation-driven />
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean> 

    </beans>

我已经拥有了我的jdbc.properties并在DAO中保存了我的客户

CustomerDAO

public class CustomerDAO {
    @Autowired
    private SessionFactory sessionFactory;

    public void persistAuthor(Customer customer) {
        sessionFactory.getCurrentSession().save(customer);
    }
}

这是实体类

@Table(name="customer")
@Entity
public class Customer {
    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private String password;
    public Integer getId() {
        return id;
    }
//Getter/Setters//
}

这是我的动作类

public class CustomerAction extends ActionSupport {
    private static final Logger logger = Logger.getLogger(CustomerAction.class);
    Customer customer;
    CustomerDAO customerdao;
    private Integer id;
    private String name;
    private String password;

//GETTERS/SETTERS//

    @Action(value="/customer", results={
            @Result(name="success",location="/customerSuccess.jsp"),
        })
    public String execute() throws Exception {
        logger.debug("Hello");
    customer.setId(getId());
    customer.setName(getName());
    customer.setPassword(getPassword());
    customerdao.persistAuthor(customer);
    return SUCCESS;
}

然而,它向我展示了这个例外

异常

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.IllegalStateException: AnnotationTransactionAttributeSource is only available on Java 1.5 and higher
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:420)

1 个答案:

答案 0 :(得分:0)

Spring框架不支持您的Java版本,因为TxNamespaceUtils它可能是一个也报告为Bug 1090968的错误。建议您不要使用具有最新Java 8的旧库。尝试升级某些库,尤其是Spring和Hibernate,并将配置更改为不使用已弃用的API。