我在更新具有多对多关联的实体时遇到问题,是否对我做错了事感到好奇,或者更具体地说,什么是正确的方法
请考虑以下实体...
@Entity
class Subject
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
name: string;
@ManyToMany(() => Note, note => note.subjects)
@JoinTable()
notes: Note[];
...
@Entity()
export class Note {
@PrimaryGeneratedColumn('uuid')
id: string;
@ManyToMany(() => Subject, (subject: Subject) => subject.notes)
subjects: Subject[];
在我的代码中,我找到了该节点,然后尝试对其进行更新并保存,如下所示...
const note = await noteRepo.findOneOrFail(noteId);
const foundSubjects = await subjectRepo.findByIds(Array.from(subjectIds));
note.subjects = foundSubjects;
noteRepo.save(note);
但可惜,主题并没有保存在笔记上。
正确的方法是什么?
谢谢!
答案 0 :(得分:1)
因此以下内容对我有用:
model <- keras_model_sequential() %>%
layer_dense(units = 16, activation = "relu", input_shape = c(1218)) %>%
layer_dense(units = 16, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("accuracy")
)
model %>% compile(
optimizer = optimizer_rmsprop(lr = 0.001),
loss = "binary_crossentropy",
metrics = c("accuracy")
)
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("accuracy")
)
history <- model %>% fit(
x_train,
y_train,
epochs = 7,
batch_size = 500,
validation_data = list(x_test, y_test)
)
尽管如此,我仍然觉得repo.save()方法应该有效
答案 1 :(得分:1)
默认情况下,级联设置为false,您可以启用以下操作:
@Entity()
export class Note {
@PrimaryGeneratedColumn('uuid')
id: string;
@ManyToMany(() => Subject, (subject: Subject) => subject.notes, { cascade: true })
subjects: Subject[];
答案 2 :(得分:0)
在我的情况下,我试图更新现有关系,但是由于该关系已经存在,这给我带来了独特的密钥冲突,因此我首先需要删除所有现有关系,然后添加更新的用户的关系:
export const updateUser = async (user: User): Promise<User | undefined> => {
/**
* Get the actual relationships of that user.
*/
const actualRelationships = await getRepository(User)
.createQueryBuilder()
.relation(User, 'roles')
.of(user).loadMany();
/**
* Add new relationships of the user, and delete the old relationships.
*/
await getRepository(User)
.createQueryBuilder()
.relation(User, 'roles')
.of(user)
.addAndRemove(user.roles, actualRelationships);
/**
* Update only the table USER.
*/
await getRepository(User)
.createQueryBuilder()
.update()
.set({
name: user.name,
username: user.username,
active: user.active
})
.where('id = :id', {id: user.id})
.execute();
/**
* Return the updated user
*/
return await getUser(user.id, true, true)
};