0
我有一個使用Map的雙向映射。當我嘗試堅持它時,我得到一個錯誤,認爲hibernate不能將null插入到mapkey列中。Hibernate嘗試將null插入到mapkey中
地圖實體:
@Entity
@Table(name = "map_parent)
public class MapParent {
private long id;
private Map<String, Metadata> datamap = new HashMap<String, Metadata>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "MY_SEQ_GEN")
@SequenceGenerator(name = "MY_SEQ_GEN",
sequenceName = "my_base_sequence",
allocationSize = 1)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@OneToMany(cascade = CascadeType.ALL)
@MapKeyColumn(name = "mapKey")
@JoinColumn(name="datamap_id")
public Map<String, Metadata> getDataMap() {
return dataMap;
}
public void setDataMap(Map<String, Metadata> dataMap) {
this.dataMap = dataMap;
}
}
價值實體:
@Entity
@Table(name = "my_metadata")
public class Metadata {
private boolean binary = false;
private String data;
private MapParent parent;
public Metadata() {
//empty bean constructor
}
@Column(name = "binary")
public boolean isBinary() {
return binary;
}
public void setBinary(boolean binary) {
this.binary = binary;
}
@Column
public String getData() [
return data;
}
public void setData(String data) {
this.data = data;
}
@ManyToOne
@JoinColumn(name = "datamap_id", updatable = false, insertable = false)
public MapParent getMapParent() {
return mapParent;
}
public void setMapParent(MapParent mapParent) {
this.mapParent = mapParent;
}
的堅持代碼:
public void submit() {
MapParent parent = new MapParent();
Metadata metadata1 = new Metadata();
metadata1.setData("Data 1");
parent.getDataMap().put("Metadata1", metadata1);
entityManager.persist(parent);
entityManager.flush();
}
上面的代碼提供了以下堆棧跟蹤:
org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("MY_Metadata"."MAPKEY")
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:566)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:215)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:58)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:943)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1075)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3820)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3897)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1361)
at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:493)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:186) [hibernate-core-4.2.18.Final-redhat-2.jar:4.2.18.Final-redhat-2]
... 91 more
任何幫助解決這個錯誤是不勝感激。
錯誤在於mapkey被設置爲null,而不是連接列。這不是同一個問題。 –
在保存之前設置'metadata1.setMapParent(parent)'。 –
我試過了。它沒有什麼區別。 –