我有一个类似Process的实体,它将由创建,并由一个用户更新。当我尝试应用过滤器时。我已经在数据库中创建了外键关系。现在,当我使用JPA规范来应用动态过滤器时,我得到了的异常
No property CREATED found for type Process!
@Table(name = "process")
@Entity
public class Process {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PROCESS_ID")
@JsonProperty("id")
private Long id = null;
@NotNull
@Column(name = "NAME")
@JsonProperty("name")
private String name = null;
@Column(name = "CREATED_BY", updatable = false)
@JsonProperty("createdBy")
private Long createdBy = null;
@Column(name = "updatedBy", nullable = true)
@JsonProperty("updatedBy")
private Long updatedBy = null;
}
因此,我在流程实体中添加了实体关系映射,如下所示,
现在,我得到以下错误。我是JPA和hibernate的新手,关系映射非常混乱,请帮忙。
@Table(name = "process")
@Entity
public class Process {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PROCESS_ID")
@JsonProperty("id")
private Long id = null;
@NotNull
@Column(name = "NAME")
@JsonProperty("name")
private String name = null;
@Column(name = "CREATED_BY", updatable = false)
@JsonProperty("createdBy")
private Long createdBy = null;
@Column(name = "updatedBy", nullable = true)
@JsonProperty("updatedBy")
private Long updatedBy = null;
//newly added below properties so that there will be no error when fetching data
@OneToOne(targetEntity = UserDetails.class, fetch = FetchType.LAZY, mappedBy = "id")
private UserDetails CREATED;
@OneToOne(targetEntity = UserDetails.class, fetch = FetchType.LAZY, mappedBy = "id")
private UserDetails UPDATED;
}
现在,我得到以下错误Referenced property not a (One|Many)ToOne: com.app.users.details.domain.UserDetails.id in mappedBy of com.app.scenarios.domain.Process.CREATED
请告诉我我做错了什么。我有一个流程,可以由用户创建,也可以由用户更新。在DB中,我有一个进程和userdetails实体的外键关系。
编辑
使用JPA规范从DB获取过滤数据的代码
Page<process> result = this.processDao.findAll(getprocessGridData(processSearchCondition.getprocessName()), pageRequest);
private static Specification<process> getprocessGridData(String processName) {
return (Specification<process>) (root, query, criteriaBuilder) -> (
criteriaBuilder.like(root.get("name"), processName)
);
}
我想你真正想要的是:
@Table(name = "process")
@Entity
public class Process {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PROCESS_ID")
@JsonProperty("id")
private Long id;
@NotNull
@Column(name = "NAME")
@JsonProperty("name")
private String name;
@OneToOne(fetch = FetchType.LAZY)
@jOINColumn(name = "CREATED_BY", updatable = false)
private UserDetails createdBy;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "UPDATED_BY", nullable = true)
private UserDetails updatedBy;
}