Java CRUDEPository#在mySQL数据库中保存非持久数据

Java CRUDEPository#在mySQL数据库中保存非持久数据,java,mysql,spring,hibernate,Java,Mysql,Spring,Hibernate,我正在尝试将实体(事件)保存到本地运行的mySql数据库中。当我尝试使用crudepository执行此操作时,数据不会持久化到数据库中。为什么会发生这种情况?我如何解决此问题 我看不出我做错了什么,因为crudepository#findAll按预期工作 以下是一些上下文: 属性 # Spring spring.jpa.show-sql=true spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrat

我正在尝试将实体(
事件
)保存到本地运行的mySql数据库中。当我尝试使用
crudepository
执行此操作时,数据不会持久化到数据库中。为什么会发生这种情况?我如何解决此问题

我看不出我做错了什么,因为
crudepository#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>
事件

@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;
    }
}
EventRepository

public interface EventRepository extends CrudRepository<Event, Long> {

}

你有什么错误吗?尝试在您的服务类上使用@Transactional annotation。DatabaseProperties来自何处?您确定调用了您的代码吗?为什么不尝试使用JpaRepository而不是Crudepository?@PoojaAggarwal没有任何错误。我试着使用
@Transactional
,但没有用。
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();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<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>
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);