Hibernate JOIN结果集

时间:2018-04-07 09:47:54

标签: java database spring hibernate spring-mvc

我在使用Hibernate和Spring MVC时遇到了问题,也就是说,如果我在内部使用JOIN运行查询,我会收到500错误而没有任何细节,有人可以帮助我吗?

2表是:

用户:

 @Entity
 @Table(name = "user")
 public class User {
    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
    @Column(name = "id_user")
    private Long id;

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

    @Column(name = "surname")
    private String surname;

    @OneToMany(mappedBy = "contentManager")
    private Set<Project> projects;
    ...

项目:

    @Entity
    @Table(name = "projects")
    public class Project {

        @Id
        @GeneratedValue(generator="increment")
        @GenericGenerator(name="increment", strategy = "increment")
        @Column(name = "id_project")
        private Long id;

        @Column(name = "project_name")
        private String name;

        @Column(name = "project_language")
        private String language;

        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name = "id_content_manager")
        @JsonBackReference
        private User contentManager;

        ...

控制器类方法:

@Transactional
@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody  List<Project> home(Locale locale, Model model) {

    Session session=new Configuration().configure().buildSessionFactory().getCurrentSession();

    session.beginTransaction();
    List<Project> result = (List<Project>) session.createQuery("FROM projects p JOIN p.contentManager").getResultList();
    session.getTransaction().commit();
    session.close();

    return result;
}

有人可以帮助我吗?

编辑:tomcat控制台:

WARN : org.hibernate.orm.connections.pooling - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Hibernate: select project0_.id_project as id_proje1_0_0_, user1_.id_user as id_user1_1_1_, project0_.id_content_manager as id_conte4_0_0_, project0_.project_language as project_2_0_0_, project0_.project_name as project_3_0_0_, user1_.name as name2_1_1_, user1_.surname as surname3_1_1_ from projects project0_ inner join user user1_ on project0_.id_content_manager=user1_.id_user

浏览器截图: enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

List<?> list = (List<?>) session.createQuery("FROM Project p JOIN p.contentManager").list();
List<Project> result  =  new ArrayList<Project>();  
for(int i = 0; i < list.size(); i++) {
    Object[] row = (Object[]) list.get(i);
    Project p = (Project) row[0];
    User s = (User) row[1];
    p.setContentManager(s);
    result.add(p);
}