Note that there are some explanatory texts on larger screens.

plurals
  1. PO@PrePersist is being called and modifies the entity, but is not written to the database
    text
    copied!<p>I am developing an application in Spring MVC using Hibernate and MySQL and I have an issue. I am trying to populate my last modified field in my Java entity using the <code>@PrePersist</code> annotation. I have debugged the code and the method is getting called and the value is getting set. However, the database is throwing a null violation, since it is not writing out the values the <code>@PrePersist</code> method added. Does anyone know how to fix this t write out the data to the database? </p> <p>FYI, besides changing dates, I want to use these JPA annotations to do certain business logic or use something similar to the annotations.</p> <p>The code:</p> <pre><code>@Entity @Table(name = "account") public class Account { @Column(name = "modified_on") @Temporal(TemporalType.TIMESTAMP) @DateTimeFormat(style = "MM") @NotNull() private Calendar modifiedOn; ... getters, setter and other stuff @PrePersist public void prePersist() { Calendar now = Calendar.getInstance(); this.createdOn = now; this.modifiedOn = now; } @PreUpdate public void preUpdate() { Calendar now = Calendar.getInstance(); this.modifiedOn = now; } </code></pre> <p>applicationContext-Persistence.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans:beans xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"&gt; &lt;repositories base-package="${repositoryPackageName}" /&gt; &lt;beans:bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt; &lt;beans:bean id="exceptionTranslator" class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" /&gt; &lt;beans:bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"&gt; &lt;beans:property name="entityManagerFactory" ref="entityManagerFactory" /&gt; &lt;/beans:bean&gt; &lt;beans:bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt; &lt;beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;beans:property name="driverClassName" value="${database.driverClassName}" /&gt; &lt;beans:property name="url" value="${database.url}" /&gt; &lt;beans:property name="username" value="${database.username}" /&gt; &lt;beans:property name="password" value="${database.password}" /&gt; &lt;/beans:bean&gt; &lt;beans:bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"&gt; &lt;beans:property name="dataSource" ref="dataSource" /&gt; &lt;beans:property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" /&gt; &lt;beans:property name="packagesToScan" value="${scanPackageName}" /&gt; &lt;beans:property name="jpaProperties"&gt; &lt;beans:props&gt; &lt;beans:prop key="hbm2ddl.auto"&gt;validate&lt;/beans:prop&gt; &lt;beans:prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/beans:prop&gt; &lt;beans:prop key="hibernate.query.substitutions"&gt;true '1', false '0'&lt;/beans:prop&gt; &lt;beans:prop key="hibernate.generate_statistics"&gt;true&lt;/beans:prop&gt; &lt;beans:prop key="hibernate.show_sql"&gt;false&lt;/beans:prop&gt; &lt;beans:prop key="hibernate.format_sql"&gt;true&lt;/beans:prop&gt; &lt;beans:prop key="hibernate.hbm2ddl.auto"&gt;validate&lt;/beans:prop&gt; &lt;/beans:props&gt; &lt;/beans:property&gt; &lt;/beans:bean&gt; &lt;/beans:beans&gt; </code></pre> <p>applicationContext.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation= "http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd" &gt; &lt;context:property-placeholder location="classpath:META-INF/spring/*.properties" /&gt; &lt;context:property-placeholder location="classpath:META-INF/properties/*.properties"/&gt; &lt;context:component-scan base-package="${doaminPackageName}"/&gt; &lt;context:component-scan base-package="${repositoryPackagename}"/&gt; &lt;context:component-scan base-package="${repositoryPackageName}" &gt; &lt;context:exclude-filter type="custom" expression="ourapp.util.TestClassFilter"/&gt; &lt;/context:component-scan&gt; &lt;context:component-scan base-package="${utilBeanPackageName}"/&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"&gt; &lt;property name="host" value="${mail.server.host}" /&gt; &lt;property name="port" value="${mail.server.port}" /&gt; &lt;property name="protocol" value="${mail.server.protocol}" /&gt; &lt;property name="username" value="${mail.server.username}" /&gt; &lt;property name="password" value="${mail.server.password}" /&gt; &lt;property name="javaMailProperties"&gt; &lt;util:properties location="classpath:META-INF/spring/javamail.properties" /&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>applicationContext-web.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation= "http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd" &gt; &lt;context:component-scan base-package="${controllerPackageName}"/&gt; &lt;mvc:annotation-driven/&gt; &lt;mvc:default-servlet-handler/&gt; &lt;mvc:resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**"/&gt; &lt;mvc:interceptors&gt; &lt;bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/&gt; &lt;bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang"/&gt; &lt;/mvc:interceptors&gt; &lt;mvc:view-controller path="/uncaughtException"/&gt; &lt;mvc:view-controller path="/resourceNotFound"/&gt; &lt;mvc:view-controller path="/dataAccessFailure"/&gt; &lt;bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:fallbackToSystemLocale="false"&gt; &lt;property name="basenames"&gt; &lt;list&gt; &lt;value&gt;META-INF/web-resources/i18n/accountCreation/messages&lt;/value&gt; &lt;value&gt;META-INF/web-resources/i18n/accountCreation/application&lt;/value&gt; &lt;value&gt;META-INF/web-resources/i18n/accountCreation/errors&lt;/value&gt; &lt;!-- Keep Global resource bundles at the bottom, they are checked last --&gt; &lt;value&gt;META-INF/web-resources/i18n/global_messages&lt;/value&gt; &lt;value&gt;META-INF/web-resources/i18n/global_application&lt;/value&gt; &lt;value&gt;META-INF/web-resources/i18n/global_errors&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver" p:cookieName="locale"&gt; &lt;property name="defaultLocale" value="en"/&gt; &lt;/bean&gt; &lt;bean class="org.springframework.ui.context.support.ResourceBundleThemeSource" id="themeSource"/&gt; &lt;bean class="org.springframework.web.servlet.theme.CookieThemeResolver" id="themeResolver" p:cookieName="theme" p:defaultThemeName="standard"/&gt; &lt;bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:defaultErrorView="exceptions/uncaughtException"&gt; &lt;property name="exceptionMappings"&gt; &lt;props&gt; &lt;prop key=".DataAccessException"&gt;exceptions/dataAccessFailure&lt;/prop&gt; &lt;prop key=".NoSuchRequestHandlingMethodException"&gt;exceptions/resourceNotFound&lt;/prop&gt; &lt;prop key=".TypeMismatchException"&gt;exceptions/resourceNotFound&lt;/prop&gt; &lt;prop key=".MissingServletRequestParameterException"&gt;exceptions/resourceNotFound&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- Enable this for integration of file upload functionality --&gt; &lt;bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver"/&gt; &lt;!-- TymeLeaf Settings --&gt; &lt;!-- THYMELEAF: Template Resolver for webapp pages --&gt; &lt;bean class="org.thymeleaf.templateresolver.ServletContextTemplateResolver" id="templateResolver"&gt; &lt;property name="prefix" value="/WEB-INF/templates/"/&gt; &lt;property name="suffix" value=".html"/&gt; &lt;property name="templateMode" value="HTML5"/&gt; &lt;/bean&gt; &lt;!-- THYMELEAF: Template Engine (Spring3-specific version) --&gt; &lt;bean class="org.thymeleaf.spring3.SpringTemplateEngine" id="templateEngine"&gt; &lt;qualifier value="templateEngine"/&gt; &lt;property name="templateResolver" ref="templateResolver"/&gt; &lt;/bean&gt; &lt;!-- THYMELEAF: View Resolver - implementation of Spring's ViewResolver interface --&gt; &lt;bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver"&gt; &lt;property name="templateEngine" ref="templateEngine"/&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload