我正在尝试使用以下方法更新可用用户的角色
PersistenceManager pm = connectionFactory.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
try {
Query query = pm.newQuery(User.class, "email == '" + email + "'");
List<User> user = (List<User>) query.execute();
user.get(0).getRoles().clear();
for (Role r : roles) {
user.get(0).getRoles().add(pm.getObjectById(Role.class, r.getId()));
}
tx.commit();
return true;
} catch (Exception e) {
throw e;
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
我观察到的是,并非所有角色都被分配了。它是不一致的,有时候会分配3个角色中的3个,有时候所有角色都被分配,有时候不属于它们。
根据我所了解的文件,
有人能告诉我我做错了吗?
我正在使用MySQL作为数据库。
--- ---编辑
@PersistenceCapable(table = "USER_INFO")
public class User{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Column(name = "USER_ID")
private Long Id;
@Persistent
@Column(name = "EMAIL")
private String email;
@Persistent
@Column(name = "PASSWORD")
private String password;
@Persistent
@Column(name = "USERNAME")
private String username;
@Persistent
@Column(name = "PHONE_NUMBER")
private String phonenumber;
@Column(name = "VALID")
private Boolean isValidUser = true;
@Persistent
@Column(name = "ACTIVE")
private String active;
@Persistent(table = "USER_ROLE_RELATION", defaultFetchGroup = "true")
@Join(column = "USER_ID")
@Element(column = "ROLE_ID")
private List<Role> role = new ArrayList<Role>();
@Persistent(table = "USER_STREAM_RELATION", defaultFetchGroup = "true")
@Join(column = "USER_ID")
@Element(column = "STREAM_ID")
private List<Stream> stream = new ArrayList<Stream>();
}
@PersistenceCapable(table = "ROLE")
public class Role implements Serializable {
private static final long serialVersionUID = 2600988373295393375L;
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY, column = "ROLE_ID")
private Long id;
@Unique
@Persistent(column = "ROLE_NAME")
private String roleName;
@Persistent(mappedBy = "role")
private List<UserInfo> user;
}
我正在尝试更新角色。这是删除以前添加的角色并添加新角色
抛出异常:
2017-01-25 15:09:11 DEBUG Datastore:58 - Using PreparedStatement "org.datanucleus.store.rdbms.ParamLoggingPreparedStatement@6e68bf64"
2017-01-25 15:09:11 DEBUG Native:58 - INSERT INTO `USER_ROLE_RELATION` (`ROLE_ID`,`USER_ID`,`IDX`) VALUES (<3>,<1>,<1>)
2017-01-25 15:09:11 DEBUG NewPooledConnection:430 - com.mchange.v2.c3p0.impl.NewPooledConnection@7d7d54ba handling a throwable.
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '3-1' for key 'PRIMARY'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:971)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:971)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530)
2017-01-25 15:09:11 DEBUG DefaultConnectionTester:126 - Testing a Connection in response to an Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '3-1' for key 'PRIMARY'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:971)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530)