在Struts 2 + Spring + Hibernate项目中使用Hibernate Annotation映射

时间:2015-09-09 10:00:53

标签: java hibernate annotations

我正在遵循Mkyong的教程,使用Maven,Struts 2,Hibernate和Spring创建项目(http://www.mkyong.com/struts2/struts-2-spring-hibernate-integration-example

本教程使用XML映射从数据库中获取数据。实际上我读了一些关于比较XML映射和注释映射的文章。

_Current project struct:

enter image description here

enter image description here

_使用XML映射的客户模型来源:

package com.mkyong.customer.model;

import java.util.Date;

public class Customer implements java.io.Serializable {

private Long customerId;
private String name;
private String address;
private Date createdDate;

public Customer() {
}

public Customer(String name, String address, Date createdDate) {
    this.name = name;
    this.address = address;
    this.createdDate = createdDate;
}


public Long getCustomerId() {
    return this.customerId;
}

public void setCustomerId(Long customerId) {
    this.customerId = customerId;
}


public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}


public String getAddress() {
    return this.address;
}

public void setAddress(String address) {
    this.address = address;
}


public Date getCreatedDate() {
    return this.createdDate;
}

public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}

}

_ HibernateSession工厂的源代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Hibernate session factory -->
<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

    <property name="mappingResources">
        <list>
          <value>com/mkyong/customer/hibernate/Customer.hbm.xml</value>          
        </list>
    </property> 

</bean>
</beans>

现在我想更改为使用注释映射,并将模型类更新为:

pakage com.mkyong.customer.model

import java.util.Date;
import javax.persistence.*;

@Entity
@Table(name="customer", catalog = "test")
public class Customer implements java.io.Serializable {

    private Long customerId;
    private String name;
    private String address;
    private Date createdDate;

    public Customer() {
    }

    public Customer(String name, String address, Date createdDate) {
        this.name = name;
        this.address = address;
        this.createdDate = createdDate;
    }

    @Id
    @GeneratedValue
    @Column(name="customer_id")
    public Long getCustomerId() {
        return this.customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    @Column(name="name")
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="address")
    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column(name="created_date")
    public Date getCreatedDate() {
        return this.createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

}

并在HibernateSessionFactory中更新映射部分:

 <property name="mappingResources">
        <list>        
          <value>com.mkyong.customer.model.Customer</value>
        </list>
 </property>    

运行时没有错误,但是当我访问locahost / Struts2Example / listCustomerAction时,它返回404(此链接适用于XML映射)

我试图在这个项目中使用注释映射进行Google搜索,但它失败了。

那么,我可以在这个项目中使用Annotation映射而不是XML映射吗?

请建议我一个简单的解决方案或使用注释映射的示例。

0 个答案:

没有答案