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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top