Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用CriteriaAPI构建一个动态查询,其中添加到日期的天数,并将该日期与另一个日期进行比较?_Java_Hibernate_Jpa_Criteria Api - Fatal编程技术网

Java 如何使用CriteriaAPI构建一个动态查询,其中添加到日期的天数,并将该日期与另一个日期进行比较?

Java 如何使用CriteriaAPI构建一个动态查询,其中添加到日期的天数,并将该日期与另一个日期进行比较?,java,hibernate,jpa,criteria-api,Java,Hibernate,Jpa,Criteria Api,我有一张a桌和一张B桌。JPA实体,A和B类分别具有joda.time.datetime持久字段表示retentionDate和lastmodifiedDate。A还有一个类型为int的持久字段,表示天。现在我想将a.days的数量添加到a.retentionDate中,然后使用JPA标准API将其与b.lastmodifiedDate进行比较 实体类 @Entity @Table(name = "A") @Data @JsonInclude(JsonInclude.Include.NON_NU

我有一张a桌和一张B桌。
JPA实体
,A和B类分别具有
joda.time.datetime
持久字段
表示
retentionDate
lastmodifiedDate
。A还有一个类型为
int
持久字段
,表示
。现在我想
将a.days
的数量添加到
a.retentionDate
中,然后使用
JPA标准API将其与
b.lastmodifiedDate
进行比较

实体类

@Entity
@Table(name = "A")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode(of = "id", callSuper = false)
public class A extends AbstractBaseEntity {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(unique = true, length = 36)
    private String id;

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @JsonDeserialize(using = DateTimeDeserializer.class)
    @JsonSerialize(using = DateTimeSerializer.class)
    private DateTime retentionDate;

    private int days;
}
@Entity
@Table(name = "B")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode(of = "id", callSuper = false)
public class B extends AbstractBaseEntity {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(unique = true, length = 36)
    private String id;

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @JsonDeserialize(using = DateTimeDeserializer.class)
    @JsonSerialize(using = DateTimeSerializer.class)
    private DateTime lastmodifiedDate ;

   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "A_ID")
   private A a;
}
B实体类

@Entity
@Table(name = "A")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode(of = "id", callSuper = false)
public class A extends AbstractBaseEntity {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(unique = true, length = 36)
    private String id;

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @JsonDeserialize(using = DateTimeDeserializer.class)
    @JsonSerialize(using = DateTimeSerializer.class)
    private DateTime retentionDate;

    private int days;
}
@Entity
@Table(name = "B")
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@EqualsAndHashCode(of = "id", callSuper = false)
public class B extends AbstractBaseEntity {
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(unique = true, length = 36)
    private String id;

    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    @JsonDeserialize(using = DateTimeDeserializer.class)
    @JsonSerialize(using = DateTimeSerializer.class)
    private DateTime lastmodifiedDate ;

   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "A_ID")
   private A a;
}
JPA标准查询:

CriteriaQuery<B> b = builder.createQuery(B.class);
Root<B> fromB = criteria.from(B.class); 
Expression<DateTime> lastmodifiedDate = fromB.get(B_.lastmodifiedDate);
Expression<DateTime> retentionDate = fromB.get(B_.a).get("retentionDate");
Expression<int> days = fromB.get(B_.a).get("days");
Expression<DateTime> newDate = // how to add days in retentionDate here so it became a newDate?
b.select(fromB);
b.where(builder.greaterThanOrEqualTo(newDate , lastmodifiedDate)
);
CriteriaQuery b=builder.createQuery(b.class);
根fromB=标准from(B.class);
表达式lastmodifiedDate=fromB.get(B_u.lastmodifiedDate);
表达式retentionDate=fromB.get(B_u.a.get)(“retentionDate”);
表达式days=fromB.get(B_.a).get(“days”);
表达式newDate=//如何在retentionDate中添加天数,使其成为newDate?
b、 选择(fromB);
b、 其中(builder.greaterThanOrEqualTo(newDate,lastmodifiedDate)
);
请在上述查询中帮助我在retentionDate中添加天数。谢谢。

您可以使用执行数据库功能将
天数添加到
保留日期中。
我已经用做了一个例子,因为我的测试项目运行H2数据库

    ParameterExpression<String> dayUnit = builder.parameter(String.class, "dayUnit");
    Expression<DateTime> newDate = builder.function("DATEADD", DateTime.class, dayUnit, days, retentionDate);