JPA many-to-many persist to join table

前端 未结 2 1822
遥遥无期
遥遥无期 2020-12-18 04:52

I have two table with many-to-many relations. I mapped this two entities when I am persisting users doesn\'t insert anything to join table. I am debugging till the persist,

相关标签:
2条回答
  • 2020-12-18 05:18

    For your first question about to show sql scripts, i've added this code in the persistence.xml

    <properties>
      <property name="eclipselink.logging.level.sql" value="FINE"/>
      <property name="eclipselink.logging.parameters" value="true"/>
    </properties>
    

    After that, clean and build and then deploy. The sql executed by JPA will be displayed in the glassfish domain log of the netbeans.

    For your second issue about the many-to-many relationship.

    i was having the same issue in the same scenario, beacuse i've to implement web security and i need the same relationship you are using. Anyway, i figured out that the problem wasn't in the entities.

    You have to check (for my case) the insert and edit methods on the UserJPAController.java, those methods some how knows how the entities are referenced each other, so i implemented this code and it works fine.

    List<Grupo> attachedGrupoList = new ArrayList<Grupo>();
            for (Grupo grupoListGrupoToAttach : usuario.getGrupoList()) {
                grupoListGrupoToAttach = em.getReference(grupoListGrupoToAttach.getClass(),      grupoListGrupoToAttach.getIdGrupo());
                attachedGrupoList.add(grupoListGrupoToAttach);
            }
            usuario.setGrupoList(attachedGrupoList);
            em.persist(usuario);
    for (Grupo grupoListGrupo : usuario.getGrupoList()) {
                grupoListGrupo.getUsuarioList().add(usuario);
                grupoListGrupo = em.merge(grupoListGrupo);
            }
    

    After executing that code and the transaction has ended you will see the sql scripts executed by JPA in the Data Base.

    Hope it helps!

    Mike

    0 讨论(0)
  • 2020-12-18 05:26

    You need to enable cascade for merge (using PERSIST) or all operations with ALL.

    @ManyToMany(mappedBy = "usersList", cascade = CascadeType.PERSIST)
    private List<Groups> groupsList;
    

    If I set CascadeType.PERSIST, it insert data to groups table too. I want to add date users table and user_group table (pk_user, pk_group)

    The way a mapping/join table works is that user_group would have a foreign key constraint on user and group table. That's why a new row in group has to be inserted for its primary key to be used to add a new row to user_group.

    This has nothing to do with JPA and the same would apply to you even if you were using plain JDBC instead. This is how Entity-Relationships work in database.

    Also I dropped all tables and they were generated by eclipse link automatically. They are same but don't insert any row to 'user_group'.

    This behaviour is controlled by the eclipselink.ddl-generation property specified for your persistence-unit. When specified as drop-and-create-tables, EclipseLink recreates the whole database schema (deleting any existing data in the process).

    <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    

    This, however, is enabled just to ease your development. This isn't supposed to be used in production environments where its disabled by not specifying this property or by setting its value as none.

    <property name="eclipselink.ddl-generation" value="none"/>
    
    0 讨论(0)
提交回复
热议问题