gpt4 book ai didi

java - 使用onetomany连接更新实体,而子实体中没有id

转载 作者:行者123 更新时间:2023-12-01 12:40:46 25 4
gpt4 key购买 nike

我有以下结构:表“parents”和“children”,由parents.id = Children.parent_id绑定(bind)

应用程序将这些表中的数据读取到以下类的对象中:

public class Parent {
private int id;
private Map<String, String> children;
....
}

然后应用程序更改其中的数据。我需要做的是将更改应用于现有记录。我不允许更改父类或数据库结构。我尝试通过以下方式使用 jpa 存储库来做到这一点:

我的实体

@Repository
public interface ParentsDao extends JpaRepository<ParentEntity, Integer> {
}

@Entity
@Table(name = "parents")
public class ParentEntity {
@Id
@GeneratedValue
@Column(name = "id", updatable = false, insertable = false)
private Integer id;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name="parent_id")
private Set<ChildEntity> children;
...
}

@Entity
@Table(name = "children")
public class ChildEntity {
@Id
@GeneratedValue
@Column(name = "id", updatable = false, insertable = false)
private Integer id;

@ManyToOne
@JoinColumn(name="parent_id", updatable = false)
private ScheduledReportsEntity scheduledReport;

@Column(name = "child_name")
private String name;

@Column(name = "child_value")
private String value;
...
}

以下是我的服务,其中 action() 启动了整个过程:将 Parent 转换为 ParentEntity 并保存它

public class ParentServiceImpl {
@Autowired
ParentsDao parentsDao;

public

void action(Parent parent) {
ParentEntity entity = convert(parent); // moving data from Parent to ParentEntity object
parentsDao.save(entity); //here I receive exception
}
private ParentEntity convert(Parent parent) {

Set<ChildEntity> children = new HashSet<ChildEntity>();
for(Map.Entry<String, String> entry : simpleObj.getChildren().entrySet()){
ChildEntity childEntity = new ChildEntity();
childEntity.setName(entry.getKey());
childEntity.setValue(entry.getValue());
children.add(entity);
}

ParentEntity entity = new ParentEntity();
entity.setId(parent.getId());
entity.setChildren(children);

return entity;
}
}

但我收到以下错误:

com.app.impl.ParentServiceImpl: org.springframework.dao.DataIntegrityViolationException: 
could not insert: [com.app.entities.ChildEntity];
SQL [insert into scheduled_report_params (child_name, child_value, parent_id) values (?, ?, ?)];
constraint [null];
nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [com.app.entities.ChildEntity]

我有两个问题:1. 我认为这与 child 身份缺失有关,对吗?2.如果原因是缺少id,那么有什么办法可以以某种方式绕过它并通过其他字段提供保存?将不胜感激任何建议。谢谢

最佳答案

您遇到设计问题。

你是对的,如果id是子实体的主键,它必须存在于子集合中所有预先存在的(不是新的)ChildEntity元素中ParentEntity 元素(如果您对其进行修改)。如果没有,Hibernate 将尝试插入它们。

因此您必须在两种解决方案之间进行选择:

  • ChildEntity 中使用生成的主键,并找到一种方法将其保留在您的应用程序中。例如,您可以使用 ParentEntity 作为真实模型类,而 Parent 是其上的 View :

    class Parent {
    private ParentEntity inner;
    public Parent(ParentEntity entity) {
    inner = entity;
    // initialize children or have getChildren to dynamically create it
    // from inner.childre
    ...
    }
    ...
    }
  • 使用子名称作为主键。 ChildEntity 看起来像:

    @Entity
    @Table(name = "children")
    public class ChildEntity {
    @Id
    @Column(name = "child_name")
    private String name;

    @Column(name = "child_value")
    private String value;

    @ManyToOne
    @JoinColumn(name="parent_id", updatable = false)
    private ScheduledReportsEntity scheduledReport;
    ...
    }

但是您不能简单地忘记应用程序中的主键。

关于java - 使用onetomany连接更新实体,而子实体中没有id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25118892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com