【问题标题】:Attribute based Query using Spring Data JPA使用 Spring Data JPA 的基于属性的查询
【发布时间】:2016-08-21 09:12:14
【问题描述】:

我需要有一个带有 System_id、名称、描述等的 Registered_Systems 表。现在每个系统可以有 n 个属性,我计划将这些属性存储在子表 Registered_System_Attributes(System_id,Attribute_name, Attribute_Value) 中,并带有外键到 Registered_Systems.System_id

由于属性必须与系统关联,我打算将类 RegisteredSystemAttribute 设为Embeddable 并定义

@Entity
@Table(name = "Registered_Systems")
public class RegisteredSystem {
...

 @ElementCollection
 @CollectionTable(name = "Registered_System_Attributes", joinColumns = { @JoinColumn(name = "System_id", referencedColumnName = "System_id") })
 private Set<RegisteredSystemAttribute> registeredSystemAttributes;
 ...
}

如果以这种方式建模,如何根据属性名称和提供的值返回系统名称?

非常欢迎对此提供任何帮助。

我正在使用

  • spring-data-commons-1.6.3.RELEASE.jar
  • spring-data-jpa-1.4.3.RELEASE.jar
  • (i)hibernate-entitymanager-4.2.14.SP1-redhat-1.jar,(ii) JBOSS EAP 6.3 提供的hibernate-jpa-2.0-api-1.0.1.Final-redhat-2.jar

【问题讨论】:

标签: jpa spring-data-jpa jpql


【解决方案1】:

我认为您正在寻找的是 JPA 2.1 地图集合(JSR 388,第 2.7 节)。所以在你的情况下:

@ElementCollection
@CollectionTable(name="Registered_System_Attributes")
@MapKeyColumn(name="Attribute_name")
@Column(name="Attribute_Value")
private Map<String, String> registeredSystemAttributes;

【讨论】:

  • 感谢您的回答。我已经开始阅读 MapKeyColumn。一个直接的问题:JPQL 会根据属性名称和提供的值返回系统名称吗?
  • 另外根据你的回答,我的实际要求是私有 Map> registeredSystemAttributes;
猜你喜欢
  • 1970-01-01
  • 2018-11-03
  • 2016-05-08
  • 2021-11-22
  • 1970-01-01
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 2019-02-28
相关资源
最近更新 更多