[英]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.