gpt4 book ai didi

java - ManyToMany 注释不会在选择查询中生成联接

转载 作者:行者123 更新时间:2023-11-29 15:41:52 24 4
gpt4 key购买 nike

摘要:我正在尝试使用 jpa + hibernate 构建并急切地检索单向 ManyToMany 关系。

当执行 findAll() 时,引用的实体为空,而本应有 1。

根据我的观察,hibernate 生成的 SQL(使用 show-sql 属性)缺少连接。

上下文:

  • Spring 数据 - 4.0.4.RELEASE
  • hibernate - 4.2.21.Final

我尝试过的一些替代方案/修复:

  • 已尝试将它们切换为 OneToMany/ManyToOne 关系。
  • 尝试过使用mappedBy引用Student的类来建立双向ManyToMany关系
  • 已尝试更新类的 id 列,使其在连接表中不发生变化

Java 实体

@Data
@Entity
@Table(name = "student")
public class Student {

@Column(name = "student_id", unique = true, nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Data
@Entity
@Table(name = "Class")
public class Class {

@Column(name = "id", unique = true, nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "class_student",
joinColumns = @JoinColumn(name = "class_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "student_id", referencedColumnName = "student_id"))
private Set<Student> students;

DDL:

CREATE TABLE IF NOT EXISTS `student` (
student_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`student_id `)
);

CREATE TABLE IF NOT EXISTS `class` (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `class_student ` (
student_id INT NOT NULL,
class_id INT NOT NULL,
PRIMARY KEY (`student_id `, `class_id`),
CONSTRAINT `fk_student_id` FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`),
CONSTRAINT `fk_class_id` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`)
);

实际与预期:当我 classRespository.getAll() 时实际生成的 sql 似乎缺少我期望的 class + class_student 之间的连接。

它还奇怪地包括我没想到的 Student 和 class_student 之间的连接,因为它是单向定义的,而且除了可能被丢弃之外,我不知道如何处理该意外查询的结果。

实际生成的 SQL :

Hibernate: 
select
classin0_.id as id1_29_,
from
class classin0_
INNER JOIN
post_tag pt
ON p.id = pt.post_id
INNER JOIN
tag t
ON pt.tag_id = t.id
WHERE p.id = 1
Hibernate:
select
class_student0_.class_id as id1_30_1_,
class_student0_.student_id as featured2_10_1_,
student1_.student_id as featured1_28_0_,
from
class_student class_student0_
inner join
student student1_
on class_student0_.student_id=student1_.student_id
where
class_student0_.id=?

我期望一个跨三个表的内部联接查询,jpa 可以将其解析回一个集合。

如有任何帮助,我们将不胜感激。

最佳答案

我已经检查了您的映射 - 它是正确的。我正在使用这些类。

@Entity
@Table(name = "student")
public class Student {

@Column(name = "student_id", unique = true, nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

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

}

@Entity
@Table(name = "Class")
public class Class {

@Column(name = "id", unique = true, nullable = false)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

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

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "class_student",
joinColumns = @JoinColumn(name = "class_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "student_id", referencedColumnName = "student_id"))
private Set<Student> students;

}

The actual generated sql when I classRespository.getAll() seems to be missing the join between class + class_student that I would expect. It also oddly includes a join between student and class_student that I would not expect

Hibernate 不使用 join 来连接 classclass_student 关系。

Hibernate 通过第一个查询获取所有类

select
class0_.id as id1_0_,
class0_.className as classNam2_0_
from
Class class0_

然后,使用 class_studentstudent 之间的内部联接获取一个现有类(class)的所有学生。

   select
students0_.class_id as class_id1_1_0_,
students0_.student_id as student_2_1_0_,
student1_.student_id as student_1_2_1_,
student1_.name as name2_2_1_
from
class_student students0_
inner join
student student1_
on students0_.student_id=student1_.student_id
where
students0_.class_id=?

我无法检查您的 SQL,因为它不正确。例如,class_student表中没有这样的字段class_student0_.id=?

关于java - ManyToMany 注释不会在选择查询中生成联接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57553333/

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