Hibernate条件与2个表连接

vtwuwzda  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(133)

我有两个表,一个是Student表,另一个是Event表。我正在尝试连接这两个表,这是Student表

@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;

这是“事件”表

@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

;
我正在编写在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();
    }

在控制器中

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

运行代码时,出现错误HTTP状态500 -请求处理失败;嵌套的异常是组织。无法解析属性:事件:样本.实体.学生

klr1opcd

klr1opcd1#

由于学生和事件之间没有直接关系,而是事件和学生之间的多对一关系,因此必须将查询更改为从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();
}

相关问题