Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA many-to-many persist to join table

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, I see groups list is not null.

Ther is no error message just persist user.

users <--> user-group <--> groups

I am using netbeans 7.3, Glassfish 3.1.2.2, postgresql 9.1 and eclipselink 2

Also, I tried to show sql scripts that properties below doesn't work for me.

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

Abstact DAO:

public abstract class GenericDAO<E> implements Serializable{

@PersistenceContext
EntityManager entityManager;

public void persist(E object){
    entityManager.persist(object);
}

public void merge(E object){
    entityManager.merge(object);
}

public void delete(E object){
    object = entityManager.merge(object);
    entityManager.remove(object);
}
}

Users Entity :

   @Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries(
{
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
    @NamedQuery(name = "Users.findByUserId", query = "SELECT u FROM Users u WHERE u.userId = :userId"),
    @NamedQuery(name = "Users.findByName", query = "SELECT u FROM Users u WHERE u.name = :name"),
    @NamedQuery(name = "Users.findBySurname", query = "SELECT u FROM Users u WHERE u.surname = :surname"),
    @NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
    @NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
    @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password")
})
public class Users implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "user_id")
    private Integer userId;
    @Size(max = 2147483647)
    @Column(name = "name")
    private String name;
    @Size(max = 2147483647)
    @Column(name = "surname")
    private String surname;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @Size(max = 2147483647)
    @Column(name = "email")
    private String email;
    @Size(max = 2147483647)
    @Column(name = "username")
    private String username;
    @Size(max = 2147483647)
    @Column(name = "password")
    private String password;
    @ManyToMany(mappedBy = "usersList")
    private List<Groups> groupsList;
    @OneToMany(mappedBy = "userId")
    private List<Person> personList;

//Getters Setters

Groups Entity :

@Entity
@Table(name = "groups")
@XmlRootElement
@NamedQueries(
{
    @NamedQuery(name = "Groups.findAll", query = "SELECT g FROM Groups g"),
    @NamedQuery(name = "Groups.findByGroupId", query = "SELECT g FROM Groups g WHERE g.groupId = :groupId"),
    @NamedQuery(name = "Groups.findByGroupName", query = "SELECT g FROM Groups g WHERE g.groupName = :groupName"),
    @NamedQuery(name = "Groups.findByGroupDescription", query = "SELECT g FROM Groups g WHERE g.groupDescription = :groupDescription")
})
public class Groups implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "group_id")
    private Integer groupId;
    @Size(max = 2147483647)
    @Column(name = "group_name")
    private String groupName;
    @Size(max = 2147483647)
    @Column(name = "group_description")
    private String groupDescription;
    @JoinTable(name = "user_group", joinColumns =
    {
        @JoinColumn(name = "group_id", referencedColumnName = "group_id")
    }, inverseJoinColumns =
    {
        @JoinColumn(name = "user_id", referencedColumnName = "user_id")
    })
    @ManyToMany(fetch = FetchType.EAGER)
    private List<Users> usersList;
//Getters Setters

User DAO :

public class UserDAO extends GenericDAO<Users> implements Serializable {

    public List<Users> getAllUsers()
    {
        Query query = entityManager.createNamedQuery("Users.findAll");
        List<Users> users = query.getResultList();
        return users;
    }
}
like image 357
erdoganonur Avatar asked Oct 19 '13 11:10

erdoganonur


People also ask

How do you persist many-to-many relationships in JPA?

In JPA we use the @ManyToMany annotation to model many-to-many relationships. This type of relationship can be unidirectional or bidirectional: In a unidirectional relationship only one entity in the relationship points the other. In a bidirectional relationship both entities point to each other.

What should be done for many-to-many joins in Hibernate?

In order to map a many-to-many association, we use the @ManyToMany, @JoinTable and @JoinColumn annotations. Let's have a closer look at them. The @ManyToMany annotation is used in both classes to create the many-to-many relationship between the entities.

Can a JPA entity have multiple Onetomany associations?

You can have multiple one-to-many associations, as long as only one is EAGER.

Is @column mandatory in JPA?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.


2 Answers

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"/>
like image 163
Ravi K Thapliyal Avatar answered Sep 18 '22 02:09

Ravi K Thapliyal


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

like image 34
viejodecaldas Avatar answered Sep 22 '22 02:09

viejodecaldas