有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java试图在spring引导中从null onetoone属性分配id

我在spring boot中遇到以下错误:

attempted to assign id from null one-to-one property [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 + "]";
 }
}

服务:

@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) 个答案

  1. # 1 楼答案

    我修改了服务,我有相同的错误

    @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. # 2 楼答案

    我的解决方案

    实体行动:

    @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;
    }
    }
    
  3. # 3 楼答案

    实体行为

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