1

SchoolViewModel.java

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

    //getter and setter
}

InstituteType.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 
}

Board.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
}

SchoolDetail.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
}

主.java

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();
            }
    }
}

例外是:

Exception in thread "main" org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.util.Set<com.mypack.model.Board> com.mypack.model.SchoolDetail.board] by reflection for persistent property [com.mypack.model.SchoolDetail#board] : com.mypack.model.InstituteType@f555e7
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:71)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValues(AbstractEntityTuplizer.java:506)
    at org.hibernate.tuple.entity.PojoEntityTuplizer.getPropertyValues(PojoEntityTuplizer.java:215)
    at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValues(AbstractEntityPersister.java:4626)
    at org.hibernate.criterion.Example.toSqlString(Example.java:190)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:400)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:106)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:75)
    at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:80)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1849)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:365)
    at com.mypack.dao.SchoolBoard.main(SchoolBoard.java:48)
Caused by: java.lang.IllegalArgumentException: Can not set java.util.Set field com.mypack.model.SchoolDetail.board to com.mypack.model.InstituteType
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:387)
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:67)
    ... 11 more

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

4

0 回答 0