Table Name for @Embeddable



我有一个类如下

@Entity
@Table(name = "template")
public class DynamicTemplate {
    @Id
    private Long id;
 
    @ElementCollection
    Set<TemplateAttributes> attributes;
    //setters and getters

下面的另一个表
@Table(name = "attribute")
@Embeddable
public class TemplateAttributes {
    private String name;
    private Double normalLow;

在数据库中@Embeddable表正在创建template_attributes属性

您需要使用CollectionTable-Annotation:

@Entity
@Table(name = "template")
public class DynamicTemplate {
    @Id
    private Long id;
    @ElementCollection
    @CollectionTable(name = "attribute")
    Set<TemplateAttributes> attributes;
   
    ...
}

@Embeddable
public class TemplateAttributes {
    private String name;
    private Double normalLow;
    ...
}

您的模式没有多大意义。

@Embeddable标记该对象不被映射为一个表,而是一组嵌入到另一个实体/表中的属性(实际上,您可以将它用于不同实体中的几个属性)。

如果DynamicTemplate有一个TemplateAttributes属性;属性,那么在表模板中将有namenormalLow列。但是,不能将一组值作为同一表中的列。

你需要的是使TemplateAttributes成为一个实体,或者,如果你需要重用TemplateAttributes,创建一个具有id和TemplateAttributes属性的新实体,并拥有一组它们。

最新更新