Hibernate尝试在mapkey中插入null

时间:2016-06-30 14:29:35

标签: java hibernate

我使用Map进行双向映射。当我试图坚持它时,我得到一个错误,即hibernate无法在mapkey列中插入null。

地图实体:

@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

非常感谢您解决此错误的任何帮助。

1 个答案:

答案 0 :(得分:0)

找到一个解决方案 - 将mapKey添加为元数据中的一个字段,并将其与该集合一起保存。