Hibernate从一对多关系中选择记录不起作用

时间:2019-04-24 13:09:09

标签: java hibernate

Am根据某些条件及其工作状况选择记录。请在下面找到查询,该查询按预期运行。

Select req1 from UserRequest as req1 where and req1.fullyApproved = true and  req1.createdAt=(select max(createdAt) from UserRequest req2 where req1.userRequestName=req2.userRequestName)

请找到UserRequest表的模型类。

@Getter
@Setter
@Entity
@Table(name = "user_requests")
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserRequest implements Identifiable<Long>, LazilyLoadable {

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

    @Column(name = "created_at")
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    private DateTime createdAt;

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

    @Column(name="fully_approved")
    private boolean fullyApproved;

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


    @Transient
    @JsonProperty
    private User user;
}   


Please find the model class for User

@Getter
@Setter
@Entity
@Table(name = "users")
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode(of = {"username"})
@ValidDsdPermissions
@ValidApprovalPrivileges
public class User implements Identifiable<Long>, LazilyLoadable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @JsonProperty("userLink")
  private Long id;

  @NotEmpty(message = "{error.model.user.attribute.username.required}")
  @Column(unique = true, nullable = false)
  private String full_name;


  @NotEmpty(message = "{error.model.user.attribute.email.required}")
  @Email(message = "{error.model.user.attribute.email.format.invalid}")
  @Column(nullable = false)
  private String email;

@Column(name = "is_active")
private boolean active = true;


  }

这两个模型类full_namesupervisor_name是相同的。

在选择记录之前,我在这里还添加了一个条件。 (u.active = true)

(u是User表的对象)。此查询无法正常工作-u.active=true不令人满意。

Select req1 from User u, UserRequest as req1 where u.active=true and req1.fullyApproved = true and  req1.createdAt=(select max(createdAt) from UserRequest req2 where req1.userRequestName=req2.userRequestName)

如何修改此查询??

0 个答案:

没有答案