Hibernate: How to join a table with a column which is not a foreign key

I’m trying to create a OneToOne relation between tartikel and teigenschaft with the primary key kArtikel in tartikel. The code snippet in TArtikelEntity:

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "kArtikel", referencedColumnName="kArtikel")
public TeigenschaftEntity getTeigenschaftEntity() {
    return teigenschaftEntity;
}

public void setTeigenschaftEntity(TeigenschaftEntity teigenschaftEntity) {
    this.teigenschaftEntity = teigenschaftEntity;
}

The complete entity of teigenschaft:

@Entity
@Table(name = "teigenschaft", schema = "dbo", catalog = "Mandant_EinsZwei")
public class TeigenschaftEntity {

private int kEigenschaft;
private int kArtikel;
private String cWaehlbar;
private Integer nSort;
private String cTyp;
private String cAktiv;

@Id
@Column(name = "kEigenschaft")
public int getkEigenschaft() {
    return kEigenschaft;
}

public void setkEigenschaft(int kEigenschaft) {
    this.kEigenschaft = kEigenschaft;
}

@Basic
@Column(name = "kArtikel")
public int getkArtikel() {
    return kArtikel;
}

public void setkArtikel(int kArtikel) {
    this.kArtikel = kArtikel;
}

@Basic
@Column(name = "cWaehlbar")
public String getcWaehlbar() {
    return cWaehlbar;
}

public void setcWaehlbar(String cWaehlbar) {
    this.cWaehlbar = cWaehlbar;
}

@Basic
@Column(name = "nSort")
public Integer getnSort() {
    return nSort;
}

public void setnSort(Integer nSort) {
    this.nSort = nSort;
}

@Basic
@Column(name = "cTyp")
public String getcTyp() {
    return cTyp;
}

public void setcTyp(String cTyp) {
    this.cTyp = cTyp;
}

@Basic
@Column(name = "cAktiv")
public String getcAktiv() {
    return cAktiv;
}

public void setcAktiv(String cAktiv) {
    this.cAktiv = cAktiv;
}

}

Here is where I’m getting the NullPointerException (Line 4):

session.beginTransaction();
TArtikelEntity tArtikelEntity = session.get(TArtikelEntity.class, 189820);
System.out.println(tArtikelEntity.toString());
System.out.println(tArtikelEntity.getTeigenschaftEntity().getkEigenschaft()+" <---- kEigenschaft");
session.getTransaction().commit();

I located the problem in the second query. It’s not using kArtikel but kEigenschaft (the primary key of teigenschaft):

select teigenscha0_.kEigenschaft as kEigensc1_12_0_, teigenscha0_.cAktiv as cAktiv2_12_0_, teigenscha0_.cTyp as cTyp3_12_0_, teigenscha0_.cWaehlbar as cWaehlba4_12_0_, teigenscha0_.kArtikel as kArtikel5_12_0_, teigenscha0_.nSort as nSort6_12_0_ from Mandant_EinsZwei.dbo.teigenschaft teigenscha0_ where teigenscha0_.kEigenschaft=?|select teigenscha0_.kEigenschaft as kEigensc1_12_0_, teigenscha0_.cAktiv as cAktiv2_12_0_, teigenscha0_.cTyp as cTyp3_12_0_, teigenscha0_.cWaehlbar as cWaehlba4_12_0_, teigenscha0_.kArtikel as kArtikel5_12_0_, teigenscha0_.nSort as nSort6_12_0_ from Mandant_EinsZwei.dbo.teigenschaft teigenscha0_ where teigenscha0_.kEigenschaft=189820

But how can that be? I mentioned the JoinColumn in TArtikelEntity to kArtikel. Why is it not using kArtikel but kEigenschaft?

Null pointer exception isn’t due to join. It’s because you are converting a null object to string.

try this :

System.out.println(tArtikelEntity==null?null:tArtikelEntity.toString());