These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate generates wrong SQL code.
PostPosted: Sat Feb 24, 2007 4:28 am 
Beginner
Beginner

Joined: Mon Mar 20, 2006 7:59 am
Posts: 30
Hi,

first of all: I am using Hibernate 3.2.2 GA - the newest version of hibernate.

I've got two classes: a class Event and a class Employee. The Event class has got a field employees which is a List of Employee-objects.

When I execute this query:
Code:
select e.employees from Event e

everything works properly. The generated SQL code is:
Code:
select employee2_.id as id6_, employee2_.personnelNumber as personne2_6_, employee2_.forename as forename6_, employee2_.surname as surname6_ from Events event0_ inner join Events_Employees employees1_ on event0_.id=employees1_.EventId inner join Employees employee2_ on employees1_.EmployeeId=employee2_.id


But when I add to the select another field, this happens:
HQL:
Code:
select e.employees, e.eventType from Event e

Generated SQL:
Code:
select . as col_0_0_, event0_.eventType as col_1_0_, employee2_.id as id6_, employee2_.personnelNumber as personne2_6_, employee2_.forename as forename6_, employee2_.surname as surname6_ from Events event0_ inner join Events_Employees employees1_ on event0_.id=employees1_.EventId inner join Employees employee2_ on employees1_.EmployeeId=employee2_.id


In the select statement, hibernate tries to fetch ". as col_0_0_" which is totally wrong...

Am I doing something wrong or why does this happen?

Regards,
Dominik


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 24, 2007 6:20 am 
Beginner
Beginner

Joined: Thu Feb 08, 2007 11:11 am
Posts: 24
Location: India
use Set inspite of List for employees


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 24, 2007 6:34 am 
Beginner
Beginner

Joined: Mon Mar 20, 2006 7:59 am
Posts: 30
Thanks for your answer, but it didn't help :-/ I changed the List into a Set, but still the same error occures :-(


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 24, 2007 6:44 am 
Beginner
Beginner

Joined: Thu Feb 08, 2007 11:11 am
Posts: 24
Location: India
can u upload ur code plz


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 24, 2007 7:50 am 
Beginner
Beginner

Joined: Mon Mar 20, 2006 7:59 am
Posts: 30
I'm not sure whether my code helps a lot. Let me just show you how I was using JPA-annotations to define the dependencies between my classes:

Code:
@Entity
@Table(name = "Events")
public class Event extends Persistent {

   /**
    * Versions-ID, die für die Serialisierung verwendet wird.
    */
   private static final long serialVersionUID = 7452522703278983008L;

   @Column(name = "EVENT_TIMESTAMP")
   private Date timestamp;

   @Enumerated(EnumType.STRING)
   private EventType eventType;

   // Kein Cascading
   @ManyToOne(optional = false, fetch = FetchType.LAZY)
   @JoinColumn(name = "MachineId", nullable = false)
   private Machine machine;

   // Kein Cascading
   @ManyToOne(optional = false, fetch = FetchType.LAZY)
   @JoinColumn(name = "OrderItemId", nullable = false)
   private OrderItem orderItem;

   @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinTable(name = "Events_Employees", joinColumns = @JoinColumn(name = "EventId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "EmployeeId", referencedColumnName = "id"))
   private List<Employee> employees = new ArrayList<Employee>();

   /**
    * Erstellt eine neue Instanz dieser Klasse.
    */
   public Event() {
   }
...
}


The class employee is quite boring:

Code:
@Entity
@Table(name = "Employees")
public class Employee extends Persistent {

   /**
    * Versions-ID, die für die Serialisierung verwendet wird.
    */
   private static final long serialVersionUID = 849339845014597020L;


   @Column(unique = true, nullable = false)
   private long personnelNumber;

   @Column(length = 50)
   private String forename;

   @Column(length = 50)
   private String surname;

   /**
    * Erstellt eine neue Instanz dieser Klasse.
    */
   public Employee() {
   }
...
}


This is the class I was using to execute my queries:
Code:

public class HqlTest {

   public static void main(String[] args) throws IOException {
      java.util.logging.Logger.getLogger("").setLevel(Level.ALL);
      java.util.logging.Logger.getLogger("org").setLevel(Level.INFO);
      java.util.logging.Logger.getLogger("sun").setLevel(Level.INFO);
      java.util.logging.Logger.getLogger("com").setLevel(Level.INFO);
      java.util.logging.Logger.getLogger("net").setLevel(Level.INFO);
      Logger.getLogger("org.hibernate.SQL").setLevel(Level.FINE);
      for (Handler handler : java.util.logging.Logger.getLogger("").getHandlers()) {
         handler.setLevel(Level.ALL);
      }

      
      HibernateDatabaseConfiguration config = new HibernateDatabaseConfiguration();
      config.setConnectionDriverClass("org.firebirdsql.jdbc.FBDriver");
      config.setConnectionUrl("jdbc:firebirdsql:localhost:c:\\Users\\dominik\\wvmonitor\\wvmonitor.fdb");
      config.setConnectionUsername("sysdba");
      config.setConnectionPassword("masterkey");
      config.setShowSql(true);
      config.setDialect("org.hibernate.dialect.FirebirdDialect");
      
      DaoFactory daoFactory = new DaoFactoryImpl(config);
      
      EntityManager em = daoFactory.createEntityManager();
      
      BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
      
      String s = input.readLine();
      while (!s.equals("QUIT")) {
         try {
            System.out.println("Executing: "+s);
            List result = em.createQuery(s).getResultList();
            for (Object obj : result) {
               if (obj instanceof Object[]) {
                  for (Object o : (Object[])obj) {
                     System.out.print(o.toString()+", ");
                  }
                  System.out.println();
               } else {
                  System.out.println(obj.toString());
               }
            }
         } catch (Exception e) {
            e.printStackTrace();
         }
         System.out.println("Nächste Query...");
         s = input.readLine();
      }
      
   }
   
}


A DaoFactory is a class that gives me access to the database either through DataAccessObjects or through an EntityManager. A HibernateDatabaseConfiguration configures hibernate to access a specific database.

I hope that helps finding the error.

Regards,
Dominik


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.