Home > Software engineering >  Hibernate Criteria Join with 2 Tables
Hibernate Criteria Join with 2 Tables

Time:10-03

I have two table one is Student and another one is Event. I am try to join two tables this is Student table

@Table(name="student")
public class Student implements Serializable {
        private static final long serialVersionUID = 1L;
    @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
 @Column(name="sid")
    private int sid;
    @Column(name="sname")
    private String sname;
     @Column(name="contactno")
    private int contactno;

this is the Event table

@Entity
@Table(name="event")

public class Event implements Serializable {
        private static final long serialVersionUID = 1L;
  @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
   @Column(name="id")
private int id;
   @Column(name="name")
  private String name;
    @Column(name="email")
  private String email;
     @Column(name="fee")
  private int fee;
         @Column(name="sid")
  private int sid;

    @ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.ALL )
    @JoinColumn(name = "sid", referencedColumnName = "sid", insertable = false, updatable = false)
  
   
@GeneratedValue(strategy = GenerationType.AUTO)
    private  Student stud

;

I am write the code for joining two tables in Dao

 public List display() {
     Session session = getSessionFactory().openSession();
        Criteria cr= session.createCriteria(Student.class,"student");   
       cr.createAlias("student.Event", "event");
        cr.setProjection ( Projections.distinct(Projections.projectionList()
                .add(Projections.property( "event.id"), "id")
                .add( Projections.property( " event.name"), "name")
                .add(Projections.property( "event.email"), "email")
                .add( Projections.property( "event.fee"), "fee")
                .add(Projections.property( "student.sname"), "sname")
                .add( Projections.property( "student.contactno"), "contactno")));
               cr.setResultTransformer((ResultTransformer) Transformers.ALIAS_TO_ENTITY_MAP);
               return cr.list();
    }

in controller

@RequestMapping(value="fetch.htm", method = RequestMethod.GET)
public @ResponseBody String display() 
{
           System.out.print("fetch the data from student controller \n");
    List<Event> listdetails = (List<Event>) stDao.display();
     System.out.print("fetch the data from student contoller listdetails \n" listdetails);
         return new Gson().toJson(listdetails);
         
}

when run the code get error HTTP Status 500 - Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: Event of: sample.entity.Student

CodePudding user response:

As you don't have a direct relationship between Student and Event. Instead, you are having a ManyToOne relationship between Event and Student. So you have to change your query to do join from event -> student.

public List display() {
    Session session = getSessionFactory().openSession();
    Criteria cr = session.createCriteria(Event.class, "event");
    cr.createAlias("event.student", "student"); // student is property name in Event class
    cr.setProjection(Projections.distinct(Projections.projectionList().add(Projections.property("event.id"), "id")
            .add(Projections.property("event.name"), "name").add(Projections.property("event.email"), "email")
            .add(Projections.property("event.fee"), "fee").add(Projections.property("student.sname"), "sname")
            .add(Projections.property("student.contactno"), "contactno")));
    cr.setResultTransformer((ResultTransformer) Transformers.ALIAS_TO_ENTITY_MAP);
    return cr.list();
}
  • Related