猿问

尝试在 spring boot 中从 null 一对一属性分配 id

我在spring boot中遇到这个错误:


试图从空一对一属性中分配 id [com.endoorment.models.entity.ActionLang.action]


我的代码:


    @Embeddable

public class ActionLangId implements Serializable {


 private static final long serialVersionUID = 1 L;


 @NotNull

 @Column(name = "actions_id")

 private Integer actionId;


 @NotNull

 @Column(name = "langs_id")

 private Integer langId;


 public ActionLangId() {}


 public ActionLangId(Integer actionId, Integer langId) {

  super();

  this.actionId = actionId;

  this.langId = langId;

 }


 public Integer getActionId() {

  return actionId;

 }


 public void setActionId(Integer actionId) {

  this.actionId = actionId;

 }


 public Integer getLangId() {

  return langId;

 }


 public void setLangId(Integer langId) {

  this.langId = langId;

 }


 @Override

 public boolean equals(Object o) {

  if (this == o) return true;

  if (o == null || getClass() != o.getClass())

   return false;


  ActionLangId that = (ActionLangId) o;

  return Objects.equals(actionId, that.actionId) &&

   Objects.equals(langId, that.langId);

 }


 @Override

 public int hashCode() {

  return Objects.hash(actionId, langId);

 }

}


@Entity

@Table(name = "actions_langs")

public class ActionLang {


 @EmbeddedId

 private ActionLangId id;


 @ManyToOne(fetch = FetchType.LAZY)

 @MapsId("actionId")

 @JoinColumn(name = "actions_id")

 private Action action;


 @ManyToOne(fetch = FetchType.LAZY)

 @MapsId("langId")

 @JoinColumn(name = "langs_id")

 private Lang lang;


 @NotNull(message = "null")

 @Size(max = 45, message = "short")

 private String name;


 public ActionLang() {}


 public ActionLang(ActionLangId actionlangid, String name) {

  this.id = actionlangid;

  this.name = name;

 }



 public ActionLangId getId() {

  return id;

 }


 public void setId(ActionLangId id) {

  this.id = id;

 }


 public String getName() {

  return name;

 }


 public void setName(String name) {

  this.name = name;

 }


 @Override

 public String toString() {

  return "ActionLang [id=" + id + ", name=" + name + "]";

 }

}


温温酱
浏览 156回答 3
3回答

青春有我

我的解决方案,实体行动:@Entity&nbsp;@Table(name = "actions")public class Action {@Idprivate Integer id;&nbsp; &nbsp; @OneToMany(mappedBy = "action")&nbsp; &nbsp; private List<ActionLang> actionlang = new ArrayList<>();&nbsp;public Action() { }public Action(Integer id) {this.id = id;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public List<ActionLang> getActionLang() {return actionlang;}public void addActionLang(ActionLang actionlang) {&nbsp; &nbsp; this.actionlang.add(actionlang);}public void removeActionLang(ActionLang actionlang) {&nbsp; &nbsp; this.actionlang.remove(actionlang);}@Overridepublic String toString() {return "id: " + id ;}}实体 ActionLang,@Entity&nbsp;@Table(name = "actions_langs")public class ActionLang {@EmbeddedIdprivate ActionLangId id;@ManyToOne(fetch = FetchType.LAZY)@MapsId("actionId")@JoinColumn(name = "actions_id", nullable = false)@OnDelete(action = OnDeleteAction.CASCADE)@JsonIgnoreprivate Action action;@ManyToOne(fetch = FetchType.LAZY)@MapsId("langId")@JoinColumn(name = "langs_id", nullable = false)@OnDelete(action = OnDeleteAction.CASCADE)@JsonIgnoreprivate Lang lang;@NotNull(message="null")@Size(max = 45, message="short")private String name;public ActionLang() {}public ActionLang(ActionLangId actionlangid, String name) {&nbsp; &nbsp; this.id = actionlangid;&nbsp; &nbsp; this.name = name;}public ActionLangId getId() {return id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void setId(ActionLangId id) {this.id = id;}&nbsp; &nbsp; public Action getAction() {return action;}public void setAction(Action action) {this.action = action;}public Lang getLang() {return lang;}public void setLang(Lang lang) {&nbsp; &nbsp; this.lang = lang;&nbsp; &nbsp;}@Overridepublic String toString() {return "ActionLang [id=" + id + ", name=" + name + "]";&nbsp; &nbsp;}}服务@Componentpublic class ActionDAOService {@Autowired&nbsp; &nbsp; private IActionDao actionRepository;@Autowired&nbsp; &nbsp; private IActionLangDao actionlangRepository;@Transactionalpublic Action saveAction(Integer idlang, String name){&nbsp; &nbsp; Lang lang = new Lang();&nbsp; &nbsp; lang.setId(idlang);&nbsp; &nbsp; Integer id = actionRepository.findActionId();&nbsp; &nbsp; if(id == null) {&nbsp; &nbsp; &nbsp; &nbsp; id=(Integer) 1;&nbsp; &nbsp; }&nbsp; &nbsp; Action action = new Action(id);&nbsp; &nbsp; actionRepository.save(action);&nbsp; &nbsp; ActionLang actionlang = new ActionLang(new ActionLangId(id, idlang),name);&nbsp; &nbsp; action.addActionLang(actionlang);&nbsp; &nbsp; actionlang.setAction(action);&nbsp; &nbsp; actionlang.setLang(lang);&nbsp; &nbsp; actionlangRepository.save(actionlang);&nbsp; &nbsp; return action;}}

隔江千里

实体动作@Entity&nbsp;@Table(name = "actions")public class Action {&nbsp; &nbsp; @Id&nbsp; &nbsp; private Integer id;&nbsp; &nbsp; @OneToMany(mappedBy = "action", cascade = CascadeType.ALL, orphanRemoval = true)&nbsp; &nbsp; private List<ActionLang> actionlang = new ArrayList<>();&nbsp; &nbsp; public Action() {&nbsp; &nbsp; }&nbsp; &nbsp; public Action(Integer id) {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public Integer getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(Integer id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public List<ActionLang> getActionlang() {&nbsp; &nbsp; &nbsp; &nbsp; return actionlang;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Action [id=" + id + ", actionlang=" + actionlang + ", getId()=" + getId() + ", getActionlang()="&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + getActionlang() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()="&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + super.toString() + "]";&nbsp; &nbsp; }}

幕布斯7119047

我已经修改了服务,但出现了同样的错误@Transactionalpublic Action saveAction(Integer idlang, String name){&nbsp; &nbsp; Integer id = actionRepository.findActionId();&nbsp; &nbsp; if(id == null) {id=(Integer) 1;}&nbsp; &nbsp; Action action = new Action(id);&nbsp; &nbsp; ActionLang actionlang = new ActionLang(new ActionLangId(id, idlang),name);&nbsp; &nbsp; action.getActionlang().add(actionlang);&nbsp; &nbsp; actionRepository.save(action);&nbsp; &nbsp; return action;}&nbsp;动作的结构是这样的:{&nbsp; &nbsp; "id": 2,&nbsp; &nbsp; "actionlang": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "actionId": 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "langId": 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "hkjhlhklhkllñkñl"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]}
随时随地看视频慕课网APP

相关分类

Java
我要回答