Spring Data不会使用save()方法更新

时间:2017-12-25 12:43:26

标签: java spring maven spring-data spring-data-jpa

我目前正在申请大学项目申请。对于这个项目,我的团队和我正在使用Vaadin / Spring / Maven配置。有了这个,我们还包括一个与存储库相连的数据库。 reposetories以下列方式构建。首先是对象类:

import javax.persistence.*;
import java.util.Set;

@Entity
@Table (name = "component")
public class Component {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    long id;
    String name;
    float price;
    boolean isVegetarian;
    @OneToMany(mappedBy = "component", cascade = {CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    Set<InvoicePosition> Positions;
    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    ComponentCategory category_id;
    String path;

public Component() {
}

public Component(String name, float price, boolean isVegetarian, ComponentCategory category, String path) {
    this.name = name;
    this.price = price;
    this.isVegetarian = isVegetarian;
    this.category_id = category;
    this.path = path;
}

然后是额外的存储库:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ComponentRepository extends JpaRepository<Component, Long>{}

然后有一个组件类别的存储库,我们应该能够存储组件列表:

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "componentCategory")
public class ComponentCategory {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    String name;
    @OneToMany(mappedBy = "category_id", cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    List<Component> components;

public ComponentCategory() {    }

public ComponentCategory(String name) {
    this.name = name;
    components = new ArrayList<>();
}

public void addComponentToCategory(Component component) {
    if(!components.contains(component))
    {
        components.add(component);
        component.category_id = this;
    }
}

然后是同一个存储库:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ComponentCategoryRepository extends JpaRepository<ComponentCategory, Long> {}

现在我可以创建新条目,它们也可以正确存储在数据库中。这都存储在Application.Java文件中以启动vaadin应用程序

ComponentCategory cat1 = new ComponentCategory("examp1");
Component test1 = new Component("example1", 1.25f, true, cat1, "");
Component test2 = new Component("example2", 0.89f, true, cat1, "");
cat1.addComponentToCategory(test1);
cat1.addComponentToCategory(test2);
compcatrep.saveAndFlush(comp); // compcatrep is the ComponentCategoryRepository, which is passed on to the init() function

现在我尝试使用以下逻辑更新数据库中的条目:

 Component test11 = new Component("Test", 2.15f, true, compcatrep.getOne((long)4), "");
 ComponentCategory comp = compcatrep.getOne((long)4);
 comp.addComponentToCategory(test11);
 compcatrep.saveAndFlush(comp);

我确保有一个带有ID(长)4的条目。当我执行应用程序时,它也可以工作并且不会显示任何错误。另外,如果我在saveAndFlush()之后检查ComponentCategory中存储列表的长度:

System.out.println(comp.getComponents().size());       
System.out.println(compcatrep.getOne((long)4).getComponents().size());

我确实收到了不同长度的列表。第二个Syso确实比第一个少了一个。 现在我尝试了很多,但没有找到错误。关于我的问题的一些更多信息: 目标文件夹中的ApplicationProperties:

spring.datasource.url=jdbc:h2:file:./mensarioDB;DB_CLOSE_ON_EXIT=TRUE
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.hs-augsburg.bpap</groupId>
<artifactId>mensario</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>mensario</name>
<description>mensario management</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <vaadin.version>8.1.0</vaadin.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <version>${vaadin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

非常感谢你的帮助。真的很感激。 最好的祝福 汤姆

0 个答案:

没有答案