0
votes

I am facing the following scenario:

Table Store: |Id|...|CostCenterNumber|

Table MasterData: |dennskdnr|...|

My current mapping looks like the following

@Entity
@Table(name = "Store")
public class Store implements Identifiable {
[...]
    @OneToOne(optional = true)
    @JoinColumn(name = "CostCenterNumber", insertable = false, updatable = false)
    private MasterData masterData;
[...]
}

and

@Entity
@Table(name = "MasterData")
public class MasterData {
[...]
    @OneToOne(optional = true)
    @JoinColumn(name = "dennskdnr")
    private Store store;
[...]
}

Leading me to the following exception:

org.hibernate.TypeMismatchException: Provided id of the wrong type for class datamodel.Store. Expected: class java.lang.String, got class java.lang.Integer

1
This mapping doesn't make sense. The CostCenterNumber join column is sufficient to know which store belongs to which master data. What are you trying to achieve? - JB Nizet
I wanted to have a bidirectional association. An additional requirement is that there might be no join partner for each store. - Coxer
A bidirectional association doesn't need two different columns to map the association. A single one is sufficient. - JB Nizet
But the names are different and a requirement dictates that i must not rename them - Coxer
The names of what? What do you want to achieve? Your single bidirectional association is mapped twice at the moment, which doesn't make sense. You haven't describe what and where the dennskdnr column is, and what you would like to achieve. - JB Nizet

1 Answers

2
votes

Here's how the association should be mapped:

@Entity
@Table(name = "Store")
public class Store implements Identifiable {

    @OneToOne(optional = true)
    @JoinColumn(name = "CostCenterNumber", referencedColumnName="dennskdnr")
    private MasterData masterData;

}

@Entity
@Table(name = "MasterData")
public class MasterData {

    @OneToOne(optional = true, mappedBy = "masterData")
    private Store store;

}

Remember: in a bidirectional association, there is always an owner side, which defines how the association is mapped, and an inverse side, which uses the must use the mappedBy attribute to say: I'm the inverse side, look at the "masterData" attribute in the other entity to know how this association is mapped.