我刚刚从Hibernate4迁移到Hibernate5。我已经在我的实体bean上实现了@PrePersist,但是@PrePersist似乎并未启动。
我已经四处寻找了 https://github.com/hibernate/hibernate-orm/wiki/Migration-Guide---5.2并在其上声明
org.hibernate.SessionFactory现在扩展了javax.persistence.EntityManagerFactory - 暂时它在技术上扩展了org.hibernate.jpa.HibernateEntityManagerFactory(后者又扩展了javax.persistence.EntityManagerFactory)以实现向后兼容。不推荐使用HibernateEntityManagerFactory。
那么这意味着JPA注释应该正常吗?
我的实体类看起来像这样
@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配置)
<!-- 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>
问题
我实施@Prepersist的方式出了什么问题?
或许我的Hibernate配置有一些错误?
谢谢