简体   繁体   English

spring-boot-starter-data-jpa @OneToMany 在更新时不会插入新子项

[英]spring-boot-starter-data-jpa @OneToMany does not insert new child when it's an update

Parent:家长:

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class A {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;

@OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = true)
public List<Child> child = new LinkedList<>();

}

Child:孩子:

@Entity
public class Child {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long child_id;

}

Repository:存储库:

import org.springframework.data.repository.CrudRepository;
@Transactional
public interface ARepository extends CrudRepository<A,Long> {

@Query("SELECT x FROM A x WHERE x.syncTimestamp IS NULL")
List<A> findAs();
}

Persisting : Update坚持:更新

public class SomeClass {

  @Autowired
  private ARepository repo;

  public void someMethod() {

    List<A> as = repo.findAs();
    A firstA = as.get(0);
    firstA.child.add(new Child());
    repo.save(firstA);
  }    

}

When I insert a new A object, everything works fine, all the child insert well in the database.当我插入一个新的 A 对象时,一切正常,所有子项都很好地插入到数据库中。

But when I load an A object from the database, add some child to the list and then update the object, this doesn't insert the new child.但是当我从数据库加载一个 A 对象时,将一些子项添加到列表中然后更新该对象,这不会插入新的子项。

According to the documentation for CascadeType, ALL should insert them.根据 CascadeType 的文档,所有人都应该插入它们。

Any idea how to fix this?知道如何解决这个问题吗?

That's because you don't have a reference to the A class inside the Child class :那是因为您没有对Child类中的A类的引用:

@Entity
public class A {
...
     @OneToMany(mappedBy = "a", cascade = {CascadeType.ALL}, orphanRemoval = true)
     public List<Child> child = new LinkedList<>();
}

@Entity
public class Child {
...
    @ManyToOne
    @JoinColumn(name = "your_table_name")
    private A a;
...
}

This one is working for me.这个对我有用。 Please try again in following way your child entity will get updated:请按照以下方式重试您的子实体将得到更新:

public class SomeClass {

    @Autowired
    private ARepository repo;

    public void someMethod() {

        List<A> as = repo.findAs();
        A firstA = as.get(0);
        List<Child> childs=firstA.getChild();

        // add new child value
        childs.add(new Child("Test"));
        firstA.setChild(childs);

        for (Child child : childs) {
            child.setA(firstA);
        }
        repo.save(firstA);
    }    

}


import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;

@Entity
public class Child {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long child_id;

    public Child(String name) {
        // TODO Auto-generated constructor stub
        this.name=name;
    }

    public Child() {
        // TODO Auto-generated constructor stub
    }
    private String name;

    @ManyToOne
    @JoinColumn(name="a_id")
    private A a;

    //your getter setter

}

import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class A {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public Long id;

    private String syncTimestamp=null;

    @OneToMany(mappedBy="a", cascade=CascadeType.ALL)
    public List<Child> child = new LinkedList<>();

    // your getter setter

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导不添加 spring-boot-starter-data-jpa - Spring boot not adding spring-boot-starter-data-jpa Java:依赖项目中的spring-boot-starter-data-jpa依赖 - Java : spring-boot-starter-data-jpa dependency in a dependent project spring-boot-starter-data-jpa依赖错误 - spring-boot-starter-data-jpa dependency error 将“spring-boot-starter-data-jpa”依赖项添加到Spring项目时出错 - Error when adding “spring-boot-starter-data-jpa” dependency to Spring project 有关Spring-boot-starter-data-jpa的问题 - A question about the Spring-boot-starter-data-jpa spring-boot-starter-data-jpa:自动重新连接 - spring-boot-starter-data-jpa : Auto Reconnect 排除 JPA Spring boot - org.springframework.boot:spring-boot-starter-data-jpa&#39; 启动时导致 CRUDRepository 错误 - Exclude JPA Spring boot - org.springframework.boot:spring-boot-starter-data-jpa' causes error in CRUDRepository when starting 使用 spring-boot-starter-data-jpa 时 Hibernate-core 错误 - Hibernate-core error when using spring-boot-starter-data-jpa H2 数据库是用 spring-boot-starter-data-jpa 创建的,但不是用 spring-boot-starter-data-jdbc 创建的 - H2 Database created with spring-boot-starter-data-jpa but not with spring-boot-starter-data-jdbc 如果我将 spring-boot-starter-data-jpa 添加到一个简单的 spring 应用程序,我会得到 404s 但如果它不存在则没有 - If I add spring-boot-starter-data-jpa to a simple spring application I get 404s but none if it's not there
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM