如何在JPA Spring

时间:2017-10-19 15:04:40

标签: java spring jpa

我有4个模特:

Blueprint,描述了单一类型的项目:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name="name")
private String name;

@ElementCollection
@CollectionTable(name = "BLUEPRINT_TAG", joinColumns = @JoinColumn(name = "blueprint"))
private List<String> tags;

@OneToMany(mappedBy = "blueprint", cascade = CascadeType.ALL, orphanRemoval = true)
private List<BlueprintProperty> properties;

@OneToMany(mappedBy = "blueprint", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnore
private List<Item> items;

@Column(name = "width")
private Float width;

@Column(name = "height")
private Float height;

@Column(name = "imagePath")
private String imagePath;

@Column(name = "zPosition")
private long zPosition;

BlueprintProperty,描述了可以为每种类型的项目输入的信息。

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "blueprint")
@JsonIgnore
private Blueprint blueprint;

@Column(name = "name")
private String name;

@Enumerated(EnumType.STRING)
private PropertyType type;

@Column(name = "data")
@Convert(converter = JpaJsonConverter.class)
private ObjectNode data;

@OneToMany(mappedBy = "property", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnore
private List<ItemProperty> itemProperties;

然后有项目:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "parent")
//@Nullable
private Item parent;

@Column(name = "pos_x")
private Float posX;

@Column(name = "pos_y")
private Float posY;

@Column(name = "transformation")
private String transformation;

@ManyToOne
@JoinColumn(name = "blueprint")
private Blueprint blueprint;

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
@JsonIgnore
private List<Item> children;

@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ItemProperty> properties;

最后,ItemProperty,它包含引用特定属性所属的Item的外键,以及它描述的BlueprintProperty:

@Id
@ManyToOne
@JoinColumn(name = "item")
@JsonIgnore
private Item item;

@Id
@ManyToOne
@JoinColumn(name = "property")
@JsonIgnore
private BlueprintProperty property;

@Column(name = "value")
private String value;

现在,我对这个设置并不完全满意,但这是我必须要做的事情,除非完全有必要,否则我不应该做任何改变。我尝试做的是创建一个Query,它接受许多Item属性并返回所有与查询匹配的属性。

使用Query by example听起来很有希望,但我发现only SingularAttribute properties can currently be used for property matching.。因此杀死了这种方法,并且我认为我不能使用本机查询,因为某个Item可以有N个属性,并且您应该能够输入一个查询,该查询按单个属性搜索,最多N个属性。

有人可以建议一种方法来执行我需要的搜索并获得一个匹配项目的列表,其属性与输入的结果相匹配吗?

0 个答案:

没有答案