954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

HQL query in JAVA

hi I am working on HQL queries and writing an application in Java it is just in development phase so I am using a dummy database.
But I am having troubles with displaying result using HQL queries.
[code = java]

/**
     * Displays a report listing all companies that have no departments.
     */
    public void displayCompaniesWithoutDepartments() {
        System.out.println("---- All Companies Without Departments ----");
		Session session = sessions.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("select comp.id , comp.name from Department as dept left outer join dept.Company as comp");
		List result = q.list();
		for (Object object : result){
			Company company = (Company) object;
			System.out.println(company.getName());
			
		}
		session.getTransaction().commit();
        // Finish this
    }

I know some part of my function is incorrect but at first the query is not running it self I have search on google It seems to be in the right order but I dont understand what is the problem.

I get this error
Error: could not resolve property: company_id of: Department
when department has many to one relation ship with Company.

sfar_furqan
Junior Poster in Training
67 posts since Jan 2008
Reputation Points: 7
Solved Threads: 2
 
/**
     * Displays a report listing all companies that have no departments.
     */
    public void displayCompaniesWithoutDepartments() {
        System.out.println("---- All Companies Without Departments ----");
		Session session = sessions.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from Company c where c.id not in (select d.company from Department d)");
		List result = q.list();
		for (Object object : result){
			Company company = (Company) object;
			System.out.println(company.getName());
			
		}
		session.getTransaction().commit();
        
    }


Stupid me I was using wrong Query but it took me long to figure it out.

sfar_furqan
Junior Poster in Training
67 posts since Jan 2008
Reputation Points: 7
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: