我有3个实体USER,APPLICATION和ROLE。
User.java @ManyToMany(targetEntity = Role.class) @JoinTable(名称= “USER_APPLICATION_ROLE”, joinColumns = @ JoinColumn(名称= “USER_ID”), inverseJoinColumns = @ JoinColumn(名称= “ROLE_ID”)) 私人收藏角色;
@ManyToMany (targetEntity=Application.class)
@JoinTable(name="USER_APPLICATION_ROLE",
joinColumns=@JoinColumn(name="USER_ID"),
inverseJoinColumns=@JoinColumn(name="APPLICATION_ID"))
private Collection<Application> applications;
Role.java
@ManyToMany(mappedBy="roles", targetEntity=User.class)
private Collection<User> users = new ArrayList<User>();
Application.java
@ManyToMany(mappedBy="applications", targetEntity=User.class)
private Collection<User> users = new ArrayList<User>();
user.getRoles()添加(基于role1)。 。user.getRoles()添加(基于role2);
role1.getUsers()添加(用户)。 。role1.getUsers()添加(用户);
role2.getUsers()添加(用户)。 。role2.getUsers()添加(用户);
user.getApplications()添加(APP1)。 。user.getApplications()添加(APP2);
app1.getUsers()添加(用户)。 。app2.getUsers()添加(用户);
...
session.beginTransaction(); session.save(用户); session.save(基于role1); session.save(role2所); session.save(APP1); session.save(APP2);
I get the following error:
Hibernate: select seq_cm_user.nextval from dual
Hibernate: select seq_role.nextval from dual
Hibernate: select seq_role.nextval from dual
Hibernate: select seq_application.nextval from dual
Hibernate: select seq_application.nextval from dual
Hibernate: insert into CM_USER (EMAIL, FIRST_NAME, LAST_NAME, MIDDLE_NAME, USERNAME, ID) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into CM_ROLE (DESCRIPTION, ID) values (?, ?)
Hibernate: insert into CM_ROLE (DESCRIPTION, ID) values (?, ?)
Hibernate: insert into CM_APPLICATION (CODE, DESCRIPTION, ID) values (?, ?, ?)
Hibernate: insert into CM_APPLICATION (CODE, DESCRIPTION, ID) values (?, ?, ?)
Hibernate: insert into USER_APPLICATION_ROLE (USER_ID, APPLICATION_ID) values (?, ?)
Hibernate: insert into USER_APPLICATION_ROLE (USER_ID, APPLICATION_ID) values (?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1179)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.hp.gdas.capman.HibernateTest.main(HibernateTest.java:73)
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("SYSTEM"."USER_APPLICATION_ROLE"."ROLE_ID")
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10657)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 14 more
6.我可能在错误的方向上这样做。如果我完全以错误的方式做,请帮助并建议我任何替代方案。感谢任何帮助。
感谢。
答案 0 :(得分:4)
您似乎希望USER_APPLICATION_ROLE表包含对应用程序中用户的角色分配。但这不是你的映射所做的。它的作用是使用此表来包含用户的角色(忽略应用程序),以及用户的应用程序(忽略角色)。
您的映射错误。您需要将USER_APPLICATION_ROLE表映射为实体,因为它不是两个实体之间的纯连接表。例如,您可以将其命名为UserApplicationRole。用户与UserApplicationRole具有OneToMany关联。 UserApplicationRole与User,以及Role和Application有一个ManyToOne关联。我强烈建议您在此表中添加一个自动生成的ID列,就像所有实体一样。