Youcef Laidani Youcef Laidani - 3 months ago 7
Java Question

JPA ManyToMany error when persistance

I get an error when i use ManyToMany relashioship to persist an object, i searched alot abut this problem but i can't find the solution, someone help me to find a solution for this problem please, here is my code:

Utilisateur entity

public class Utilisateur implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Integer id;
@Column(length = 2147483647)
private String nom;
private Integer theme;
@JoinTable(name = "utilisateur_prvilege", schema = "sch_admin", joinColumns = {
@JoinColumn(name = "privilege", referencedColumnName = "id")}, inverseJoinColumns = {
@JoinColumn(name = "utilisateur", referencedColumnName = "id")})
@ManyToMany(cascade = CascadeType.ALL)
private List<Privilege> privilegeList = new ArrayList<>();

..getters and setter..

}


Privilege entity

public class Privilege implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column
private Integer id;
private Integer pane;
@Column(length = 2147483647)
private String description;
@ManyToMany(mappedBy = "privilegeList")
private List<Utilisateur> utilisateurList = new ArrayList<>();
..getters and setter..

}


Create new Utilisateur

public void create(Utilisateur utilisateur) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(utilisateur);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}


Error

[EL Fine]: sql: 2016-08-06 18:30:53.286--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 6]
[EL Fine]: sql: 2016-08-06 18:30:53.291--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.293--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 4]
[EL Fine]: sql: 2016-08-06 18:30:53.295--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.296--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 7]
[EL Fine]: sql: 2016-08-06 18:30:53.298--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.302--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 12]
[EL Fine]: sql: 2016-08-06 18:30:53.304--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.306--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 9]
[EL Fine]: sql: 2016-08-06 18:30:53.307--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.309--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 9]
[EL Fine]: sql: 2016-08-06 18:30:53.31--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.312--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 4]
[EL Fine]: sql: 2016-08-06 18:30:53.313--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.314--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 7]
[EL Fine]: sql: 2016-08-06 18:30:53.315--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.316--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 11]
[EL Fine]: sql: 2016-08-06 18:30:53.317--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.318--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 1]
[EL Fine]: sql: 2016-08-06 18:30:53.319--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.321--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 1]
[EL Fine]: sql: 2016-08-06 18:30:53.324--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.326--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 8]
[EL Fine]: sql: 2016-08-06 18:30:53.328--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.33--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 11]
[EL Fine]: sql: 2016-08-06 18:30:53.343--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.35--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 6]
[EL Fine]: sql: 2016-08-06 18:30:53.352--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.354--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 3]
[EL Fine]: sql: 2016-08-06 18:30:53.355--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.356--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 12]
[EL Fine]: sql: 2016-08-06 18:30:53.357--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.358--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 10]
[EL Fine]: sql: 2016-08-06 18:30:53.359--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.36--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 5]
[EL Fine]: sql: 2016-08-06 18:30:53.361--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.362--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 5]
[EL Fine]: sql: 2016-08-06 18:30:53.363--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.364--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 3]
[EL Fine]: sql: 2016-08-06 18:30:53.365--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.366--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 10]
[EL Fine]: sql: 2016-08-06 18:30:53.367--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.368--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 8]
[EL Fine]: sql: 2016-08-06 18:30:53.369--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.37--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 2]
[EL Fine]: sql: 2016-08-06 18:30:53.371--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.374--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.PRIVILEGE (DESCRIPTION, PANE) VALUES (?, ?)
bind => [null, 2]
[EL Fine]: sql: 2016-08-06 18:30:53.375--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.376--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--INSERT INTO bd_smartstore.sch_admin.UTILISATEUR (ADRESSE, DESCRIPTION, EMAIL, NOM, PASSWORD, PRENOM, TELEPHONE, THEME, USERNAME) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [word, null, email, name, null, null, (000) 00-00-00, 5, user3]
[EL Fine]: sql: 2016-08-06 18:30:53.378--ClientSession(2094079506)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--select lastval()
[EL Fine]: sql: 2016-08-06 18:30:53.387--ServerSession(996714690)--Connection(504043352)--Thread(Thread[AWT-EventQueue-0,6,main])--SELECT ID, ADRESSE, DESCRIPTION, EMAIL, NOM, PASSWORD, PRENOM, TELEPHONE, THEME, USERNAME FROM bd_smartstore.sch_admin.UTILISATEUR


Thank you.

Answer

You need to make a small change in the ManyToMany annotation. You are defining the ManyToMany mapping in Utilisateur but your join column(not inverse mapping) is privilege instead of utilisateur. Make these changes and it should work now.

public class Utilisateur implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Integer id;
@Column(length = 2147483647)
private String nom;
private Integer theme;
@JoinTable(name = "utilisateur_prvilege", schema = "sch_admin", joinColumns = {
    @JoinColumn(name = "utilisateur", referencedColumnName = "id")}, inverseJoinColumns = {
    @JoinColumn(name = "privilege", referencedColumnName = "id")})
@ManyToMany(cascade = CascadeType.ALL)
private List<Privilege> privilegeList = new ArrayList<>();

..getters and setter..

}

Many To Many mapping Tutorial link.

Comments