我有一个品牌实体的模型映射:
@Entity
public class Brand implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private BrandPk id;
//...
}
Run Code Online (Sandbox Code Playgroud)
复合键为:
@Embeddable
public class BrandPk implements Serializable {
private static final long serialVersionUID = 1L;
private int id1;
private int id2;
//...
}
Run Code Online (Sandbox Code Playgroud)
现在我想加入一个产品实体(一个品牌,多种产品):
我会有:
@Entity
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToOne
// ???
private Brand brand;
//...
}
Run Code Online (Sandbox Code Playgroud)
我需要什么才能正确连接我的表实体?
table_brands有一个由两个字段组成的 PK:id1 和 id2 table_products有一个带有 id 的 PK,以及一个仅引用 id1 的字段id_brand 。
id2 已经不再使用并且根本不重要!
该映射适用于旧版数据库,遗憾的是我无法更改该映射,因此我需要加入“忽略”id2。我怎么能够?
如果您添加另一列(例如 id_brand2 引用 id2),您可以尝试以下操作:
@ManyToOne
@JoinColumns({
@JoinColumn(name="id_brand", referencedColumnName="id1"),
@JoinColumn(name="id_brand2", referencedColumnName="id2")
})
private Brand brand;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11197 次 |
| 最近记录: |