英文:
sql update one to many using spring jpa (foreign key null)
问题
使用 Spring Boot、Java 11 和 JPA 进行开发。
我有两个表,分别是 entry
和 entry_names
。我想要更新 entry
表中的一条记录以及其在 entry_names
表中的相关的多对一关系。
问题:
entry_id
字段始终为 null,因此它在 entry_names
表中创建新的条目,而不是更新它们。我做错了什么?以下是我最新的代码:
表结构:
+-------+
| Entry |
+-------+
| id | pk:id
| title |
| text |
+-------+
+-----------+
|entry_names|
+-----------+
| id | pk: id
| name | fk: entry_id -> entry(id)
| entry_id |
+-----------+
这是实体类的定义:
Entry:
@Entity
@Table(name="entry")
@Getter
@Setter
public class Entry {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String text;
@OneToMany(
fetch = FetchType.EAGER,
mappedBy = "entry",
cascade = CascadeType.ALL,
orphanRemoval = true)
private Set<EntryNames> names;
}
Entry_names:
@Entity
@Table(name="entry_names")
@Getter
@Setter
public class EntryNames {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "entry_id")
private Entry entry;
@Column(name = "name")
private String name;
}
代码:
@Override
@Transactional
public boolean update(EntryDto dto) {
try {
Optional<Entry> findEntry = dao.findById(entry.getId());
if (findEntry.isPresent()) {
Entry entry = findEntry.get();
Set<EntryNames> entryNames = new HashSet<>();
entry.getName().forEach(entryNames -> entryNames.add(new EntryName(entryName)));
entryNames.forEach(entryName -> entryName.setId(entry.getId()));
entry.setName(entryNames);
dao.save(entry);
return true;
}
return false;
} catch (Exception exception) {
logger.error("ERROR: {}", exception.getMessage());
return false;
}
}
(已编辑以删除分配,问题仍然存在)
英文:
Im working with spring boot, java 11 and jpa.
I have 2 tables, entry and entry_names. I want to update both an entry, and its subsequent many to one relationship in the second table(entry_names).
The problem:
Entry_id field is always null, so it creates new entries in Entry_names instead of updating them. What am i doing wrong? This is my most up-to-date code:
Tables:
+-------+
| Entry |
+-------+
| id | pk:id
| title |
| text |
+-------+
+-----------+
|entry_names|
+-----------+
| id | pk: id
| name | fk: entry_id -> entry(id)
| entry_id |
+-----------+
These are the entities:
Entry:
@Entity
@Table(name="entry")
@Getter
@Setter
public class Entry {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String text;
@OneToMany(
fetch = FetchType.EAGER,
mappedBy = "entry",
cascade = CascadeType.ALL,
orphanRemoval = true)
private Set<EntryNames> names;
}
Entry_names:
@Entity
@Table(name="entry_names")
@Getter
@Setter
public class EntryNames {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "entry_id")
private Entry entry;
@Column(name = "name")
private String name;
}
Code:
@Override
@Transactional
public boolean update(EntryDto dto) {
try {
Optional<Entry> findEntry = dao.findById(entry.getId());
if (findEntry.isPresent()) {
Entry entry = findEntry.get();
Set<EntryNames> entryNames = new HashSet<>();
entry.getName().forEach(entryNames -> entryNames.add(new EntryName(entryName)));
entryNames.forEach(entryName -> entryName.setId(entry.getId()));
entry.setName(entryNames);
dao.save(entry);
return true;
}
return false;
} catch (Exception exception) {
logger.error("ERROR: {}", exception.getMessage());
return false;
}
}
(edited to remove assignment, still same problem)
答案1
得分: 1
你正在使用 new HashSet<>(); 来设置名称,这将覆盖这些值。
更改为:
private Set<EntryNames> names;
同时在更新方法中添加以下内容:
entrynames.setEntry(entry);
英文:
You are setting names with new HashSet<>(); which will overwrite the values.
Change to:
private Set<EntryNames> names ;
Aslo add this in update method
entrynames.setEntry(entry)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论