1 Reply Latest reply on Jul 14, 2006 1:41 PM by tsar_bomba

    @OneToOne, no primary keys...

      I'm simply trying to join two entities together w/ a @OneToOne but I'm not using primary keys on either side of the relationship.

      Here are my (shortened) entities:

      @Entity
      @Table(name="call_req")
      public class Incident implements Serializable
      {
       @Column(name="status", insertable=false, updatable=false)
       private String status;
      
       @OneToOne(fetch=FetchType.LAZY)
       @JoinColumn(name="status", referencedColumnName="code")
       private IncidentStatus incidentStatus;
      }
      
      @Entity
      @Table(name="cr_stat")
      public class IncidentStatus implements Serializable
      {
       @Column(name="code", nullable=false, insertable=false, updatable=false)
       private String code;
      
       @OneToOne
       private Incident incident;
      }
      


      Here's the EJBQL and the query that was generated:

      Hibernate:
       /* select
       i
       from
       Incident i
       inner join
       fetch i.incidentStatus ist */ select
       top 50 incident0_.id as id927_0_,
       incidentst1_.id as id928_1_,
       incident0_.persid as persid927_0_,
       incident0_.ref_num as ref3_927_0_,
       incident0_.summary as summary927_0_,
       incident0_.description as descript5_927_0_,
       incident0_.status as status927_0_,
       incident0_.active_flag as active7_927_0_,
       incident0_.open_date as open8_927_0_,
       incident0_.time_spent_sum as time9_927_0_,
       incident0_.last_mod_dt as last10_927_0_,
       incident0_.close_date as close11_927_0_,
       incident0_.resolve_date as resolve12_927_0_,
       incident0_.rootcause as rootcause927_0_,
       incident0_.log_agent as log14_927_0_,
       incident0_.assignee as assignee927_0_,
       incident0_.group_id as group16_927_0_,
       incident0_.customer as customer927_0_,
       incident0_.charge_back_id as charge18_927_0_,
       incident0_.affected_rc as affected19_927_0_,
       incident0_.support_lev as support20_927_0_,
       incident0_.category as category927_0_,
       incident0_.solution as solution927_0_,
       incident0_.impact as impact927_0_,
       incident0_.priority as priority927_0_,
       incident0_.urgency as urgency927_0_,
       incident0_.severity as severity927_0_,
       incident0_.extern_ref as extern27_927_0_,
       incident0_.last_act_id as last28_927_0_,
       incident0_.cr_tticket as cr29_927_0_,
       incident0_.parent as parent927_0_,
       incident0_.template_name as template31_927_0_,
       incident0_.sla_violation as sla32_927_0_,
       incident0_.predicted_sla_viol as predicted33_927_0_,
       incident0_.created_via as created34_927_0_,
       incident0_.call_back_date as call35_927_0_,
       incident0_.call_back_flag as call36_927_0_,
       incident0_.event_token as event37_927_0_,
       incident0_.type as type927_0_,
       incident0_.change as change927_0_,
       incident0_.problem as problem927_0_,
       incident0_.incident_priority as incident41_927_0_,
       incidentst1_.persid as persid928_1_,
       incidentst1_.del as del928_1_,
       incidentst1_.sym as sym928_1_,
       incidentst1_.last_mod_dt as last5_928_1_,
       incidentst1_.description as descript6_928_1_,
       incidentst1_.code as code928_1_,
       incidentst1_.active as active928_1_,
       incidentst1_.hold as hold928_1_,
       incidentst1_.resolved as resolved928_1_,
       incidentst1_.cr_flag as cr11_928_1_,
       incidentst1_.in_flag as in12_928_1_,
       incidentst1_.pr_flag as pr13_928_1_,
       incidentst1_.incident_id as incident14_928_1_
       from
       call_req incident0_
       inner join
       cr_stat incidentst1_
       on incident0_.status=incidentst1_.code
      


      ...it's so *close* - if it wasn't creating that 'incident_id' field out of thin-air (which doesn't actually exist in the table) it would be perfect. Otherwise, the join is perfect - exactly what I needed. Of course the exception I get is:


      javax.ejb.EJBException

      .............

      Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:567)
      at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:56)

      .............

      Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)

      .............

      Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'incident_id'.
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
      ..............


      I need Incident.status = IncidentStatus.code

      I'm getting very close but I can't seem to get the right relationship. How can I simply join two entities, one-to-one, where I'm not using the primary keys in either of the two tables as part of the relationship?

      Thanks very much in advance!

        • 1. Re: @OneToOne, no primary keys...

          I have an update on this issue, hopefully someone can fill me in. I've been testing Hibernate as the persistence engine in Glassfish and once I switched to Toplink in Glassfish, this issue went away. See this thread:

          http://forums.java.net/jive/thread.jspa?messageID=132930&#132930

          I've been using Hibernate 3.2.0.cr2 (and related annotations, entitymanager libs) but I noticed cr3 is out - does this fix this issue?

          I use both JBoss and Glassfish and would like to use Hibernate as the persistence engine in any and all cases as I use a lot of Hibernate outside of the container as well - I would like to continue to do so!

          Thanks!