-->
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.  [ 6 posts ] 
Author Message
 Post subject: I don't understand why this doesn't work
PostPosted: Wed Dec 14, 2005 8:19 am 
Newbie

Joined: Thu Dec 01, 2005 6:32 am
Posts: 5
Hy, guys!I need some help.I have two classes, telefone and endereco, wich have a one-to-many association.When I try to use a query like this:

Query query2 = session.createQuery("select tel from roseindia.tutorial.hibernate.Telefone as tel,roseindia.tutorial.hibernate.Endereco as end where tel.endereco.id = end.id and end.id = "+2);
I got this error:

org.hibernate.hql.ast.QuerySyntaxError: unexpected token: = near line 1, column 129 [select tel from roseindia.tutorial.hibernate.Telefone as tel,roseindia.tutorial.hibernate.Endereco as end where tel.endereco.id = end.id and end.id = 2]

the xml mappings are this:
endereco:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="roseindia.tutorial.hibernate.Endereco" table="ENDERECO">
    
   <id name="id" type="long" column="CONTACT" >
   <generator class="foreign" >
   <param name="property">cliente</param>
    </generator>
   </id>
   
      <property name="rua">
      <column name="RUA"/>
     </property>
     <property name="bairro">
      <column name="BAIRRO"/>
     </property>
     <property name="complemento">
      <column name="COMPLEMENTO"/>
     </property>
     <property name="numero">
      <column name="NUMERO"/>
     </property>

   <one-to-one name="cliente" class="roseindia.tutorial.hibernate.Cliente" constrained="true" />

   <set name="telefones" >
   <key column="Endereco" />
   <one-to-many class="roseindia.tutorial.hibernate.Telefone" />
   </set>   


</class>

</hibernate-mapping>


telefone:

Code:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="roseindia.tutorial.hibernate.Telefone" table="TELEFONES">
    
   <id name="id" type="long" column="ID" >
      <generator class="increment" />
   </id>
   
      <property name="telefone">
      <column name="TELEFONE"/>
     </property>

     <many-to-one name="Endereco" class="roseindia.tutorial.hibernate.Endereco" />

</class>

</hibernate-mapping>



the classe's codes are this:

endereco:

Code:

package roseindia.tutorial.hibernate;

import java.io.Serializable;
import java.util.Set;

public class Endereco implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = 1L;

   private long id;

   private String rua;

   private String bairro;

   private String complemento;

   private long numero;
   
   private Cliente cliente ;
   
   private Set telefones;

   /**
    * @return Recupera o atributo <tt>cliente</tt> desta classe.
    */
   
   public Cliente getCliente() {
      return cliente;
   }

   /**
    * @param cliente Novo valor para o atributo <tt>cliente</tt> desta classe.
    */
   
   public void setCliente(Cliente cliente) {
      this.cliente = cliente;
   }

   /**
    * @return Recupera o atributo <tt>telefones</tt> desta classe.
    */
   
   public Set getTelefones() {
      return telefones;
   }

   /**
    * @param telefones Novo valor para o atributo <tt>telefones</tt> desta classe.
    */
   
   public void setTelefones(Set telefones) {
      this.telefones = telefones;
   }

   /**
    * @return Recupera o atributo <tt>contact</tt> desta classe.
    */
   
   public Cliente getContact() {
      return cliente;
   }

   /**
    * @param contact Novo valor para o atributo <tt>contact</tt> desta classe.
    */
   
   public void setContact(Cliente contact) {
      this.cliente = contact;
   }

   /**
    * @return Recupera o atributo <tt>bairro</tt> desta classe.
    */

   public String getBairro() {
      return bairro;
   }

   /**
    * @param bairro
    *            Novo valor para o atributo <tt>bairro</tt> desta classe.
    */

   public void setBairro(String bairro) {
      this.bairro = bairro;
   }

   /**
    * @return Recupera o atributo <tt>complemento</tt> desta classe.
    */

   public String getComplemento() {
      return complemento;
   }

   /**
    * @param complemento
    *            Novo valor para o atributo <tt>complemento</tt> desta
    *            classe.
    */

   public void setComplemento(String complemento) {
      this.complemento = complemento;
   }

   /**
    * @return Recupera o atributo <tt>id</tt> desta classe.
    */

   public long getId() {
      return id;
   }

   /**
    * @param id
    *            Novo valor para o atributo <tt>id</tt> desta classe.
    */

   public void setId(long id) {
      this.id = id;
   }

   /**
    * @return Recupera o atributo <tt>numero</tt> desta classe.
    */

   public long getNumero() {
      return numero;
   }

   /**
    * @param numero
    *            Novo valor para o atributo <tt>numero</tt> desta classe.
    */

   public void setNumero(long numero) {
      this.numero = numero;
   }

   /**
    * @return Recupera o atributo <tt>rua</tt> desta classe.
    */

   public String getRua() {
      return rua;
   }

   /**
    * @param rua
    *            Novo valor para o atributo <tt>rua</tt> desta classe.
    */

   public void setRua(String rua) {
      this.rua = rua;
   }

}




telefone:

Code:

package roseindia.tutorial.hibernate;

import java.io.Serializable;

public class Telefone implements Serializable {

   private String telefone;

   private long id;

   private Endereco endereco;

   /**
    * @return Recupera o atributo <tt>endereco</tt> desta classe.
    */

   public Endereco getEndereco() {
      return endereco;
   }

   /**
    * @param endereco
    *            Novo valor para o atributo <tt>endereco</tt> desta classe.
    */

   public void setEndereco(Endereco endereco) {
      this.endereco = endereco;
   }

   /**
    * @return Recupera o atributo <tt>id</tt> desta classe.
    */

   public long getId() {
      return id;
   }

   /**
    * @param id
    *            Novo valor para o atributo <tt>id</tt> desta classe.
    */

   public void setId(long id) {
      this.id = id;
   }

   /**
    * @return Recupera o atributo <tt>telefone</tt> desta classe.
    */

   public String getTelefone() {
      return telefone;
   }

   /**
    * @param telefone
    *            Novo valor para o atributo <tt>telefone</tt> desta classe.
    */

   public void setTelefone(String telefone) {
      this.telefone = telefone;
   }

}




what is wrong with my query????


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 1:20 pm 
Newbie

Joined: Wed Dec 14, 2005 5:48 am
Posts: 4
Hi,

Something you can do, is to set the "id" in your request that way:

Code:
Query query2 = session.createQuery("select tel from roseindia.tutorial.hibernate.Telefone as tel,roseindia.tutorial.hibernate.Endereco as end where tel.endereco.id = end.id and end.id = :idInQuery");

query2.setLong("idInQuery", 2);


I don't know if your problem is coming from that syntax but this is start.

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 2:38 pm 
Beginner
Beginner

Joined: Wed Nov 30, 2005 2:41 pm
Posts: 29
what db are you using? I don't know HQL that well, but could it be the case? That is, maybe it has to be "where tel.Endereco.id =" since class & property names are case-sensitive.

Note that column 129 (where the error is flagged) is the "=" after tel.enereco.id.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 7:24 am 
Newbie

Joined: Thu Dec 01, 2005 6:32 am
Posts: 5
I already tried this, nut I still got the same error.Did anyone knows what is wrong??please, help!!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 9:35 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Endereco 1--* Telefone

So you seem to want the Telefones for a particular Endereco ?
Speaking objet is so easy... HQL is easy too

select end.telefones
from Endereco end
where end.id = :id


or

select tel
from Telefone tel
where tel.endereco.id = :id

play with HQL, you'll see how powerfull it is !

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 15, 2005 1:30 pm 
Beginner
Beginner

Joined: Thu Apr 07, 2005 5:12 pm
Posts: 27
Location: Hamburg
"Query query2 = session.createQuery("select tel from roseindia.tutorial.hibernate.Telefone as tel,roseindia.tutorial.hibernate.Endereco as end where tel.endereco.id = end.id and end.id = "+2); "

You are probably not allowed to say "as end, because it may be reserved by underlaying database system. Try call it endState or whatever.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.