依赖 class 的 Hibernate CreateCriteria

Hibernate CreateCriteria for dependent class

我正在开发一个必须显示 data/information 车辆详细信息的应用程序。我有 2 tables makemodel,其中 make_idmodel table 中的外键。我有 2 个实体 类 MakeModel,如下所示:

@Entity
@Table(name = "make")
@PrimaryKeyJoinColumn(name="make_id")
public class Make {
    @Id
    @Column(name = "make_id")
    private String makeId;

    @Column(name = "make_name")
    private String makeName;

    @ManyToOne
    @JoinColumn(name = "mfg_unit_id")
    private MfgUnit mfgUnit;

    // Getter and Setters
}


@Entity
@Table(name = "model")
public class Model {
    @Id
    @Column(name = "model_id")
    private String modelId;

    @Column(name = "model_creation_date")
    private Date modelCreationDate;

    @Column(name = "make_id")
    private long makeId;

    @ManyToOne
    @JoinColumn(name = "make_id")
    private Make make;

    // Getter and Setters
}

我能够检索所有 Make,但我的要求是仅检索 model_creation_date 在今天和过去 30 天之间的 Make。谁能帮助我如何为此建立 Hibernate 标准?

抱歉,我不知道您为什么将日期声明为字符串:

私有 String modelCreationDate;

无论如何,我假设您会将 modelCreationDate 声明为 Date/LocalDate 或类似的类型。我还没有测试代码,但我希望下面的代码能解决您的问题。祝你好运!

Calendar cal = Calendar.getInstance();
// today 
Date toDate=cal.getTime(); 
cal.add( Calendar.DATE, -30 );
//date before 30 days
Date fromDate=cal.getTime();


Criteria criteria = session.createCriteria(Model.class , "s")
        .createAlias("make", "m")
        .add(Restrictions.between("s.modelCreationDate",  fromDate, toDate))
        // if the above condition dosen't give you exact result you may try following(2)
        // try gt or lt ... as needed 
        //.add(Restrictions.ge("publicationDate", fromDate)) 
        //.add(Restrictions.le("publicationDate", fromDate))
        .setProjection(
                Projections.projectionList()
                .add(Projections.groupProperty("m.makeId").as("makeId"))
                .add(Projections.property("m.makeName").as("makeName")) 
            )
        .setResultTransformer(Transformers.aliasToBean(Make.class));


List<Make> ll2 = criteria.list();

可以有另一种解决方案,但要实现这一点,您需要修改 Make class,如下所示,引入与模型 class 的新关系 (@OneToMany):

@Entity
@Table(name = "make")
@PrimaryKeyJoinColumn(name="make_id")
public class Make {
    @Id
    @Column(name = "make_id")
    private String makeId;

    @Column(name = "make_name")
    private String makeName;

    // ** introduced new relationship **
    @OneToMany(mappedBy="make", fetch = FetchType.LAZY)
    private List<Model> models;

    @ManyToOne
    @JoinColumn(name = "mfg_unit_id")
    private MfgUnit mfgUnit;

    // Getter and Setters
}

并保持 'Model' class 原样:

@Entity
@Table(name = "model")
public class Model {
    @Id
    @Column(name = "model_id")
    private String modelId;

    // change the datatype as Date or similar Datatype
    @Column(name = "model_creation_date")
    private Date modelCreationDate;

    @Column(name = "make_id")
    private long makeId;

    @ManyToOne
    @JoinColumn(name = "make_id")
    private Make make;

    // Getter and Setters
}

从 'Make' 与 'Model' 建立关系后,您可以执行以下代码来获取结果:

Calendar cal = Calendar.getInstance();
// today 
Date toDate=cal.getTime(); 
cal.add( Calendar.DATE, -30 );
//date before 30 days
Date fromDate=cal.getTime();


Criteria criteria = session.createCriteria(Make.class , "m")
        .createAlias("models", "s")
        .add(Restrictions.between("s.modelCreationDate",  fromDate, toDate))
        // if the above condition dosen't give you exact result you may try following(2)
        // try gt or lt ... as needed 
        //.add(Restrictions.ge("publicationDate", fromDate)) 
        //.add(Restrictions.le("publicationDate", fromDate))
        .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

List<Make> ll2 = criteria.list();

除了 ,我还可以使用查询检索记录

Calendar cal = Calendar.getInstance();
Date toDate = cal.getTime();
cal.add(Calendar.DATE, -30);
Date fromDate = cal.getTime();

String hql = "SELECT make FROM Model m where m.modelCreationDate between :from_date and :to_date";
Query query = session.createQuery(hql);
query.setParameter("from_date", fromDate);
query.setParameter("to_date", toDate);

List results = query.list(); // Result list