无法执行语句约束[null] ManyToMany关联表

时间:2019-05-10 08:18:53

标签: java spring spring-boot spring-mvc

我实现了两个实体,即“ script”和“ libelleprerequis”,我创建了一个关联表“ scriptlibelleprerequis”以实现“多种多样”

生成数据库时,我有两个表和关联表,其中包含“ id_script”和“ id_libelleprerequis”

我创建了一个函数,该函数使我可以向“ ScriptController”中的脚本添加libelleprerequis(updateScriptLibellePrerequis) 我使用objectNode和对象libelleprerequis获取了JSON的id脚本。

JSON示例:

libellePrerequis:
--> id: 1
--> libelle_prerequis: "MY LIBELLE EXAMPLE"
--> produit: {id: 1, nom: "EXEMPLE"}
--> scripts: []
--> typologie: {id: 1, nom: "EXEMPLE"}
script:
--> id: 1
--> libelleprerequiss: []
--> nom: "EXAMPLE NAME SCRIPT"

但是,在该行上我的函数出现错误

"Script updateScriptLibellePrerequis = scriptRepository.save (savedScript);"

错误消息:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: The 'script_id' field can not be empty (null)

脚本:

@Table(name = "script")
public class Script implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "nom")
    private String nom;

    @OneToMany(
            mappedBy = "script",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<ScriptLibellePrerequis> libelleprerequiss = new ArrayList<>();

    public void addLibellePrerequis(LibellePrerequis libelleprerequis) {
        ScriptLibellePrerequis scriptLibellePrerequis = new ScriptLibellePrerequis( this, libelleprerequis );
        libelleprerequiss.add( scriptLibellePrerequis );
        libelleprerequis.getScripts().add( scriptLibellePrerequis );
    }

    public void removeLibellePrerequis(LibellePrerequis libelleprerequis) {
        ScriptLibellePrerequis scriptLibellePrerequis = new ScriptLibellePrerequis( this, libelleprerequis );
        libelleprerequis.getScripts().remove( scriptLibellePrerequis );
        libelleprerequiss.remove( scriptLibellePrerequis );
        scriptLibellePrerequis.setScript( null );
        scriptLibellePrerequis.setLibelleprerequis( null );
    }

LibellePrerequis:

@Table(name = "libelleprerequis")
public class LibellePrerequis implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "libelle_prerequis")
    private String libelle_prerequis;

    @ManyToOne
    private Produit produit;

    @ManyToOne
    private Typologie typologie;

    @OneToMany(
            mappedBy = "script",
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    private List<ScriptLibellePrerequis> scripts = new ArrayList<>();

ScriptLibellePrerequis:

@Table(name = "scriptlibelleprerequis")
public class ScriptLibellePrerequis implements Serializable {

    @Id
    @ManyToOne
    private Script script;

    @Id
    @ManyToOne
    private LibellePrerequis libelleprerequis;

    public Script getScript() {
        return script;
    }

    public void setScript(Script script) {
        this.script = script;
    }

    public LibellePrerequis getLibelleprerequis() {
        return libelleprerequis;
    }

    public void setLibelleprerequis(LibellePrerequis libelleprerequis) {
        this.libelleprerequis = libelleprerequis;
    }

我的控制器将脚本和libelleprerequis添加到我的关联表中:

@PostMapping("/create")
public ResponseEntity<Script> updateScriptLibellePrerequis(@RequestBody ObjectNode objectNode) {
    Long id_script = objectNode.get("script").get("id").asLong();
    String libelleprerequis = objectNode.get("libellePrerequis").toString();

    ObjectMapper objectMapper = new ObjectMapper();

    try {
        LibellePrerequis mylibelle = objectMapper.readValue(libelleprerequis, LibellePrerequis.class);

        Optional<Script> scriptData = scriptRepository.findById(id_script);
        if (scriptData.isPresent()) {
            Script savedScript = scriptData.get();

            savedScript.addLibellePrerequis(mylibelle);

            Script updateScriptLibellePrerequis = scriptRepository.save(savedScript);
            return new ResponseEntity<>(updateScriptLibellePrerequis, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

1 个答案:

答案 0 :(得分:0)

尝试添加:

@Id 
@ManyToOne
@JoinColumn