JPA 实体,如何连接表
JPA Entitties, how to join tables
我有三张表
CREATE TABLE "ingredient" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY,
"ingredient" VARCHAR(50) NOT NULL
);
CREATE TABLE "pizza" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY,
"pizza" VARCHAR(50) NOT NULL
);
CREATE TABLE "pizza_structure" (
"pizza_id" INT NOT NULL,
"ingredient_id" INT NOT NULL,
"amount" INT NOT NULL
);
如何加入它们,将 Pizzas 结构作为 Map
@Entity
@Table(name = "ingredient")
public class Ingredient{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public Ingredient() {
}
}
@Entity
@Table(name = "pizza")
public class Pizza {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany ????
private Map<Ingredient, Integer> pizzaStructure;
public Pizza() {
}
public Pizza(String name, Map<Long, Integer> pizzaStructure) {
this.name = name;
this.pizzaStructure = pizzaStructure;
}
}
我需要创建@Embeddable class PizzaStructure,如果需要,什么时候使用它?
现在我收到一个错误
init 方法调用失败;嵌套异常是 org.hibernate.AnnotationException:使用 @OneToMany 或 @ManyToMany 定位未映射的 class:
你在比萨饼和配料之间有一个多对多的关系,并且在你的关系中有一个额外的列。
我在这里发现了一个类似的问题:JPA 2.0 many-to-many with extra column
(我会评论,但我没有足够的声誉。)
how to join them, to get Pizzas structure as a Map
看起来像这样:
@ElementCollection
@CollectionTable(name = "pizza_structure", joinColumns = {@JoinColumn(name = "pizza_id")})
@Column(name = "amount")
@MapKeyJoinColumn(name = "ingredient_id")
private Map<Ingredient, Integer> pizzaStructure;
do I need to create @Embeddable class PizzaStructure
没有
更多信息在这里:Hibernate User Guide - Maps。
注意 table pizza_structure
应该有 pizza
和 ingredient
table 的外键以及 pizza_id
和 pizza_id
的唯一约束ingredient_id
,像这样(这是 postgresql 方言):
create table pizza_structure
(
pizza_id ... constraint fk_structure_pizza references pizza,
ingredient_id ... constraint fk_structure_ingredient references ingredient,
amount ...,
constraint pizza_structure_pkey primary key (pizza_id, ingredient_id)
);
我有三张表
CREATE TABLE "ingredient" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY,
"ingredient" VARCHAR(50) NOT NULL
);
CREATE TABLE "pizza" (
"id" INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY,
"pizza" VARCHAR(50) NOT NULL
);
CREATE TABLE "pizza_structure" (
"pizza_id" INT NOT NULL,
"ingredient_id" INT NOT NULL,
"amount" INT NOT NULL
);
如何加入它们,将 Pizzas 结构作为 Map
@Entity
@Table(name = "ingredient")
public class Ingredient{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public Ingredient() {
}
}
@Entity
@Table(name = "pizza")
public class Pizza {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany ????
private Map<Ingredient, Integer> pizzaStructure;
public Pizza() {
}
public Pizza(String name, Map<Long, Integer> pizzaStructure) {
this.name = name;
this.pizzaStructure = pizzaStructure;
}
}
我需要创建@Embeddable class PizzaStructure,如果需要,什么时候使用它?
现在我收到一个错误 init 方法调用失败;嵌套异常是 org.hibernate.AnnotationException:使用 @OneToMany 或 @ManyToMany 定位未映射的 class:
你在比萨饼和配料之间有一个多对多的关系,并且在你的关系中有一个额外的列。 我在这里发现了一个类似的问题:JPA 2.0 many-to-many with extra column (我会评论,但我没有足够的声誉。)
how to join them, to get Pizzas structure as a Map
看起来像这样:
@ElementCollection
@CollectionTable(name = "pizza_structure", joinColumns = {@JoinColumn(name = "pizza_id")})
@Column(name = "amount")
@MapKeyJoinColumn(name = "ingredient_id")
private Map<Ingredient, Integer> pizzaStructure;
do I need to create @Embeddable class PizzaStructure
没有
更多信息在这里:Hibernate User Guide - Maps。
注意 table pizza_structure
应该有 pizza
和 ingredient
table 的外键以及 pizza_id
和 pizza_id
的唯一约束ingredient_id
,像这样(这是 postgresql 方言):
create table pizza_structure
(
pizza_id ... constraint fk_structure_pizza references pizza,
ingredient_id ... constraint fk_structure_ingredient references ingredient,
amount ...,
constraint pizza_structure_pkey primary key (pizza_id, ingredient_id)
);