Search code examples
javahibernatejpaormhibernate-mapping

Null referency for child using @OneToOne annotation


I am getting a null reference on a child object.

Let me explain:

I have two classes:

@Entity
@Table (name="CONTROLADORES")
@NamedQueries({
    @NamedQuery(name="Controlador.findAll", query="SELECT c FROM Controlador c"), 
    @NamedQuery(name="Controlador.findByIdWithChilds", query="SELECT c FROM Controlador c JOIN FETCH c.cruce WHERE c.id = :id")
})
public class Controlador implements Serializable{

    private static final long serialVersionUID = -1018356240274259744L;

    @Expose
    private long id;

    @Id
        @GeneratedValue
        @GenericGenerator(name="increment", strategy = "increment")
        @Column(name="controlador_id")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn  
    public CruceSemaforos getCruce() {
        return cruce;
    }

    public void setCruce(CruceSemaforos cruces) {
        this.cruce = cruce;
    }
}

and

@Entity
@Table (name="CRUCE_SEMAFOROS")
@NamedQueries({
    @NamedQuery(name="CruceSemaforos.findAll", query="SELECT c FROM CruceSemaforos c")
})
public class CruceSemaforos implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 140756341984196420L;

    @Expose
    private long id;

    @Id
    @GeneratedValue
    @Column(name="cruce_id")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    private Controlador controlador;

    @OneToOne
    @JoinColumn(name="CONTROLADORES_idControlador", referencedColumnName = "controlador_id")
    public Controlador getControlador() {
        return controlador;
    }

    public void setControlador(Controlador controlador) {
        this.controlador = controlador;
    }
}

I can create and presist instances of this objects and in the database, the foreing key i set with the correct value (i mean: if i have a Controlador which has a Cruce in the table CRUCE_SEMAFOROS the controlador id is setted in the column CONTROLADORS_idControlador.

This part works fine.

The problem is that when i try to get the Controlador, the cruce attibute of this class is null!

I think thats maybe an annotation error (i am new in the world of annotation for hibernate).

Please i would appreciate any kind of help!


Solution

  • For every bi-directional association, you must choose one and only one owner side. In your case, the CruceSemaforos.controlador is the owner of this association.

    So you need to declare the other side as inverse, by using the mappedBy directive:

    public class Controlador implements Serializable {
    
        ...
    
        @OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL, mappedBy = "controlador") 
        public CruceSemaforos getCruce() {
            return cruce;
        }
    
    }