0
我嘗試:(看代碼),但它不起作用。JPA,如何插入關係ManyToMany的新實體?
List<User> users = query.getResultList();
if (users.isEmpty()) {
System.out.println("tworzę konto admina");
User admin = new User();
admin.set(...)
Role role = new Role();
role.setName("Develop");
role.set...
// the first method dont work
admin.getRoles().add(role); /// but admin.getRoles() IS Null!!! (I get nullPointerExeption)
// the second method dont work too
// List<Role> roles = new ArrayList<Role>();
//roles.add(role);
//admin.setRoles(roles);
entityManager.persist(role);
entityManager.persist(admin);
}
用戶等級:
public class User {
@ManyToMany
@JoinTable(
name = "role_has_user",
joinColumns = {
@JoinColumn(name = "user_id")
},
inverseJoinColumns = {
@JoinColumn(name = "role_id")
}
)
}
在第二種方法獲得:
Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'rw.SEQUENCE' doesn't exist
Error Code: 1146
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
bind => [2 parameters bound]
Query: DataModifyQuery(name="SEQUENCE" sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)...
你能告訴我們完整的'User'類嗎?你的'eclipselink.ddl-generation'屬性值是什麼? –
根據異常跟蹤,問題與ManyToMany關係無關。您可能已經設置了要生成的用戶字段之一,但尚未在要生成的persistence.xml表中指定。因此,沒有稱爲SEQUENCE的表,可以用它來生成你生成的一個Ids – maress