在春季保存带有@EelementCollection标签的元素

时间:2018-11-30 19:34:21

标签: spring hibernate spring-boot

大家好,

我有一个名为Engine的类,在其中我指定了一个称为约束的元素集合。当我发送json请求时,它要么无法将新实例完全保存在数据库中(引擎实例),要么不保存约束就保存它。如果有人可以澄清我的代码或解决此特定问题的方法有什么问题,我将不胜感激。

这是我的课程:

引擎实体类:

@Entity
@Table(name = "quiz_engines")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
        value = {"lastModified"},
        allowGetters = true
)
public class Engine implements Model {
@Id
@Column(name = "engine_id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = com.QCMGenerator.QCMGenerator.Model.Test.class)
@JoinColumn(name = "test_id", referencedColumnName = "test_id", nullable = false, updatable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Test test;

@Column(name = "quiz_name", nullable = false)
@NotNull
private String name;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_modified", nullable = false)
@LastModifiedDate
private Date lastModified;

@ElementCollection
@CollectionTable(name = "engine_constraints", joinColumns = @JoinColumn(name = "engine_id"))
private List<EngineConstraint> constraints;

public Engine() {
}

public Engine(@NotNull String name, List<EngineConstraint> constraints) {
    this.name = name;
    this.constraints = constraints;
}

public Long getId() {
    return id;
}

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

public Test getTest() {
    return test;
}

public void setTest(Test test) {
    this.test = test;
}

public String getName() {
    return name;
}

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

public Date getLastModified() {
    return lastModified;
}

public void setLastModified(Date lastModified) {
    this.lastModified = lastModified;
}

public List<EngineConstraint> getConstraints() {
    return constraints;
}

public void setConstraints(List<EngineConstraint> constraints) {
    this.constraints = constraints;
}

}

引擎DTO类:

public class EngineDTO implements ModelDTO {

private Long id;

@JsonIgnore
private TestDTO test;

private String name;

private Date lastModified;

private EngineConstraintList constraints;

public Long getId() {
    return id;
}

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

public TestDTO getTest() {
    return test;
}

public void setTest(TestDTO test) {
    this.test = test;
}

public String getName() {
    return name;
}

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

public Date getLastModified() {
    return lastModified;
}

public void setLastModified(Date lastModified) {
    this.lastModified = lastModified;
}

public EngineConstraintList getConstraints() {
    return constraints;
}

public void setConstraints(EngineConstraintList constraints) {
    this.constraints = constraints;
}

}

EngineConstraintList类:

@Embeddable
public class EngineConstraintList implements Serializable {

private List<EngineConstraint> engineConstraints;

public EngineConstraintList() {}

public List<EngineConstraint> getEngineConstraints() {
    return engineConstraints;
}

public void setEngineConstraints(List<EngineConstraint> engineConstraints) {
    this.engineConstraints = engineConstraints;
}

}

EngineConstraint类:

@Embeddable
public class EngineConstraint implements Serializable {

@Column
private Integer weight;

@Column
private Integer questionsCount;

public EngineConstraint() {
}

public EngineConstraint(Integer weight, Integer questionsCount) {
    this.weight = weight;
    this.questionsCount = questionsCount;
}

public Integer getWeight() {
    return weight;
}

public void setWeight(Integer weight) {
    this.weight = weight;
}

public Integer getQuestionsCount() {
    return questionsCount;
}

public void setQuestionsCount(Integer questionsCount) {
    this.questionsCount = questionsCount;
}

}

和我的控制器中的post方法

public ResponseEntity<?> saveEngine(
        @PathVariable(value = "testID") Long testID,
        @Valid @RequestBody EngineDTO newEngine
){
    if(!testRepo.existsById(testID)){
        throw new ResourceNotFoundException("No test with the ID '"+testID+"' was found...");
    }

    Test test = testRepo.findById(testID).get();
    EngineDTO savedEngineDTO = null;
    try {
        Engine engine;
        if(newEngine.getName() != null && newEngine.getConstraints() != null){
            engine = new Engine(newEngine.getName(), newEngine.getConstraints().getEngineConstraints());
        }else{
            throw new ResourceNotFoundException("The engine name field and constraints field cannot be empty....");
        }

        engine.setTest(test);
        Engine savedEngine = engineRepo.save(engine);
        savedEngineDTO = convertToDTO(savedEngine);
    }catch (Exception e){
        System.out.println(e.getMessage());
    }

    return ResponseEntity.status(HttpStatus.CREATED).build();
}

预先感谢大家的帮助和宝贵的时间。非常感谢。

祝大家有美好的一天...

0 个答案:

没有答案