1 Reply Latest reply on Oct 26, 2005 8:59 AM by dornus

    Need help for primary key as foreign key

    dornus Novice

      I have 2 database tables.

      TableOne
       `myAutoId` int(10) unsigned NOT NULL auto_increment,
       `myValue` varchar(96) NOT NULL,
       etc...
      


      TableTwo
       `myTableOneReference` int(10) unsigned NOT NULL,
       `myOtherValue` tinyint(3) unsigned NOT NULL,
       etc...
      


      The primary key for the second table is a reference to the Table One object. I was told to set it up similar to below.

       TableTwo.class
      
       private int myTableOneReference;
       private TableOne tableOne;
       private int myOtherValue;
      
       @Id
       public int getMyTableOneReference() {
       return myTableOneReference;
       }
      
       @ManyToOne
       @JoinColumn(insertable=false, updatable=false, name="myTableOneReference")
       public TableOne getTableOne() {
       return tableOne;
       }
      


      Now when I run the following code, I get mapping errors. I have tried
       private TableTwo getTableTwo(TableOne tableOne) {
       final Query q = em.createQuery(
       "from TableTwo o " +
       "where tableOne = :tableOne "
       );
       q.setParameter("tableOne", tableOne);
      
       return (TableTwo) q.getSingleResult();
       }
      

      and
       private TableTwo getTableTwo(int myTableOneReference) {
       final Query q = em.createQuery(
       "from TableTwo o " +
       "where myTableOneReference = :myTableOneReference "
       );
       q.setParameter("myTableOneReference", myTableOneReference);
      
       return (TableTwo) q.getSingleResult();
       }
      

      and get similar error messages, which is shown below.
      [STDOUT] org.hibernate.hql.ast.QuerySyntaxException: TableTwo is not mapped. [from TableTwo o where myTableOneReference = :myTableOneReference ]
       at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:145)
       at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:86)
       at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
       at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:260)
       at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3019)
       at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2908)
       at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:676)
       at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:532)
       at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:269)
       at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:217)
       at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:214)
       at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:154)
       at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:101)
       at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:473)
       at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1032)
       at org.hibernate.impl.SessionImpl.list(SessionImpl.java:982)
       at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
       at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:650)
       at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:43)