在Spring Boot中尝试从空的一对一属性分配ID

时间:2018-10-21 16:21:26

标签: java spring spring-data-jpa

我在Spring Boot中出现此错误:

  

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

我的代码:

    @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 + "]";
 }
}

服务:

@Transactional
public ActionLang saveAction(Integer idlang, String name) {

 Integer id = actionRepository.findActionId();

 Action action = new Action(id);
 actionRepository.save(action);

 ActionLang actionlang = new ActionLang(new ActionLangId(id, idlang), name);
 actionlangRepository.save(actionlang);
 return actionlang;
}

Structure actionlang: {
  "id": {
   "actionId": 2,
   "langId": 1
  },
  "name": "hkjhlhklhkllñkñl"

谢谢

3 个答案:

答案 0 :(得分:0)

实体动作

@Entity 
@Table(name = "actions")
public class Action {

    @Id
    private Integer id;

    @OneToMany(mappedBy = "action", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ActionLang> actionlang = new ArrayList<>();

    public Action() {
    }

    public Action(Integer id) {
        super();
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

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

    public List<ActionLang> getActionlang() {
        return actionlang;
    }

    @Override
    public String toString() {
        return "Action [id=" + id + ", actionlang=" + actionlang + ", getId()=" + getId() + ", getActionlang()="
                + getActionlang() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()="
                + super.toString() + "]";
    }
}

答案 1 :(得分:0)

我修改了服务,但遇到了相同的错误

@Transactional
public Action saveAction(Integer idlang, String name){
    Integer id = actionRepository.findActionId();
    if(id == null) {id=(Integer) 1;}
    Action action = new Action(id);
    ActionLang actionlang = new ActionLang(new ActionLangId(id, idlang),name);
    action.getActionlang().add(actionlang);
    actionRepository.save(action);
    return action;
} 

动作的结构是这样的:

{
    "id": 2,
    "actionlang": [
        {
            "id": {
                "actionId": 2,
                "langId": 1
            },
            "name": "hkjhlhklhkllñkñl"
        }
    ]
}

答案 2 :(得分:0)

我的解决方案,

实体动作:

@Entity 
@Table(name = "actions")
public class Action {

@Id
private Integer id;

    @OneToMany(mappedBy = "action")
    private List<ActionLang> actionlang = new ArrayList<>();

 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) {
    this.actionlang.add(actionlang);
}

public void removeActionLang(ActionLang actionlang) {
    this.actionlang.remove(actionlang);
}

@Override
public String toString() {return "id: " + id ;}
}

实体ActionLang,

@Entity 
@Table(name = "actions_langs")
public class ActionLang {

@EmbeddedId
private ActionLangId id;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("actionId")
@JoinColumn(name = "actions_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Action action;

@ManyToOne(fetch = FetchType.LAZY)
@MapsId("langId")
@JoinColumn(name = "langs_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
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 String getName() {return name;}

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

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

    public Action getAction() {return action;}

public void setAction(Action action) {this.action = action;}

public Lang getLang() {return lang;}

public void setLang(Lang lang) {    this.lang = lang;   }

@Override
public String toString() {return "ActionLang [id=" + id + ", name=" + name + "]";   }
}

服务

@Component
public class ActionDAOService {

@Autowired
    private IActionDao actionRepository;

@Autowired
    private IActionLangDao actionlangRepository;

@Transactional
public Action saveAction(Integer idlang, String name){

    Lang lang = new Lang();
    lang.setId(idlang);

    Integer id = actionRepository.findActionId();
    if(id == null) {
        id=(Integer) 1;
    }

    Action action = new Action(id);
    actionRepository.save(action);

    ActionLang actionlang = new ActionLang(new ActionLangId(id, idlang),name);
    action.addActionLang(actionlang);
    actionlang.setAction(action);
    actionlang.setLang(lang);

    actionlangRepository.save(actionlang);

    return action;
}
}