Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2012
    Posts
    1

    Unanswered: Joining multiple tables in Hibernate

    Hi everyone,

    I'm new to Hibernate (even in JAVA), and I'm having some doubt's about one thing. I created 2 tables in PostgreSQL with 1 to many relationship, and now i'm trying to test some queries to them. Tables names are TbPessoa (one) and TbTelefone(many).

    TbPessoa - idPessoa(pk), usNome(charvar), usIdade(int), usTelefone(int)

    TbTelefone - idTelefone(pk), usIOperadora(charvar), usTarifario(charvar), usData(date), idPessoa(int)

    Foreign Key on TbTelefone referencing TbPessoa (idPessoa to idPessoa on TbPessoa).

    I'm trying to create a query where I would get the items where usNome='Peter' OR usOperadora='Vodafone' like we can do with SQL statments like,

    SELECT usNome, usOperadora
    FROM TbPessoa, TbTelefone
    WHERE usNome='Peter' OR usOperadora='Vodafone'

    Finnaly, I need to know how a can print (using System.out.println) the query results. I've been using (just for one table) this:

    transaction = session.beginTransaction();

    @SuppressWarnings("unchecked")

    String query = "SELECT FROM TbPessoa";
    List <TbPessoa> lista = session.createQuery(query).list();

    int i=0;

    for( TbPessoa x : lista ) {

    System.out.println( "Id: " + x.getIdPessoa() + "| Nome: " + x.getUsNome() + "| Idade:" + x.getUsIdade() + "| Morada: " + x.getUsMorada() + "| Nr. Telefone: " + x.getUsTelefone() );

    i=i+1;

    }


    Sorry my bad English! Thanks!

    Kind Regards,

    Raul

  2. #2
    Join Date
    Apr 2012
    Posts
    3
    Can you please help me how to join three tables with one common join table? I have USER, APPLICATION, and ROLE tables. And I want thier IDs to be joined in a table named USER_APP_ROLE(user.ID, application.ID, role.ID).

    When I remove either Application or Role tables in Join Many to Many my code is working.

    I have done the following codes:

    User.java

    @ManyToMany (targetEntity=Role.class) @JoinTable(name="USER_APPLICATION_ROLE", joinColumns=@JoinColumn(name="USER_ID"), inverseJoinColumns=@JoinColumn(name="ROLE_ID")) private Collection roles;

    @ManyToMany (targetEntity=Application.class) @JoinTable(name="USER_APPLICATION_ROLE", joinColumns=@JoinColumn(name="USER_ID"), inverseJoinColumns=@JoinColumn(name="APPLICATION_I D")) private Collection applications;

    Role.java

    @ManyToMany(mappedBy="roles", targetEntity=User.class) private Collection users = new ArrayList();

    Application.java

    @ManyToMany(mappedBy="applications", targetEntity=User.class) private Collection users = new ArrayList();

    When I tried to run the following test:

    user.getRoles().add(role1);
    user.getRoles().add(role2);

    role1.getUsers().add(user);
    role1.getUsers().add(user);

    role2.getUsers().add(user);
    role2.getUsers().add(user);

    user.getApplications().add(app1);
    user.getApplications().add(app2);

    app1.getUsers().add(user);
    app2.getUsers().add(user);
    ......

    session.beginTransaction();
    session.save(user);
    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.ConstraintViolationExcepti on: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert( SQLStateConverter.java:96) at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:66) at org.hibernate.jdbc.AbstractBatcher.executeBatch(Ab stractBatcher.java:275) at org.hibernate.jdbc.AbstractBatcher.prepareStatemen t(AbstractBatcher.java:114) at org.hibernate.jdbc.AbstractBatcher.prepareStatemen t(AbstractBatcher.java:109) at org.hibernate.jdbc.AbstractBatcher.prepareBatchSta tement(AbstractBatcher.java:244) at org.hibernate.persister.collection.AbstractCollect ionPersister.recreate(AbstractCollectionPersister. java:1179) at org.hibernate.action.CollectionRecreateAction.exec ute(CollectionRecreateAction.java:58) at org.hibernate.engine.ActionQueue.execute(ActionQue ue.java:273) at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:265) at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:188) at org.hibernate.event.def.AbstractFlushingEventListe ner.performExecutions(AbstractFlushingEventListene r.java:321) at org.hibernate.event.def.DefaultFlushEventListener. onFlush(DefaultFlushEventListener.java:51) at org.hibernate.impl.SessionImpl.flush(SessionImpl.j ava:1216) at org.hibernate.impl.SessionImpl.managedFlush(Sessio nImpl.java:383) at org.hibernate.transaction.JDBCTransaction.commit(J DBCTransaction.java:133) at com.hp.gdas.capman.HibernateTest.main(HibernateTes t.java:73)

    Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into ("SYSTEM"."USER_APPLICATION_ROLE"."ROLE_ID") at oracle.jdbc.driver.DatabaseError.throwBatchUpdateE xception(DatabaseError.java:343) at oracle.jdbc.driver.OraclePreparedStatement.execute Batch(OraclePreparedStatement.java:10657) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch( BatchingBatcher.java:70) at org.hibernate.jdbc.AbstractBatcher.executeBatch(Ab stractBatcher.java:268) ... 14 more

    Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •