使用CrudRepository save()更新可更改多个文件的顺序并更改其ID

时间:2019-03-01 15:18:22

标签: java spring-boot spring-data-jpa foreign-keys one-to-many

嗨,我尝试从json更新问题表时遇到问题。 我需要从json将一些数据存储在db中。我使用多个实体来设置问题,并使用一些@onetomany来创建外键。

我第一次存储所有数据都很好,hibernate正确存储了自动生成的id的所有数据,当我尝试再次加载json子表上的某些更改时,Questionary的id正确存在(因为虽然我设置了值,但从json直接获取了一个字段)问题和答案更改ID。

我希望你能理解我的意思。

这是我的实体:

public class Questionary implements Serializable {

    private static final long serialVersionUID = -6101283729971360969L;
    /**
     * Primary key from JSON
     */
    @Id 
    @JsonIgnore
    private int id;
    @Version
    private Long version;
    private String desc;


    @OneToMany(cascade=CascadeType.ALL)
    @JoinColumn(name = "idQuest")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @OrderBy("identityQuestion ASC")
    private Set<Question> question;

public class Question implements Serializable {

private static final long serialVersionUID = -6101283729971360969L;
@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@Version
private Long version;
private int identityQuestion;
private String text;
private long idTipoTag;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name = "idQuestion")
@OnDelete(action = OnDeleteAction.CASCADE)
@OrderBy("identityAnswer ASC")
private Set<Answer> answer;

这是控制器上的方法

@RestController
@Slf4j
public class QuestionaryController {

@Autowired
private QuestionaryRepository questionaryRepository;
@Autowired
private AnswerRepository answerRepository;

@RequestMapping(value = "/save", method = RequestMethod.POST)
public void saveQuest(@RequestBody List<QuestionaryDTO> questDTO) {

    for (QuestionaryDTO questionaryDTO : questDTO) {

        /**
         * find value in db
         */
            Questionary quest = questionarioRepository.findById(questionarioDTO.getId());
            if (quest != null) {

                quest.setDescrizione(questionarioDTO.getDescrizione());

                Set<QuestionDTO> listQuestion = QuestionaryDTO.getQuestion();
                Set<Question> questionDB = quest.getQuestion();

                ArrayList<QuestionDTO> l = new ArrayList<QuestionDTO>(listQuestion);
                ArrayList<Question> c = new ArrayList<Question>(questionDB);

                int i = -1;
                for(int j=0; j<l.size();j++) {

                    i++;
                    Question dom = c.get(i);                
                    dom.setIdentityQuestion(l.get(j).getId());
                    dom.setIdTipoTag(l.get(j).getIdTipoTag());
                    dom.setText(l.get(j).getText());
                    questionDB.add(dom);

                        ecc...

                }
                quest.setquestion(questionDB);
                questionariyRepository.save(quest);

            } else {


            /**
             * Questionary
             */
            Questionary questn = new Questionary();
            questn.setTitle(questionaryDTO.getTitle()); 
            questn.setId(questionaryDTO.getId());
            /**
             * Questions
             */
            Set<QuestionDTO> listQuestion = questionaryDTO.getQuestion();
            Set<Question> QuestionDB = new HashSet<>();
            for (QuestionDTO questionDTO : listQuest) {
                Question que = new Question();
                que.setIdentityQuestion(questionDTO.getId());
                que.setText(QuestionDTO.getText());
                que.setIdTipoTag(QuestionDTO.getIdTipoTag());
                QuestionDB.add(que);
                /**
                 * Answers
                 */
                Set<AnswerDTO> listAnswers = AnswerDTO.getAnswers();
                Set<Answers> listAnswersDB = new HashSet<>();

                for (AnswersDTO AnswersDTO : listAnswer) {
                    Answers ans = new Answers();
                    ans.setIdentityAnswer(AnswerDTO.getId());
                    asn.setText(AnswerDTO.getText());
                    listAnswersDB.add(ans);
                }
                dom.setAnswer(listAnswersDB);
            }
            questn.setQuestion(QuestionDB);
            questionarioRepository.save(questn);
            }
        }
    }

这是DTO实体

public class QuestionariyDTO implements Serializable{

    private static final long serialVersionUID = -1886966747159529916L;
    private int id;
    private int identityQuestionary;
    private String title;
    private Set<QuestionDTO> questions;

}

public class QuestionDTO implements Serializable {

    private static final long serialVersionUID = -1886966747159529916L;
    private int id;
    private String text;
    private Long idTipoTag;
    private Set<AnswerDTO> answers;
}

public class AnswerDTO implements Serializable{

    private static final long serialVersionUID = -1886966747159529916L;
    private Long id;
    private String text;
    private Long identityAnswer;
}

此为json结构示例

    [
    {
        "id": 1,
        "title": "TITLE",
        "questions": [
            {
                "id": 1, <--- this id is only "identityQuestion" on db is not the primary key
                "text": "some text",
                "idTipoTag": 1,
                "answers": [
                    {
                        "id": 1, <--- this id is only "identityAnswer" on db is not the primary key
                        "text": "answ 1"
                    },
                    {
                        "id": 2,
                        "text": "answ 2"
                    },
                    {
                        "id": 3,
                        "text": "answ 3"
                    },
                    {
                        "id": 4,
                        "text": "answ 4"   
                    }
                    ]
            },
            {
                "id": 2,
                "text": "some text",
                "idTipoTag": 1,
                "answers": [
                    ...

                    .

                    .
                    .
                    .
                    .
                    ]
            },
...
        ]
    }
]

很抱歉,很长的帖子。

编辑-解决方案:

随机保存的原因是HashSet,它们没有顺序,并且总是将日期保存在随机位置。

我刚刚将所有Set值都转换为List。完成后,当我去实例化对象时,我使用LinkedList作为订单值!

这项工作对我来说很生气!

这是一个示例:

之前:

Set<QuestionDTO> listQuestion = questionaryDTO.getQuestion();
Set<Question> QuestionDB = new HashSet<>();

之后:

List<QuestionDTO> listQuestion = questionaryDTO.getQuestion();
List<Question> QuestionDB = new LinkedList<>();

希望对某人来说这很用:)

1 个答案:

答案 0 :(得分:0)

随机保存的原因是HashSet,它们没有顺序,并且总是将日期保存在随机位置。

我刚刚将所有Set值都转换为List。完成后,当我去实例化对象时,我使用LinkedList作为订单值!

这项工作对我来说很生气!

这是一个示例:

之前:

Set<QuestionDTO> listQuestion = questionaryDTO.getQuestion();
Set<Question> QuestionDB = new HashSet<>();

之后:

List<QuestionDTO> listQuestion = questionaryDTO.getQuestion();
List<Question> QuestionDB = new LinkedList<>();

希望对某人来说这很用:)