0

I just recently moved from Hibernate4 to Hibernate5. I have implemented @PrePersist on my entity bean but @PrePersist doesn't seem to be firing up.

I have searched around https://github.com/hibernate/hibernate-orm/wiki/Migration-Guide---5.2

And on it it states

org.hibernate.SessionFactory now extends javax.persistence.EntityManagerFactory - temporarily it technically extends org.hibernate.jpa.HibernateEntityManagerFactory (which in turn extends javax.persistence.EntityManagerFactory) for backwards compatibility. HibernateEntityManagerFactory is deprecated.

So this means JPA annotation should work right?

My entity class looks like this

@Entity
@Table(name="books")
@Component
public class Book implements Serializable{

private static final long serialVersionUID = -2042607611480064259L;

@Id
@GeneratedValue
private int id;

@NotBlank
private String name;

@NotBlank
@Size(min=2, max=16)
private String ispn;

@DecimalMin(value = "0.1")
private double price;


private Timestamp dateCreated;

private Date datePublished;

@PrePersist
public void beforePersist() {
     System.out.println("@PrePersist is called");
    this.dateCreated = Timestamp.from(Instant.now());

}

applicationContext.xml (Hibernate configuration)

<!-- Connection Pool -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="poolName" value="springHikariCP" />
        <property name="connectionTestQuery" value="SELECT 1" />
        <property name="dataSourceClassName"
            value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
        <property name="dataSourceProperties">
            <props>
                <prop key="url">jdbc:mysql://localhost:3306/sample</prop>
                <prop key="user">sample</prop>
                <prop key="password">sample</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
        destroy-method="close">
        <constructor-arg ref="hikariConfig" />
    </bean>



    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>


    <!-- Hibernate Settings -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.id.new_generator_mappings">false</prop>
                <prop key="hibernate.jdbc.time_zone">UTC</prop>
                <prop key="hibernate.connection.useUnicode">true</prop>
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="org.hibernate.jdbc">TRACE</prop>
            </props>

        </property>

        <property name="packagesToScan">
            <list>
                <value>com.app.books</value>
            </list>
        </property>
    </bean>

Questions

  1. Something wrong with how I implement @Prepersist?

  2. Or perhaps my Hibernate configuration has some error?

Thank you

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Eric Huang
  • 1,114
  • 3
  • 22
  • 43
  • I dont know if it these annotations works now with hibernate 5.2 but if your need is to update the creation date, you could achieve the same with `@CreationTimestamp` annotation on top of your date field it will add the current jvm datetime. – Abass A Jun 22 '17 at 22:28
  • Are your records saved to DB? Why do you have `@Component` annotation on your entity? Are you using `Session` API? Check [this answer](https://stackoverflow.com/a/4133629/1126831). – ledniov Jun 23 '17 at 14:01
  • @ledniov yes records is saved to DB. And thanks for the link.... I just went through a marathon with JPA and Session Api... As I went through some tutorials people use persistence.xml to set up JPA. But whats confusing is since persistence.xml will have db info does that mean I will have to lose my connection pool setting? eeeek... sorry some how i feel this could be a dumb question... – Eric Huang Jun 23 '17 at 18:01
  • @EricHuang no, you can still use connection pooling, see example [here](https://stackoverflow.com/a/28880127/1126831). – ledniov Jun 24 '17 at 17:46

0 Answers0