I am trying to join to Hibernate Entities in a OneToOne Mapping. I am able to fetch the data for a given primary key from the Main Entity, the joining entity, however, returns null. I am new to hibernate and any help will be appreciated.
I have two Tables,
PT_CORE
- Primary Key: ptId - Integer;
- Foreign Key: stId(ST_AUX) - Integer;
- Columns: ptId, ptName
ST_AUX
- Primary Key: stId;
- Columns: stId, stName
The two tables get populated by other applications and mine is a read-only operation.
Below is my first Entity class(PtCore.java)
@Entity
@Table(name="PT_CORE")
public class PtCore implements Serializable{
@Id
@Column(name="ptId", nullable = false)
private int id;
@Column(nullable=false)
private int stId; //The Foreign key column
@OneToOne
@JoinTable( name = "core_aux", joinColumns = {@JoinColumn(Name="ptId")},
inverseJoinColumns = {@JoinColumn(Name="stId")}
)
private StAux staux;
//Getters, setters and toString() for above
}
StAux is another Entity, defined as below,
@Entity
@Table(name="ST_AUX")
public class StAux implements Serializable {
@Id
@Column(nullable=false)
private Integer stId;
@OneToOne
private PtCore ptcore;
@Column
private String stName;
//Getters, Setters and toString follow.
}
I do below in the Service method:
PtCore obj = (PtCore) session.get(PtCore.class,1);
System.out.println(obj);
In the Results, I get the value of ptName, but the stAux class variables are null, Indicating that the join does not work as expected.