为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

温温酱 2021-12-01 14:38:13
我在spring boot中遇到这个错误:试图从空一对一属性中分配 id [com.endoorment.models.entity.ActionLang.action]我的代码:    @Embeddablepublic 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 + "]"; }}
查看完整描述

3 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

我的解决方案,


实体行动:


@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;

}

}


查看完整回答
反对 回复 2021-12-01
?
隔江千里

TA贡献1906条经验 获得超10个赞

实体动作


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

    }

}


查看完整回答
反对 回复 2021-12-01
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

我已经修改了服务,但出现了同样的错误


@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"

        }

    ]

}


查看完整回答
反对 回复 2021-12-01
  • 3 回答
  • 0 关注
  • 155 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信