Saurav Ojha
Saurav Ojha

Reputation: 155

Hibernate-OneToMany mapping for existing DB Tables

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

ST_AUX

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.

Upvotes: 0

Views: 1194

Answers (2)

pirho
pirho

Reputation: 12205

First of all you have the mapping information existing in your PT_CORE. And I assume it is something like FOREIGN KEY (stid) REFERENCES (stid). If you want to use existing schema and existing data I guess there is no mapping table core_aux really existing. At least you did not mention it. However it is visible as @JoinTable annotation but still there is this above mentioned foreign key which seems to be the real mapping (so again not the join table).

I suggest the following

remove this

@Column(nullable=false)
private int stId;  //The Foreign key column

from your PtCore. I think it is not needed. Also in PtCore, remove the @JoinTable (because what I told above) and add mapping informaiion to @OneToOne annotation, like:

@OneToOne
@JoinColumn(name = "stid")
private StAux staux;

from your PT_CORE.

Then in StAux alter also a bit:

@Id
@Column(name = "stid") // this might not be needed but if there is like "st_id"...
private Integer stId;  // so just for sure

@OneToOne(mappedBy = "staux") 
private PtCore ptcore;

Because you have existing tables and constraints there might raise errors if hibernate tries to auto-generate those again by JPA instructions.

Check this for example for more information.

UPDATE: just realized also that in your title is @OneToMany but in your code is @OneToOne.

So you might want to elaborate your question and/or title a bit.

Upvotes: 1

Mustafa Çil
Mustafa Çil

Reputation: 776

In your relation, the owning side is PtCore, the inverse side is StAux.

In bidirectional OneToOne relations, the inverse side has to have the mappedBy attribute. Actually, the mappedBy attribute contains the name of the association-field on the owning side.

So, you must change your inverse side code (StAux Entity). You have to add mappedBy attribute to @OneToOne in StAux class:

 @OneToOne(mappedBy="staux")                       
  private PtCore ptcore;

Upvotes: 0

Related Questions