- This topic has 4 replies, 3 voices, and was last updated 18 years, 5 months ago by pablogmuller.
-
AuthorPosts
-
pablogmullerMemberHi,
I have an hibernate mapping of a table that doesn¡t hace a PK, so myeclipse generates the id object that contains all the attributes. When I want to query it, I get this error
org.hibernate.hql.ast.QuerySyntaxError: expecting IDENT, found ‘.’ near line 1, column 56 [from com.movilogic.hibernate.mappings.Planes p where p..codigo_servicio = ’12’ ]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:215)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at com.movilogic.business.Prueba.main(Prueba.java:144)
Caused by: line 1:56: expecting IDENT, found ‘.’
at antlr.Parser.match(Parser.java:213)
at org.hibernate.hql.antlr.HqlBaseParser.identifier(HqlBaseParser.java:461)
at org.hibernate.hql.antlr.HqlBaseParser.atom(HqlBaseParser.java:3043)
at org.hibernate.hql.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:2806)
at org.hibernate.hql.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:2687)
at org.hibernate.hql.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2407)
at org.hibernate.hql.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:481)
at org.hibernate.hql.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2195)
at org.hibernate.hql.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2057)
at org.hibernate.hql.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2020)
at org.hibernate.hql.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:1937)
at org.hibernate.hql.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:1901)
at org.hibernate.hql.antlr.HqlBaseParser.expression(HqlBaseParser.java:1663)
at org.hibernate.hql.antlr.HqlBaseParser.logicalExpression(HqlBaseParser.java:1834)
at org.hibernate.hql.antlr.HqlBaseParser.whereClause(HqlBaseParser.java:376)
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:617)
at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:263)
at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:150)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:209)
… 7 more
This is the code:
List planes=null;
try{
planes = SessionFactory.currentSession().createQuery(“from Planes p where p..codigo_servicio = ’12’ “).list();
}catch (Exception e)
{
e.printStackTrace();
}
if (planes != null)
{
Planes p = (Planes)planes.get(0);
System.out.println(“plan: “+p.getId().getCodigoServicio());
}
I couldn’t find anything on the net about this. How do I query tables that don’t have PK with HQL. Including PK is not an option.
Thanks a lot.
Pablo
Riyad KallaMemberI don’t think Hibernate can map tables without PKs… in those situations you need to make every field combined the PK I think, that is the default behavior of our generator I believe.
pablogmullerMemberThanks for the reply!!
Yes, that’s exactly what it’s doing, it generates an ID object that’s the only attribute of my mapping and this ID object has all the attributes of the table. So I have to set all of them, and then set the ID attribute to the mapped object. For example, for my planes table I get:Planes (That has the constructors)
AbstractPlanes (which has a PlanesId member)
PlanesId (where the actual data is stored)So, if my attributes are in PlanesId, is there any way to do this query in HQL or I have to use session.createSqlQuery()?
I can’t change the table definition.
Thanls again.
Pablo
Haris PecoMemberPablo,
Reverse engineering can’t decide about your hibernate ID (PK in database terminology) when you haven’t PK in table and it choose all columns in identifier (it is similar for database view).Problems is that hibernate doesn’t accept null values in ID (PK) and it is your problem (I suppose)
You can change identifier direct in mapping file (unfortunately you can’t choose ID in mappings wizard, for now) or use native queries (createSqlQuery) – However, you will have problems with bad identifier in a lot places in hibernate (saving,updating, querying …) and it is the best for you that cohos your non-null identifier (all columns in ID have to be not-null and it have to be unique in session/database).It is not requested that PK exists in database.
Best regards
PecoPS
I apologize If your name is not Pablo
pablogmullerMemberThanks a lot,
I made it work… finally 🙂 -
AuthorPosts