我正在试图保存一个实体( Event
)到本地运行的mysql数据库。当我尝试使用 CrudRepository
,数据不会持久化到数据库。为什么会发生这种情况?我如何解决这个问题?
我不知道我做错了什么 CrudRepository#findAll
按预期工作。
以下是一些背景:
属性
# Spring
spring.jpa.show-sql=true
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
# Application
monitor.db.driverClassName=com.mysql.cj.jdbc.Driver
monitor.db.url=jdbc:mysql://localhost:3306/system_monitor_dev?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
monitor.db.username=SYSTEM_MONITOR
monitor.db.password=<omitted>
数据源配置
@Configuration
@EnableJpaRepositories(DataSourceConfig.REPOSITORIES_PACKAGE)
@EnableTransactionManagement
public class DataSourceConfig {
private static final String MODEL_PACKAGE = "be.thibaulthelsmoortel.monitor.model";
static final String REPOSITORIES_PACKAGE = "be.thibaulthelsmoortel.monitor.repositories";
private final DatabaseProperties databaseProperties;
@Autowired
public DataSourceConfig(DatabaseProperties databaseProperties) {
this.databaseProperties = databaseProperties;
}
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
sessionBuilder.scanPackages(MODEL_PACKAGE);
return sessionBuilder.buildSessionFactory();
}
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
return new HibernateTransactionManager(sessionFactory);
}
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
return initializer;
}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean getEntityManagerFactoryBean() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setJpaVendorAdapter(getJpaVendorAdapter());
factoryBean.setDataSource(getDataSource());
factoryBean.setPackagesToScan(MODEL_PACKAGE);
return factoryBean;
}
@Bean(name = "dataSource")
public DataSource getDataSource() {
return DataSourceBuilder
.create()
.username(databaseProperties.getUsername())
.password(databaseProperties.getPassword())
.url(databaseProperties.getUrl())
.driverClassName(databaseProperties.getDriverClassName())
.build();
}
@Bean
public JpaVendorAdapter getJpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
}
事件
@Entity
@Table(name = "events")
public class Event implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private long id;
@Column(name = "date_time")
private LocalDateTime dateTime;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public LocalDateTime getDateTime() {
return dateTime;
}
public void setDateTime(LocalDateTime dateTime) {
this.dateTime = dateTime;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return name;
}
}
事件存储库
public interface EventRepository extends CrudRepository<Event, Long> {
}
事件服务
public interface EventService {
Event findById(long id) throws ObjectNotFoundException;
Event save(Event event);
Iterable<Event> findAll();
}
事件服务实现
@Service
public class EventServiceImpl implements EventService {
private EventRepository eventRepository;
@Autowired
public EventServiceImpl(EventRepository eventRepository) {
this.eventRepository = eventRepository;
}
@Override
public Event findById(long id) throws ObjectNotFoundException {
Optional<Event> optionalEvent = eventRepository.findById(id);
if (optionalEvent.isPresent()) {
return optionalEvent.get();
}
throw new ObjectNotFoundException(Event.class, id);
}
@Override
public Event save(Event event) {
return eventRepository.save(event);
}
@Override
public Iterable<Event> findAll() {
return eventRepository.findAll();
}
}
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>be.thibaulthelsmoortel</groupId>
<artifactId>system-monitor</artifactId>
<version>1.0-SNAPSHOT</version>
<name>System Monitor</name>
<description>Application for monitoring any kind of system events.</description>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<log4j.version>2.11.1</log4j.version>
<mysql-connector-java.version>8.0.12</mysql-connector-java.version>
<spring.version>5.1.1.RELEASE</spring.version>
<spring-boot.version>2.0.6.RELEASE</spring-boot.version>
<junit.version>4.12</junit.version>
<mockito.version>2.23.0</mockito.version>
</properties>
<dependencies>
<!-- Logging -->
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- Database -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Spring -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Other -->
<!-- javassist required for datasource config -->
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.23.1-GA</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>
<!-- Testing -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
尝试下面附加的实际保存尝试不会将任何数据持久化到数据库,也不会将任何sql语句打印到控制台(使用时会打印和执行sql语句) CrudRepository#findAll
.
Event newEvent = new Event();
//newEvent.setId(2);
newEvent.setName("New event");
newEvent.setDescription("A new event arose from the ashes.");
newEvent.setDateTime(LocalDateTime.now());
Event savedEvent = eventService.save(newEvent);
暂无答案!
目前还没有任何答案,快来回答吧!