2013-05-03 102 views
1

我有一個簡單的實體和JPA存儲庫的Spring項目。該實體有三個變量:Name (String),active (boolean)date (java.util.Date)。一旦部署,Hibernate使用varchar,tinyintdatetime在我的MySQL數據庫中創建表。所有似乎都是正確的,但是當我創建/修改實體的實例並調用存儲庫的save()方法時,除日期之外,所有字段都被保存。是否有任何知道的問題與JPA存儲庫或meaby存儲日期我做錯了什麼?春季數據不保存日期

我沒有在這裏放任何代碼,因爲只是一個簡單的類和存儲庫的接口。此外,實體正在保存。我的問題只與日期字段(以及我可以定義的任何其他日期字段)有關。話雖如此,如果有需要的話,就問。

編輯:

根的context.xml

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

    <!-- Root Context: defines shared resources visible to all other web components --> 

    <import resource="infraestructure.xml" /> 

    <jpa:repositories base-package="com.smarttabletv.repository" /> 

</beans> 

servlet的context.xml中

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="com.smarttabletv" /> 

    <!-- Activates @Scheduled and @Async annotations for scheduling --> 
    <task:annotation-driven /> 

</beans:beans> 

infraestructure.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" 
    default-autowire="byName"> 

    <!-- Scans within the base package of the application for @Components to 
     configure as beans --> 
    <bean id="placeholderConfig" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="location" value="classpath:db.properties" /> 
    </bean> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="showSql" value="true" /> 
       <property name="generateDdl" value="true" /> 
       <property name="databasePlatform" value="${db.dialect}" /> 
      </bean> 
     </property> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="org.hibernate.envers.auditTablePrefix"></prop> 
       <prop key="org.hibernate.envers.auditTableSuffix">_history</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="${db.driver}" /> 
     <property name="url" value="${db.url}" /> 
     <property name="username" value="${db.username}" /> 
     <property name="password" value="${db.password}" /> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <bean 
     class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

</beans> 

個的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml</param-value> 
    </context-param> 

    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>appServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

db.properties

db.password= 
db.username= 
db.url=jdbc:mysql://ip:3306/smarttabletv 
db.dialect=org.hibernate.dialect.MySQLInnoDBDialect 
db.driver=com.mysql.jdbc.Driver 

代碼不工作

Task task = taskService.findDispatchableTask(); 
task.setDispatched(true); // This is saved 
task.setEndDate(new Date()); // But this not 
taskService.save(task); 

Task類第一線

@Entity 
@Table(name = "sttv_tasks") 
public class Task { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "tsk_id") 
    private Long id; 

    @Column(name = "tsk_end_date") 
    private Date endDate; 
+0

請發佈具有實體映射,存儲庫定義以及客戶端代碼的域類。 – 2013-05-04 12:42:24

+0

感謝您的回答@OliverGierke。我剛更新了這篇文章。 – 2013-05-04 13:10:19

回答

3

你忘了上添加註解時空結束日期,指定是否應該保留日期,時間或兩者。

@Column(name = "tsk_end_date") 
@Temporal(TemporalType.TIMESTAMP) 
private Date endDate; 
+0

我不知道我必須這樣做。非常感謝你! – 2013-05-04 14:06:31