I'm getting a ORA-00904: Invalid Identifier when trying to retrieve all items from a table which would appear to be due to an alias being used in the Where clause hibernate generates.
The issue appears to be related to this ->
http://opensource.atlassian.com/project ... se/HHH-817 which is set to a minor bug so I I'm not sure if I am simply not understanding a simple workaround?
The basic data access code is all generated using salto-db (which is based on the pattern descibed here ->
http://www.hibernate.org/328.html) and does not (as far as I understand it) use a projection as described in the bug report. The code I am running (see blow) is just passing an empty Criteria in order to return all items.
Can anyone help me determine how to avoid this issue? If it is due to the structure of the data model there is an opportunity to alter it should that make my life easier.
The tables in question are
Location
- name (PK)
- other params
and
RouteSection
- location from ( FK to location name)
- location to( FK to location name)
- other params
Thanks
Tom
Hibernate version:
Hibernate 3.3.1.GA
Mapping documents:
- Annotated Class - Originally generated by salto-db-generator, but with a few tweaks since it would not compile due to the two Lists of ModelRouteSection which were generated with the same parameter name (I added the From and To)
Code:
package uk.co.networkrail.elmtree.dataaccesslayer.network.pojo;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* <p>Pojo mapping TABLE ELMTREE_NETWORKMOD_OWNER_TDP.LOCATION</p>
*
* <p>Generated at Mon Dec 29 16:17:42 GMT 2008</p>
* @author Salto-db Generator v1.0.16 / EJB3
*
*/
@Entity
@Table(name = "LOCATION")
@SuppressWarnings("serial")
public class ModelLocation implements Serializable
{
/**
* Attribute name.
*/
private String name;
/**
* Attribute modelLocationType
*/
private ModelLocationType modelLocationType;
/**
* Attribute elr.
*/
private String elr;
/**
* Attribute miles.
*/
private Integer miles;
/**
* Attribute chains.
*/
private Integer chains;
/**
* List of ModelLocationKvp
*/
private List<ModelLocationKvp> modelLocationKvps = null;
/**
* List of ModelRouteSection
*/
private List<ModelRouteSection> modelRouteSectionsTo = null;
/**
* List of ModelRouteSection
*/
private List<ModelRouteSection> modelRouteSectionsFrom = null;
/**
* @return name
*/
@Basic
@Id
@Column(name = "name", length = 50)
public String getName()
{
return name;
}
/**
* @param name new value for name
*/
public void setName(String name)
{
this.name = name;
}
/**
* get modelLocationType
*/
@ManyToOne
@JoinColumn(name = "location_type_id")
public ModelLocationType getModelLocationType()
{
return this.modelLocationType;
}
/**
* set modelLocationType
*/
public void setModelLocationType(ModelLocationType modelLocationType)
{
this.modelLocationType = modelLocationType;
}
/**
* @return elr
*/
@Basic
@Column(name = "ELR", length = 50)
public String getElr()
{
return elr;
}
/**
* @param elr new value for elr
*/
public void setElr(String elr)
{
this.elr = elr;
}
/**
* @return miles
*/
@Basic
@Column(name = "miles")
public Integer getMiles()
{
return miles;
}
/**
* @param miles new value for miles
*/
public void setMiles(Integer miles)
{
this.miles = miles;
}
/**
* @return chains
*/
@Basic
@Column(name = "chains")
public Integer getChains()
{
return chains;
}
/**
* @param chains new value for chains
*/
public void setChains(Integer chains)
{
this.chains = chains;
}
/**
* Get the list of ModelLocationKvp
*/
@OneToMany(mappedBy="modelLocationKvpPK.modelLocation")
public List<ModelLocationKvp> getModelLocationKvps()
{
return this.modelLocationKvps;
}
/**
* Set the list of ModelLocationKvp
*/
public void setModelLocationKvps(List<ModelLocationKvp> modelLocationKvps)
{
this.modelLocationKvps = modelLocationKvps;
}
/**
* Get the list of ModelRouteSection
*/
@OneToMany(mappedBy="modelLocationTo")
public List<ModelRouteSection> getModelRouteSectionsTo()
{
return this.modelRouteSectionsTo;
}
/**
* Set the list of ModelRouteSection
*/
public void setModelRouteSectionsTo(List<ModelRouteSection> modelRouteSections)
{
this.modelRouteSectionsTo = modelRouteSections;
}
/**
* Get the list of ModelRouteSection
*/
@OneToMany(mappedBy="modelLocationFrom")
public List<ModelRouteSection> getModelRouteSectionsFrom()
{
return this.modelRouteSectionsFrom;
}
/**
* Set the list of ModelRouteSection
*/
public void setModelRouteSectionsFrom(List<ModelRouteSection> modelRouteSections)
{
this.modelRouteSectionsFrom = modelRouteSections;
}
}
Code between sessionFactory.openSession() and session.close():Code:
public List<Location> getLocations() throws NetworkModelItemRollbackException
{
List<Location> locations = null;
// Create a session and start a transaction
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
try
{
// Get the DAO and return all locations
ModelLocationHibernateDAO locationManager = (ModelLocationHibernateDAO) DAOFactory.DEFAULT.buildModelLocationDAO();
locationManager.setSession(session);
locations = buildLocations(locationManager.findAll());
transaction.commit();
} catch (HibernateException he)
{
transaction.rollback();
NetworkModelItemRollbackException rollback = new NetworkModelItemRollbackException("Failed to retrieve all Locations", he);
mLogger.logMessage(rollback, LogLevel.ERROR);
throw rollback;
} finally
{
// Always close the session if it is open
if (session.isOpen())
{
session.close();
}
}
return locations;
}
locationManager.findAll() code shown below.....
Code:
public List<T> findAll()
{
return findByCriteria();
}
/**
* Use this inside subclasses as a convenience method.
*/
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(Criterion... criterion)
{
Criteria crit = getSession().createCriteria(getPersistentClass());
for (Criterion c : criterion)
{
crit.add(c);
}
return crit.list();
}
Full stack trace of any exception that occurs:1876 [main] ERROR org.hibernate.util.JDBCExceptionReporter - ORA-00904: "MODELLOCAT2_"."LOCATION_TYPE_ID": invalid identifier
Name and version of the database you are using:Oracle 10.2
The generated SQL (show_sql=true):Code:
Hibernate:
select this_.name as name0_1_,
this_.chains as chains0_1_,
this_.ELR as ELR0_1_,
this_.miles as miles0_1_,
this_.location_type_id as location5_0_1_,
modellocat2_.location_type_id as location1_2_0_,
modellocat2_.description as descript2_2_0_
from LOCATION this_, LOCATION_TYPE modellocat2_
where this_.location_type_id=modellocat2_.location_type_id(+)
Debug level Hibernate log excerpt:
(Not sure what I can provide here above that already shown above)