我有三个共享相同主键的实体。 帐户和提供商实体之间存在双向一对一关系,其中先前命名的实体共享其主键,但是同时提供商实体与 GlobalRating 实体共享主键,其中它们之间存在单向关系。我必须说我正在使用hibernate和MySQL作为数据库:
帐户实体:
@Entity
@Table(name="account")
@Getter
@Setter
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "email")
public class Account implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.AUTO, generator="native")
@GenericGenerator(name = "native", strategy = "native")
@Column(name="id")
private int id;
@OneToOne(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Provider provider;
}
提供商实体:
@Entity
@Table(name="provider")
@Getter
@Setter
@JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "providerId")
public class Provider implements Serializable, Comparable<Provider>{
private static final long serialVersionUID = 1L;
@Id
@Column(name="pk_account$provider")
@GeneratedValue(generator="native")
@GenericGenerator(name="native", strategy="foreign",parameters=@Parameter(name="property", value="account"))
private int providerId;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private Account account;
@OneToOne(fetch=FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
@PrimaryKeyJoinColumn
private GlobalRating globalRating;
}
GlobalRating实体:
@Entity
@Table(name="global_rating")
@Getter
@Setter
public class GlobalRating implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name="pk_provider$global_rating")
@GeneratedValue(generator="native")
@GenericGenerator(name="native", strategy="foreign",parameters=@Parameter(name="property", value="globalRating"))
private int globalRatingId;
}
我遇到的问题是使用提供商和 GlobalRating 创建帐户时。我确信帐户和提供商之间的双向映射有效,但问题在于实体提供商和实体 GlobalRating 即可。我已经尝试了几种方法,但它不起作用。那么,这种情况如何在hibernate中正确映射?
我很感激你可以帮助我。