开发者社区> 问答> 正文

CrudRepository.save()似乎级联

实体调查:

    @Entity
    @Table(name = "Survey")
    public class Survey implements Serializable {

        private Long id;
        private String name;
        private String address;
        private List<Question> questions;

        @Id
        @Column(name = "id")
        public Long getId() {
            return id;
        }

        @Column(name = "name")
        public String getName() {
            return name;
        }

        @Column(name = "address")
        public String getAddress() {
            return address;
        }

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "survey")
        public List<Question> getQuestions() {
            return questions;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public void setAddress(final String address) {
            this.address = address;
        }

        public void setQuestions(List<Question> _questions) {
            this.questions = _questions;
        }
    }

问题实体:

@Entity
@Table(name = "Question")
public class Question {

    private Long id;
    private String question;
    private Survey survey;

    @Id
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    @Column(name = "question")
    public String getQuestion() {
        return question;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "survey_id")
    public Survey getSurvey() {
        return survey;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public void setSurvey(Survey survey) {
        this.survey = survey;
    }
}

问题Crud回购:

 public interface QuestionRepo extends CrudRepository<Question, Long> {
    }

控制器:

@RestController
@RequestMapping("/default")
public class DefaultEndpoint {

    @Autowired
    private QuestionRepo questionRepo;


    @PostMapping(value = "/saveQuestion")
    public void saveQuestion(@Param("id") Long id, @Param("question") String question, @Param("survey_id") Long survey_id) {
        Question questionObject = new Question();
        questionObject.setId(id);
        questionObject.setQuestion(question);
        Survey survey = surveyRepo.findById(survey_id).get();
        survey.setName("example");
        questionObject.setSurvey(survey);
        questionRepo.save(questionObject);
    }
}

在控制器代码段中,当我做 Survey.setName(“ example”); 。此更改反映在数据库上。

这意味着,即使未指定级联类型,使用EntityManager方法MERGE和PERIST实现的save()操作也会级联到子实体。 这是应该如何工作的,还是我在犯一些愚蠢的错误? 非常感谢 !

问题来源:Stack Overflow

展开
收起
montos 2020-03-26 21:02:42 476 0
1 条回答
写回答
取消 提交回答
  • 请检查@ManyToOne注释以查看默认级联为“ none”。

    public @interface ManyToOne {
       /**
         * (Optional) The operations that must be cascaded to 
         * the target of the association.
         *
         * <p> By default no operations are cascaded.
         */
        CascadeType[] cascade() default {};
    }
    

    没有级联发生。

    您观察到的效果很可能是以下两个功能的组合:

    如果您正在运行Web应用程序,则Spring Boot默认情况下会注册OpenEntityManagerInViewInterceptor以应用“在视图中打开EntityManager”模式,以允许在Web视图中进行延迟加载。如果您不希望出现这种情况,则应spring.jpa.open-in-view在application.properties 中将其设置为false。

    脏检查是众所周知的,但是对于某些开发人员而言,View Open Session往往会感到惊讶(例如,在Spring中看到另一个相关效果-同一实体在@EventListener上分离但在@Service类中附加)

    回答来源:Stack Overflow

    2020-03-26 21:05:35
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载