Hibernate注释 - 如何连接具有多对多关系的三个表

时间:2014-01-04 08:00:14

标签: java hibernate nhibernate-mapping hibernate-annotations

  1. 我有3个实体USER,APPLICATION和ROLE。

    1. 我希望在名为USER_APP_ROLE(user_ID,application_ID,role_ID)的表中加入ID,它们之间有多对多关系。
    2. 我希望这种结构的原因是 - 我要求允许用户处理具有多个角色的多个应用程序。
    3. 我已完成以下代码:
    4. 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>();
      
      1. 当我尝试运行以下测试时:
      2. 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.我可能在错误的方向上这样做。如果我完全以错误的方式做,请帮助并建议我任何替代方案。感谢任何帮助。

        感谢。

1 个答案:

答案 0 :(得分:4)

您似乎希望USER_APPLICATION_ROLE表包含对应用程序中用户的角色分配。但这不是你的映射所做的。它的作用是使用此表来包含用户的角色(忽略应用程序),以及用户的应用程序(忽略角色)。

您的映射错误。您需要将USER_APPLICATION_ROLE表映射为实体,因为它不是两个实体之间的纯连接表。例如,您可以将其命名为UserApplicationRole。用户与UserApplicationRole具有OneToMany关联。 UserApplicationRole与User,以及Role和Application有一个ManyToOne关联。我强烈建议您在此表中添加一个自动生成的ID列,就像所有实体一样。