gpt4 book ai didi

Java JPA Hibernate Spring @EntityListeners 抛出 org.springframework.dao.DataIntegrityViolationException

转载 作者:行者123 更新时间:2023-12-01 12:38:08 24 4
gpt4 key购买 nike

我正在使用 Spring 3 和 Hibernate 3。我想在更新实体时自动更新上次修改日期。下面是示例代码:

hibernate 配置:

@Configuration
public class HibernateConfig {

@Bean
public DataSource dataSource() throws Exception {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
Properties properties = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream(new String("hibernate.properties")));
dataSource.setUrl(properties.getProperty(new String("jdbc.url")));
dataSource.setUsername(properties.getProperty(new String("jdbc.username")));
dataSource.setPassword(properties.getProperty(new String("jdbc.password")));
dataSource.setDriverClassName(properties.getProperty(new String("jdbc.driverClassName")));
return dataSource;
}

@Bean
public AnnotationSessionFactoryBean sessionFactory() throws Exception {
AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
Properties hibernateProperties = new Properties();
Properties properties = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream(new String("hibernate.properties")));

// set the Hibernate Properties
hibernateProperties.setProperty(new String("hibernate.dialect"), properties.getProperty(new String("hibernate.dialect")));
hibernateProperties.setProperty(new String("hibernate.show_sql"), properties.getProperty(new String("hibernate.show_sql")));
hibernateProperties.setProperty(new String("hibernate.hbm2ddl.auto"), properties.getProperty(new String("hibernate.hbm2ddl.auto")));
sessionFactory.setDataSource(dataSource());
sessionFactory.setHibernateProperties(hibernateProperties);
sessionFactory.setAnnotatedClasses(new Class[]{Message.class})
return sessionFactory;
}

@Bean
public HibernateTemplate hibernateTemplate() throws Exception {
HibernateTemplate hibernateTemplate = new HibernateTemplate();
hibernateTemplate.setSessionFactory(sessionFactory().getObject());
return hibernateTemplate;
}
}

DAO配置:

@Configuration
public class DAOConfig {

@Autowired
private HibernateConfig hibernateConfig;

@Bean
public MessageDAO messageDAO() throws Exception {
MessageDAO messageDAO = new MessageHibernateDAO(hibernateConfig.hibernateTemplate());
return messageDAO;
}
}

消息:

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Table
@EntityListeners(value = MessageListener.class)
public class Message implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int id;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date lastMod;

public Message() {

}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Date getLastMod() {
return lastMod;
}

public void setLastMod(Date lastMod) {
this.lastMod = lastMod;
}
}

消息监听器:

import java.util.Date;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import org.springframework.stereotype.Component;

@Component
public class MessageListener {


@PrePersist
@PreUpdate
public void setLastMod(Message message) {
message.setLastMod(new Date());
}
}

运行此命令时,不会调用 MessageListener。我使用 DAO 设计模式,当调用 dao.update(Message) 时,它会抛出以下异常:

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.persistence.entities.MessageStatus.lastMod; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.persistence.entities.Message.lastMod
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:665)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
at com.persistence.dao.hibernate.GenericHibernateDAO.save(GenericHibernateDAO.java:38)

看了很多网站,似乎没有解决方案。

最佳答案

您似乎没有使用 EntityManager,而是使用简单的 Hibernate SessionFactory。另一个答案,Hibernate/JPA - Entity listener not being called properly ,似乎表明如果不手动设置监听器,这将无法工作。

关于Java JPA Hibernate Spring @EntityListeners 抛出 org.springframework.dao.DataIntegrityViolationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25380101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com