Sql 来自具有非唯一id的表的JPA实体
我想要一个实体Sql 来自具有非唯一id的表的JPA实体,sql,hibernate,jpa,dictionary,orm,Sql,Hibernate,Jpa,Dictionary,Orm,我想要一个实体 class InkFormula { int id; Map<Ink, Integer> inkPercents; ... } 其中必须使用非唯一的id从ink\u id和百分比聚集Map 我需要一个实体和一个表的原因是需要有唯一的约束id,ink\u id,这样每个公式就不能包含重复的墨水 JPA和Hibernate可以吗?我将创建一个复合主键InkFormulaId,其中包含两个字段:id和ink\u id,类似于 @Em
class InkFormula {
int id;
Map<Ink, Integer> inkPercents;
...
}
其中必须使用非唯一的id
从ink\u id
和百分比聚集Map
我需要一个实体和一个表的原因是需要有唯一的约束id
,ink\u id
,这样每个公式就不能包含重复的墨水
JPA和Hibernate可以吗?我将创建一个复合主键InkFormulaId
,其中包含两个字段:id
和ink\u id
,类似于
@Embeddable
public class InkFormulaId implements Serializable {
private Integer id;
private Integer inkId;
// ...
}
@Entity
public class InkFormula {
@EmbeddedId
private InkFormulaId inkFormulaId;
@ManyToOne
@MapsId(value = "inkId")
private Ink ink;
private Integer percents;
// ...
}
因此,每个InkFormula
表示上表中的一行,带有唯一的主键(id
,inkId
)及其百分比
要获得上面提到的映射,您必须为单个id
选择allInkFormula
,然后在Java代码中按inkId
对它们进行分组。我将创建一个复合主键InkFormulaId
,其中包含两个字段:id
和ink\u id
,类似于
@Embeddable
public class InkFormulaId implements Serializable {
private Integer id;
private Integer inkId;
// ...
}
@Entity
public class InkFormula {
@EmbeddedId
private InkFormulaId inkFormulaId;
@ManyToOne
@MapsId(value = "inkId")
private Ink ink;
private Integer percents;
// ...
}
因此,每个InkFormula
表示上表中的一行,带有唯一的主键(id
,inkId
)及其百分比
要获得上面提到的映射,您必须为单个id
选择allInkFormula
,然后在Java代码中按inkId
对它们进行分组。另一种解决方案可以是使用@ElementCollection
注释并将数据分为两个表:
ink_formula ink_formula_ink_percent
┌───┐ ┌──────────────┬──────┬───────┐
│id │ │ink_formula_id│ink_id│percent│
├───┤ ├──────────────┼──────┼───────┤
│ 1 │ │ 1 │ 1 │ 20 │
├───┤ ├──────────────┼──────┼───────┤
│ 2 │ │ 1 │ 2 │ 55 │
├───┤ ├──────────────┼──────┼───────┤
. . . . . . . . . . . . . . . . . . .
实体类将如下所示
@Entity
public class InkFormula {
@Id
@GeneratedValue
int id;
@ElementCollection
@CollectionTable(
name="ink_formula_ink_percent",
joinColumns=@JoinColumn(name="ink_formula_id")
)
@MapKeyJoinColumn(name="ink_id")
@Column(name = "percent")
private Map<Ink, Integer> inkPercents = new TreeMap<>();
//...
}
@实体
公共类公式{
@身份证
@生成值
int-id;
@元素集合
@收集表(
name=“墨水\公式\墨水\百分比”,
joinColumns=@JoinColumn(name=“ink\u formula\u id”)
)
@MapKeyJoinColumn(name=“ink\u id”)
@列(name=“百分比”)
私有映射inkPercents=新树映射();
//...
}
此设置满足唯一的InkFormula墨水
要求,但我们需要为InkFormula
提供一个单独的表
下一个问题是如何确保每个InkFormula
的唯一性,因为具有不同id
s的两个InkFormula
可以在其inkPercents
映射中保存相同的值。另一种解决方案是使用@ElementCollection
注释并将数据分离到两个表中:
ink_formula ink_formula_ink_percent
┌───┐ ┌──────────────┬──────┬───────┐
│id │ │ink_formula_id│ink_id│percent│
├───┤ ├──────────────┼──────┼───────┤
│ 1 │ │ 1 │ 1 │ 20 │
├───┤ ├──────────────┼──────┼───────┤
│ 2 │ │ 1 │ 2 │ 55 │
├───┤ ├──────────────┼──────┼───────┤
. . . . . . . . . . . . . . . . . . .
实体类将如下所示
@Entity
public class InkFormula {
@Id
@GeneratedValue
int id;
@ElementCollection
@CollectionTable(
name="ink_formula_ink_percent",
joinColumns=@JoinColumn(name="ink_formula_id")
)
@MapKeyJoinColumn(name="ink_id")
@Column(name = "percent")
private Map<Ink, Integer> inkPercents = new TreeMap<>();
//...
}
@实体
公共类公式{
@身份证
@生成值
int-id;
@元素集合
@收集表(
name=“墨水\公式\墨水\百分比”,
joinColumns=@JoinColumn(name=“ink\u formula\u id”)
)
@MapKeyJoinColumn(name=“ink\u id”)
@列(name=“百分比”)
私有映射inkPercents=新树映射();
//...
}
此设置满足唯一的InkFormula墨水
要求,但我们需要为InkFormula
提供一个单独的表
下一个问题是如何确保每个InkFormula
的唯一性,因为两个InkFormula
具有不同id
s的映射可以在其inkPercents
映射中保存相同的值。以及什么映射到“percent”?映射必须从“ink_id”(int)和“percent”(int)映射。你的意思是这是一个可合并的?尼尔,事实并非如此。我需要一个表。什么映射到“百分比”?映射必须从“ink_id”(int)和“percent”(int)映射。你的意思是这是一个JoinTable?Neil,不是。我需要一个表。我想要的是一个InkFormula
实体,它来自所有具有相同id
的行,而不是每一行。这是一个挑战。这是不可能的,因为一行有多个值会破坏“ACID”()中的“a”,每个JPA实体代表一行。好的,我们可以将InkFormula
的id
放入一个单独的ink\u公式
表中。然后将此表命名为ink\u formula\u percent
。但问题仍然存在:如何将ink\u公式
和ink\u公式
合并到一个实体中。为什么要使用另一个表?您尝试过我上面的示例吗?它会生成一个表,您只需手动将InkFormula
对象首先按id
分组,然后按inkId
分组即可。谢谢,Smutje,我尝试过了–效果很好。但是我太懒了,无法手动对地图进行分组。我想用Hibernate和数据库来完成。或者我不知道如何在没有太多工作的情况下做到这一点?请参阅下面我的备选解决方案。我想要的是一个来自所有具有相同id
的行的InkFormula
实体,而不是针对每一行。这是一个挑战。这是不可能的,因为一行有多个值会破坏“ACID”()中的“a”,每个JPA实体代表一行。好的,我们可以将InkFormula
的id
放入一个单独的ink\u公式
表中。然后将此表命名为ink\u formula\u percent
。但问题仍然存在:如何将ink\u公式
和ink\u公式
合并到一个实体中。为什么要使用另一个表?您尝试过我上面的示例吗?它会生成一个表,您只需手动将InkFormula
对象首先按id
分组,然后按inkId
分组即可。谢谢,Smutje,我尝试过了–效果很好。但是我太懒了,无法手动对地图进行分组。我想用Hibernate和数据库来完成。或者我不知道如何在没有太多工作的情况下做到这一点?请参阅下面的替代解决方案。