[英]Hibernate bidirectional @OneToMany mapping return Column 'xxx' cannot be null
I have a problem with Hibernate bidirectional @OneToMany
mapping.我对 Hibernate 双向
@OneToMany
映射有疑问。 This is my root entity:这是我的根实体:
@Entity(name = "Proposta")
@Table(name = "PROPOSTA")
public class Proposta implements Serializable {
private static final long serialVersionUID = -705828064150128352L;
public Proposta() {
super();
}
@Id
@Column(name = "COD_PROPOSTA")
private Integer codProposta;
@OneToMany(mappedBy="proposta", fetch=FetchType.EAGER)
@Cascade({ CascadeType.ALL })
private List<PropostaUL> listaPropostaUl;
}
This is my child entity:这是我的子实体:
@Entity(name = "PropostaUL")
@Table(name = "PROPOSTA_UL")
public class PropostaUL {
public PropostaUL() {
super();
}
@Id
@Column(name = "COD_FONDO_UE_UL")
private String codFondoUeUl;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "COD_PROPOSTA")
private Proposta proposta;
}
When I read the values of the two tables everything works properly;当我读取这两个表的值时,一切正常; when I try to save it instead I have the following error
当我尝试保存它时,出现以下错误
gen 05, 2021 10:14:03 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1048, SQLState: 23000
gen 05, 2021 10:14:03 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Column 'COD_PROPOSTA' cannot be null
gen 05, 2021 10:14:03 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
The save call it the following保存调用它如下
protected Object saveOrUpdate(Object obj, List<String> errorLog) {
Transaction transaction = null;
try {
if (session == null || ! session.isOpen()) {
session = HibernateFactory.getSessionFactory().openSession();
}
transaction = session.beginTransaction();
session.saveOrUpdate(obj);
transaction.commit();
} catch (HibernateException e) {
obj = null;
errorLog.add(e.getMessage());
} finally {
if (session != null) {
session.close();
}
}
return obj;
}
I read other same problems in the forum but all the solutions suggested doesn't worked.我在论坛中阅读了其他相同的问题,但建议的所有解决方案都不起作用。 I checked this site too
我也检查了这个网站
https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/ https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/
and even if the case analyzed seems the same, and the configuration too, the problem persists.即使分析的案例看起来相同,配置也一样,问题仍然存在。 The value of
COD_PROPOSTA
in the root entity is provided and it is not null
.提供了根实体中
COD_PROPOSTA
的值,它不是null
。 Do you have any suggestion on how to solve the problem?你对如何解决这个问题有什么建议吗?
The column "COD_PROPOSTA" and "COD_FONDO_UE_UL" marked as Id cannot be null and you are not giving any value to that property (unless you do it manually once created the object).标记为 Id 的“COD_PROPOSTA”和“COD_FONDO_UE_UL”列不能是 null,并且您没有为该属性提供任何值(除非您在创建对象后手动执行此操作)。 To give an Id automatically you can add the annotation
@GeneratedValue
to codProposta
and codFondoUeUl
.要自动提供 Id,您可以将注释
@GeneratedValue
添加到codProposta
和codFondoUeUl
。
As it stated in the documentation :正如文档中所述:
Whenever a bidirectional association is formed, the application developer must make sure both sides are in-sync at all times.
每当形成双向关联时,应用程序开发人员必须确保双方始终保持同步。
So you should add addPropostaUl()
and removePropostaUl()
utility methods that synchronize both ends whenever a child element is added or removed:因此,您应该添加
addPropostaUl()
和removePropostaUl()
实用程序方法,以便在添加或删除子元素时同步两端:
@Entity
public class Proposta implements Serializable {
// ...
@OneToMany(mappedBy = "proposta", fetch = FetchType.EAGER)
@Cascade(CascadeType.ALL)
private List<PropostaUL> listaPropostaUl;
public void addPropostaUl(PropostaUL propostaUl) {
listaPropostaUl.add(propostaUl);
propostaUl.setProposta(this);
}
public void removePropostaUl(PropostaUL propostaUl) {
listaPropostaUl.remove(propostaUl);
propostaUl.setProposta(null);
}
}
and then use only these methods when you want modify this association.然后在要修改此关联时仅使用这些方法。
Example (adding new PropostaUL
to the existent Proposta
):示例(将新的
PropostaUL
添加到现有的Proposta
):
Transaction transaction = session.beginTransaction();
Proposta proposta = session.load(Proposta.class, propostaId);
PropostaUL newPropostaUL = new PropostaUL();
// ...
proposta.addPropostaUl(newPropostaUL);
session.saveOrUpdate(proposta);
transaction.commit();
Thanks all for your answers.谢谢大家的回答。
I found the problem.我发现了问题。
The object that I have to save is json object converted in Java object via Jackson library. The object that I have to save is json object converted in Java object via Jackson library.
So I added @JsonManagedReference and @JsonBackReference to the objects and it works.所以我在对象中添加了@JsonManagedReference 和@JsonBackReference 并且它可以工作。
This is the right code这是正确的代码
@Entity(name = "Proposta")
@Table(name = "PROPOSTA")
public class Proposta implements Serializable {
private static final long serialVersionUID = -705828064150128352L;
public Proposta() {
super();
}
@Id
@Column(name = "COD_PROPOSTA")
private Integer codProposta;
@JsonManagedReference
@OneToMany(mappedBy="proposta", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
private List<PropostaUL> listaPropostaUl;
}
and the PropostaUL object和 PropostaUL object
@Entity()
@Table(name = "PROPOSTA_UL")
public class PropostaUL {
public PropostaUL() {
super();
}
@Id
@Column(name = "COD_FONDO_UE_UL")
private String codFondoUeUl;
@JsonBackReference
@ManyToOne
@JoinColumn(name = "COD_PROPOSTA")
private Proposta proposta;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.