Hibernate没有插入外键值,尽管提供了相关的实体

时间:2013-10-03 07:47:59

标签: hibernate hibernate-mapping

我有3个关系类:ParametrosEntity,NomFicherosEntity和TipoCamposEntity。当试图保持ParametrosEntity对象时,问题出现了,用其他两个需要的对象提供它。

ParametrosEntity:

@Entity
@Table(name = "CON_DPARAMETROS")
public class ParametrosEntity {

@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "componenteId", column = @Column(name = "COM_IDENTIFICADOR",
                                                                   nullable = false)),
        @AttributeOverride(name = "clave", column = @Column(name = "PAR_CLAVE",
                                                            nullable = false)),
        @AttributeOverride(name = "fichero", column = @Column(name = "NOM_FICHERO",
                                                              nullable = false))})
private ParametrosPK id;
@Column(name = "PAR_VALOR")
private String valor;
@Column(name = "PAR_LABEL")
private String label;
@Column(name = "PAR_DESCRIPCION")
private String descripcion;
@Column(name = "PAR_PRESENTACION")
private String presentacion;
@Column(name = "PAR_PROGRAMA")
private String programa;

@OneToOne
@JoinColumns({
        @JoinColumn(name = "COM_IDENTIFICADOR", referencedColumnName = "COM_IDENTIFICADOR",
                    nullable = false, insertable = false, updatable = false),
        @JoinColumn(name = "NOM_FICHERO", referencedColumnName = "NOM_FICHERO",
                    nullable = false, insertable = false, updatable = false)})
private NombreFicherosEntity fichero;
@OneToOne
@JoinColumn(name = "TIP_TIPO", nullable = false, insertable = false, updatable = false)
private TipoCamposEntity tipo;

TipoCamposEntity:

@Entity
@Table(name = "CON_PTIPOCAMPOS")
public class TipoCamposEntity {

@Id
@Column(name = "TIP_TIPO")
private String tipo;
@Column(name = "TIP_DESCRIPCION")
private String descripcion;

NomFicherosEntity:

@Entity
@Table(name = "CON_DNOMFICHEROS")
public class NombreFicherosEntity {

@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "idComponente", column = @Column(name = "COM_IDENTIFICADOR",
                                                                   nullable = false)),
        @AttributeOverride(name = "fichero", column = @Column(name = "NOM_FICHERO",
                                                              nullable = false))})
private NombreFicherosPK id;

@OneToOne
@JoinColumn(name = "COM_IDENTIFICADOR", nullable = false, insertable = false, updatable = false)
private ComponentesEntity componente;

我在尝试保留ParametrosEntity对象时遇到问题。我将数据分配给对象ParametrosEntity,包括所需的实体,但插入时出现错误。

以下是我用于持久化的代码和获得的错误:

坚持代码:

ParametrosEntity param = new ParametrosEntity();

    Session session = HibernateUtil.getSession();
    Transaction tx = null;
    tx = session.beginTransaction();

    ParametrosPK paramPK = new ParametrosPK();

    paramPK.setClave("clave");
    paramPK.setComponenteId("SGACAC");
    paramPK.setFichero("sgacac");

    param.setId(paramPK);

    param.setDescripcion("dfasfasfadfaf");
    param.setLabel("label");

    param.setFichero(buscarNomPorIdyNombre("SGACAC", "sgacac")); //  This obtain an existing NombreFicherosEntity from the DB
    param.setTipo(buscarTipoPorTipo("Texto")); // This obtains an existing TipoCamposEntity from the DB

    session.save(param);

    tx.commit();

    session.close();

输出错误:

Hibernate: insert into CON_DPARAMETROS (PAR_DESCRIPCION, PAR_LABEL, PAR_PRESENTACION, PAR_PROGRAMA, PAR_VALOR, PAR_CLAVE, COM_IDENTIFICADOR, NOM_FICHERO) values (?, ?, ?, ?, ?, ?, ?, ?)
ORA-01400: cannot insert NULL into ("SGAINSDIST"."CON_DPARAMETROS"."TIP_TIPO")

我可以看到hibernate不包括插入查询中的列“TIP_TIPO”,但我不明白为什么不。我这里做错了什么?谢谢你的帮助。

0 个答案:

没有答案