现在有question表,paper表paper_question表,hibernate查询出question表和paper表
的ID,循环插入到paper_question表中,请问各位大神该怎么做,希望可以写出代码
关于hibernate循环添加的问题多对多关系。。。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答
- 假人大哥 2018-02-12 12:42关注
@Entity data class Question( @Id @GeneratedValue var id: Long? = null ) interface QuestionRepository: CrudRepository<Question, Long> @Entity data class Paper( @Id @GeneratedValue var id: Long? = null ) interface PaperRepository: CrudRepository<Paper, Long> @Entity data class PaperQuestion( @Id @GeneratedValue var id: Long? = null, @ManyToOne @JoinColumn(name="paper_id") val paper: Paper, @ManyToOne @JoinColumn(name = "question_id") val question: Question ) interface PaperQuestionRepository: CrudRepository<PaperQuestion, Long> @Service class TestService( val paperQuestionRepository:PaperQuestionRepository, val paperRepository:PaperRepository, val questionRepository: QuestionRepository ){ fun test(){ val papers = paperQuestionRepository.findAll() val questions = paperQuestionRepository.findAll() val paperQuestions = mutableListOf<PaperQuestion>() papers.forEach{ paper -> questions.forEach{ question -> paperQuestions.add(PaperQuestion(paper=paper,question=question)) } } paperQuestionRepository.save(paperQuestions) } } @SpringBootApplication class Application fun main(args: Array<String>){ SpringApplication.run(Application::class.java, *args) }
用的springboot 和jpa规范,spring-data-jpa作为dao层实现,然后语言是和java差不多的kotlin,只是为了写起来简单点,少写些public getter/setter什么的,
用java写逻辑也是那样,hibernate作为jpa的实现,也是可以直接使用jpa的规范的本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?