JPA多对多关系未插入生成的表 [英] JPA many to many relation not inserting into generated table

查看:71
本文介绍了JPA多对多关系未插入生成的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个多对多关系,尽管我可以在两个实体表中编写内容,但是关系表没有编写任何内容.

I have a many-to-many relation in my project and although I'm able to write in my two Entities table, the relational table does not get anything written.

这是我使用JPA批注声明的方式:

Here's how I'm declaring this using JPA annotations:

Professor.java

Professor.java

@Entity
@Table(name = "Professor")
public class Professor implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "ALUNO_PROFESSOR",
            joinColumns = @JoinColumn(name = "idProfessor", referencedColumnName = "idProfessor"),
            inverseJoinColumns = @JoinColumn(name = "idAluno", referencedColumnName = "idAluno"))
    private List<Aluno> alunoList;

    // getters and setters
}

Aluno.java

Aluno.java

@Entity
@Table(name = "Aluno")
public class Aluno implements Serializable {
    private static final long serialVersionUID = 1L;

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

    @ManyToMany(mappedBy = "alunoList", fetch = FetchType.EAGER)
    private List<Professor> professorList;

    // getters and setters
}

这是要插入数据库的服务层:

And here is the service layer to insert into database:

@Autowired
private AlunoDao alunoDao;

@Autowired
private ProfessorDao professorDao;

@RequestMapping(value = RestUriConstants.SUBMETER, method = RequestMethod.POST)
public @ResponseBody JsonResponse submeter(@RequestBody final Aluno aluno) {

    Professor professor = professorDao.find(1);
    aluno.setProfessorList(Arrays.asList(professor));
    alunoDao.persist(aluno);

    ...
}

在这种情况下,请考虑我已经有一个ID为"1"的教授条目.

In this case, please consider that I already have an entry with id "1" for Professor.

正如我说的那样,它确实在Aluno和Professor表上进行写操作,但不向ALUNO_PROFESSOR表中写任何内容.

As I said, it does write on Aluno and Professor table but does NOT write anything into ALUNO_PROFESSOR table.

我已经看过这三种类似的问题,但是它们都无法帮助我:

I've already taken a look at these three kind of similiar questions but none of them could help me:

Hibernate和Spring:未插入生成表中的多对多值

JPA多对多坚持加入表

如何保持@ManyToMany关系-重复输入或独立实体

编辑-添加更多代码段

JpaAlunoDao.java

JpaAlunoDao.java

@Repository
public class JpaAlunoDao implements AlunoDao {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void persist(Aluno aluno) {
        em.persist(aluno);
    }
}

JpaExercicioDao.java

JpaExercicioDao.java

@Repository
public class JpaExercicioDao implements ExercicioDao {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void persist(Exercicio exercicio) {
        em.persist(exercicio);
    }
}

推荐答案

尝试一下:

public class Professor {
  @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinTable(name = "ALUNO_PROFESSOR",
        joinColumns = @JoinColumn(name = "idProfessor", referencedColumnName = "idProfessor"),
        inverseJoinColumns = @JoinColumn(name = "idAluno", referencedColumnName = "idAluno"))
  private List<Aluno> alunoList;
}

public class Aluno {
  @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinTable(name = "ALUNO_PROFESSOR",
        joinColumns = @JoinColumn(name = "idAluno", referencedColumnName = "idAluno"),
        inverseJoinColumns = @JoinColumn(name = "idProfessor", referencedColumnName = "idProfessor"))
  private List<Professor> professorList;
}

这将确保多对多关系的元数据在两个实体上都可用,并且关系的任一侧的操作都级联到另一侧.

This will ensure that the metadata for the many-to-many relationship is available on both the entities and that operations on either side of the relationship are cascaded to the other side.

我还建议将 FetchType.EAGER 替换为 FetchType.LAZY ,以获得更好的性能,因为这有可能加载非常大的数据集.

I also suggest replacing FetchType.EAGER with FetchType.LAZY for better performance because this has the potential of loading a very large dataset.

这篇关于JPA多对多关系未插入生成的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆