2015-05-01 30 views
1

我在休眠中创建了一个多对一的映射。以下是设置请参考休眠中没有属性的外键

<many-to-one name="groups" class = "Groups" column="cgid" unique="true" not-null="true" cascade="all"/> 

在mysql中,这会创建一个名为cgid的列。

mysql> describe CONTACT 
    -> ; 
+-----------+--------------+------+-----+---------+-------+ 
| Field  | Type   | Null | Key | Default | Extra | 
+-----------+--------------+------+-----+---------+-------+ 
| IDCONTACT | bigint(20) | NO | PRI | NULL |  | 
| FIRSTNAME | varchar(255) | YES |  | NULL |  | 
| LASTNAME | varchar(255) | YES |  | NULL |  | 
| EMAIL  | varchar(255) | YES |  | NULL |  | 
| addressId | bigint(20) | NO | UNI | NULL |  | 
| cgid  | bigint(20) | NO | UNI | NULL |  | 
+-----------+--------------+------+-----+---------+-------+ 

现在,我需要基于cgid名称进行查询。

queryString = "from Contact where cgid = :id"; 
query = session.createQuery(queryString); 
query.setParameter("id", gd.getGid()); 
contactl = query.list(); 

休眠不断抱怨它

could not resolve property: cgid of: domain.Contact [from domain.Contact c where c.cgid = :id] 

不知道,什么可以做,以解决这个问题。有什么建议么 ?

+0

在HQL中只允许@Entity属性,而不是实际的DB列名。为'cgid'创建新属性(或)使用本机SQL查询。 – K139

回答

0

这里queryString = "from Contact where cgid = :id"您正在使用HQL.cgid是数据库列名称。您必须编写Contact类变量而不是cgid.And此变量必须与cgid映射。如果您使用本地SQL查询与hibernate,则可以使用数据库列名但用hql不能使用。