在条件中添加多个示例(Hibernate)

时间:2017-02-17 16:36:15

标签: java hibernate criteria hibernate-criteria query-by-example

SchoolViewModel.java

rect

InstituteType.java

public class SchoolViewModel {
    private String state;
    private String city;
    private String instituteType;

    //getter and setter
}

Board.java

@Entity
@Table(name="REF_INSTITUTE_TYPE")
public class InstituteType implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name="ID")
    private Integer id;

    @Column(name="INSTITUTE_TYPE")
    private String instituteType;

    public Integer getId() {
        return id;
    }

    @ManyToMany(mappedBy="instituteType")
    private Set<SchoolDetail> schoolDetail =new HashSet<SchoolDetail>();

    //getter and setter 
}

SchoolDetail.java

@Entity
@Table(name="REF_BOARD")
public class Board implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name="ID")
    private Integer id;

    @Column(name="BAORD")
    private String board;

    @ManyToMany(mappedBy="board")
    private Set<SchoolDetail> schoolDetail =new HashSet<SchoolDetail>();

    //getter and setter
}

Main.java

@Entity
@Table(name="SCHOOL_DETAIL")
public class SchoolDetail implements Serializable{
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name="id")
    private int id;
    @Column(name="name")
    private String name;

    @Column(name="state")
    private String state;
    @Column(name="city")
    private String city;


    @ManyToMany
    @JoinTable(name="SCHOOL_INSTITUTE_TYPE",
               joinColumns={@JoinColumn(name="SCHOOL_ID")},
               inverseJoinColumns={@JoinColumn(name="INSTITUTE_TYPE_ID")}
              )
    private Set<InstituteType> instituteType=new HashSet<InstituteType>();

    @ManyToMany
    @JoinTable(name="SCHOOL_BOARD",
               joinColumns={@JoinColumn(name="SCHOOL_ID")},
               inverseJoinColumns={@JoinColumn(name="BOARD_ID")}
            )
    private Set<Board> board=new HashSet<Board>();

//getter and setter
}

例外是:

public class SchoolBoard {
    public static void main(String[] args) {
        Configuration cfg=new Configuration();
        cfg.configure("hibernate.cfg.xml");
        SessionFactory sessionFactory=cfg.buildSessionFactory();
        Session session=sessionFactory.openSession();

        try{
            SchoolViewModel svm=new SchoolViewModel();
            svm.setState("Maharastra");
            svm.setCity("Pune");
            svm.setInstituteType("college");

            SchoolDetail school=new SchoolDetail();
            school.setState(svm.getState());
            school.setCity(svm.getCity());
            InstituteType instituteType=new InstituteType();
            instituteType.setInstituteType(svm.getInstituteType()); 
            Board board=new Board();
            board.setBoard("STATE");
            Example schoolExample=Example.create(school).ignoreCase().enableLike().excludeZeroes();
            Example instituteTypeExample=Example.create(instituteType).ignoreCase().enableLike().excludeZeroes();
            Example boardExample=Example.create(board).ignoreCase().enableLike().excludeZeroes();
            Criteria criteria=session.createCriteria(SchoolDetail.class,"schoolDetail");
            criteria.createAlias("schoolDetail.instituteType", "instituteType");
            criteria.createAlias("schoolDetail.board", "board");
            criteria.add(schoolExample);
            criteria.add(instituteTypeExample);
            criteria.add(boardExample);

Line - 48   List<SchoolDetail> schoolList=criteria.list();

        }finally{
            session.close();
            sessionFactory.close();
            }
    }
}

描述:SchoolViewModel是pojo类,它将从前端传输数据。屏幕是搜索屏幕,包含优化您的搜索,就像任何可以过滤或缩小搜索范围的电子商务网站一样。基于用户选择数据库将从数据库中检索数据。为此,我在hibernate中通过示例实现查询。但是,虽然标准中的多个示例会引发异常。

0 个答案:

没有答案