简体   繁体   English

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

[英]Attempted to assign id from null one-to-one property in spring boot

I have this error in spring boot: 我在spring boot中有这个错误:

attempted to assign id from null one-to-one property [com.endoorment.models.entity.ActionLang.action] 尝试从空的一对一属性分配ID [com.endoorment.models.entity.ActionLang.action]

My code: 我的代码:

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

Service: 服务:

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

Thanks 谢谢

Entity action 实体行动

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

I have modified service and I have the same error 我修改了服务,并且遇到了相同的错误

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

And the structure of action is this: 动作的结构是这样的:

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

My solution, 我的解决方案

Entity Action: 实体动作:

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

Entity ActionLang, 实体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 + "]";   }
}

Service 服务

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate 尝试从 null 一对一属性分配 id - Hibernate attempted to assign id from null one-to-one property 休眠一对零或一个映射。 例外:尝试从空的一对一属性分配ID? - Hibernate one to zero or one mapping. Exception: attempted to assign id from null one-to-one property? Hibernate merge org.hibernate.id.IdentifierGenerationException:尝试从空的一对一属性分配ID - Hibernate merge org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property org.hibernate.id.IdentifierGenerationException:“试图从空一对一属性分配 id” - org.hibernate.id.IdentifierGenerationException: "attempted to assign id from null one-to-one property" Hibernate OneToOne 尝试从 null 一对一属性分配 id - Hibernate OneToOne attempted to assign id from null one-to-one property Hibernate 错误:试图从 null 一对一属性分配 id。 为什么? - Hibernate Error : attempted to assign id from null one-to-one property. Why? org.hibernate.id.IdentifierGenerationException:试图从空一对一属性(hibernate+postgres)分配id - org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property (hibernate+postgres) org.hibernate.id.IdentifierGenerationException:尝试从空的一对一属性(使用session.merge)分配ID - org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property (with session.merge) Java JPA-将多对多结构保存到DB-错误:尝试从空的一对一属性分配ID - Java JPA - save many-to-many structure to DB - error: attempted to assign id from null one-to-one property 错误:试图从空一对一属性 [com.dbtest.springboot.db_model.Family.user] 分配 id - Error: attempted to assign id from null one-to-one property [com.dbtest.springboot.db_model.Family.user]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM