我想查询联接表中的属性。创建的规范基于基于用户请求创建的动态查询。
我正在根据下面的文章使用动态规范创建。 https://www.baeldung.com/rest-api-query-search-language-more-operations
当我查询主要元素时,查询工作正常。我想使用动态查询来查询联接表中的元素。我收到错误消息“无法找到属性”
@Entity
@Table(name = "user")
public class UserEntity {
@Getter @Setter
@Id
@Column(name = "id")
private String id;
@Getter @Setter
@Column(name = "firstName")
private String firstName;
@Setter @Getter
@OneToOne(fetch = FetchType.EAGER, optional = true, cascade=CascadeType.ALL)
@JoinColumn(name = "deviceId", nullable = false)
private DeviceEntity device;
}
@Repository
public interface UserRepository extends CrudRepository<UserEntity, String>, JpaSpecificationExecutor<UserEntity> {
}
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "device")
public class DeviceEntity {
@Id
@Column(name = "id")
private String id;
@Column(name = "serialNumber", nullable = false)
private String serialNumber;
}
答案 0 :(得分:0)
您加入即可加入DeviceEntity
this.service.editOneProduit(this.idProduit).subscribe((data: Produit) => {
答案 1 :(得分:0)
我用了下面的那个。根将始终指向主实体,对于复合实体,我需要获取其路径并添加元素。我正在使用映射器来映射实体以进行静态映射。
protected void loadSearchParameterMappings() {
getSearchParameterMappings().put("id", "id");
getSearchParameterMappings().put("firstName", "firstName");
getSearchParameterMappings().put("lastName", "lastName");
getSearchParameterMappings().put("email", "email");
getSearchParameterMappings().put("serialNumber", "device.serialNumber");
getSearchParameterMappings().put("phoneNumber", "device.phoneNumber");
}
private Path<String> getParameterPath(final Root<T> root) {
String[] keys = criteria.getKey().split("\\.");
Path<String> path = null;
for (String key : keys) {
path = (path != null) ? path.get(key) : root.get(key);
// verify all the mappings are done correctly or this mapping is not supported
if (path == null) {
throw new
InvalidSearchParameterException(ErrorCode.INVALID_SEARCH_PARAM, key);
}
}
if (path == null) {
throw new InvalidSearchParameterException(ErrorCode.INVALID_SEARCH_PARAM, criteria.getKey());
}
return path;
}