gpt4 book ai didi

java - Hibernate JOIN 结果集

转载 作者:搜寻专家 更新时间:2023-10-30 19:50:11 24 4
gpt4 key购买 nike

我在使用 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;

...

和 Controller 类方法:

@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

最佳答案

试试这个:

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

关于java - Hibernate JOIN 结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49706020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com