java - JPA Hibernate 一对多

标签 java hibernate jpa one-to-many

对 JPA 中一对多的工作方式感到非常困惑,我阅读的所有文档在其示例中都使用了一对多和多对一,我不知道它们是否有必要,而且它没有我尝试的时候不行。

我的问题是,假设我有两个表,我想使用 findCollegeData() 方法填充 College 对象,以便在初始化该对象时,该学院的所有学生都在列表中。

下面是我的方法,我可以使用 storeCollegeData() 方法存储大学列表中的所有学生,但我无法完全检索大学对象,学生列表始终为空,即使数据是在数据库中,如果我尝试直接使用大学名称搜索学生,它会起作用。

public static EntityManager entityManager = something;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public College {
    @Id   
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int cId;
    private String collegeName;
    private int numOfStudent;

    @OneToMany(mappedBy="collegeName", cascade=CascadeType.ALL, orphanRemoval=true)
    private List<Student> studentList = new ArrayList<>();
}


@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public Student {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int sId;
    private String name;
    private String collegeName;
    private String city;
}


// college.getStudentList is always empty and I don't know why
public findCollegeData(String collegeName) {
    College college = entityManager.find(College.class, collegeName);
}

// Student data in the studentList are inserted into student table
public storeCollegeData(College college) {
    entityManager.persist(college);
}

// This method works
public findStudent(String collegeName) {

    CriteriaBuilder cb = provider.get().getCriteriaBuilder();
    CriteriaQuery<Student> query = cb.createQuery(Student.class);
    Root<Student> student = query.from(Student.class);

    query.where(
            cb.and(
                    cb.equal(student.get("collegeName"), collegeName)
            )
    );

    JobStatisticDB Student = provider.get().createQuery(query).getSingleResult();
}

我是不是漏掉了什么??? join 比这里的 map 更合适吗???我不知道该做什么,伙计

编辑: 通过添加 @Id 注释将 CollegeName 更改为表的主键来使其工作,但是,如何将 sId 和 cId 添加到表中,以便它们可以具有重复的大学名称???现在,我不能拥有同名的重复大学,以及就读同一所大学的学生!

最终编辑: 更改数据库设计以使用外键,请参阅下面的解决方案

最佳答案

接受的答案不正确:您定义实体之间的关系。对于双向 @OneToMany

,映射应如下所示

学院:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public College {
    @Id   
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int cId;
    private String collegeName;
    private int numOfStudent;

    @OneToMany(mappedBy="college", cascade=CascadeType.ALL, orphanRemoval=true)
    private List<Student> studentList = new ArrayList<>();
}

学生:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public Student {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int sId;
    private String name;
    private String city;

    //student table has a FK column college_id 
    @ManyToOne
    @JoinColumn(name = "college_id")
    private College college;
}

EntityManager find() 将 PK 作为参数:

public findCollege(int collegeId) {
    College college = entityManager.find(College.class, collegeId);
    college.getStudents(); //will be populated
}

public findStudent(int studentId) {
    Student student = entityManager.find(Student.class, studentId);
    student.getCollege(); //will be populated
    student.getCollege().getStudents(); //will be populated
}

如果您想按名称查找大学,请创建 JPQL 或 Criteria 查询:

关于java - JPA Hibernate 一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39047600/

相关文章:

java - 我应该在线程中声明变量还是在Android中创建一个全局变量?

java - android: listpreference setOnPreferenceClickListener 错误

java - 比较不同格式的两个日期

mysql - 关于spring/jpa/hibernate中乐观锁的简单设计问题

java - JPA 类型的搜索查询

java - 在 hibernate 中从 session.createQuery 获取超过 1 个对象

java - 跨类使用方法?

java - 将命令行参数传递给在 Docker 中运行的 Java 应用程序 (Spring Boot)

java - 如何在 hibernate 和 postgres 上同步 id?

java - 为什么这个应该选择一些随机行的 Hibernate HQL 查询不起作用?我获得了 "QuerySyntaxException: unexpected token"