私はそれを回避するためにsaveメソッドをオーバーライドしていたようですが、もっと良い方法があると確信しています。
BaseRepository
@NoRepositoryBean
interface BaseRepository<T, ID extends Serializable> extends Repository<T, Long> {
@RestResource(path="byIdIn",rel="byIdIn")
@Query("select r from #{#entityName} r where r.id in :q")
Page<T> findByIdIn(@Param("q") List<Long> q, Pageable pageable)
Page<T> findAll(Pageable pageable)
T findOne(ID id)
T save(T entity)
T delete(ID id)
}
ContactRepository
@RepositoryRestResource(collectionResourceRel="contacts", path="contacts")
interface ContactRepository extends BaseRepository<Contact, Long>, ContactRepositoryCustom {
}
ContactRepositoryCustom
interface ContactRepositoryCustom {
public <S extends Contact> S save(S entity)
}
ContactRepositoryImpl
@NoRepositoryBean
class ContactRepositoryImpl implements ContactRepositoryCustom {
@PersistenceContext
private EntityManager em
@Transactional
@Override
public <S extends Contact> S save(S entity) {
Contact contact = entity as Contact
try {
em.persist(contact)
contact.getComment().each {
Comment comment = new Comment(contact, it)
em.persist(comment)
}
} catch (Exception e) {
println e
}
return contact
}
}
これは単なるサンプルですが、いくつかのクリーンアップが必要ですが、save()メソッドが期待通りに機能しています。 Spring Data/Spring Data Restでこのようなことを行うために、このようなソリューションをロールバックしなくても、アノテーションを使用して焼き付けが行われていれば、それ以上のことはしたくありません。私はドキュメントとオンラインで検索しましたが、解決策は見つかりませんでした。私は何かを見過ごしたかもしれないが、確かではない。
このシナリオでは、標準のSpring MVCコントローラが必要です:http://stackoverflow.com/questions/40362789/how-to-save-many-objects-in-the-same-request-using-spring- boot-data-rest –