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
}
Main.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 クラスです。画面は検索画面で、検索をフィルタリングまたは絞り込むことができる他の e コマース Web サイトと同じように、検索を絞り込みます。ユーザーの選択に基づいて、データベースはデータベースからデータを取得します。このために、休止状態で例によってクエリを実装しました。ただし、基準に複数の例があると、例外がスローされます。