0

How to run the join HQL query in Hibernate ? I have inserted the values into one-to-one relationship tables.But before i'm inserting into tables, i need to check if the value is exist is not. If exit i need id for that record.After that i have insert into orderdetails table.

So , i need run the SQL to get the ID from table.

Table : product

Product_id
product_name

Table : version

version_id
product_id
date

So i need to find version_id if record exist. I would like to run the SQL : select version_id from product p,version v where p.product_id=v.product_id and product_name='stackoverflow'

I've tried this way..

Query q=session.createQuery("from Product p,Version v where p.product_id=v.product_id and productname='"+item+"'");
if(query.list().size()>0){
for(Iterator it=query.iterate();it.hasNext();)
{
Version std = ( Version) it.next();
System.out.println("partid: " + std.getversion_id());
V.setversion_id(std.getversion_id());
 }

But above code giving error.

org.hibernate.QueryException: could not resolve property: product_id of: Version [from Product p,Version v where p.product_id=v.product_id and productname='stack']
    at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81)
    at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75)
    at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1465)
    at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:315)
    at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:487)
    at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:611)
    at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:263)
    at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:210)
    at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117)
    at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113)
    at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:880)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1330)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4471)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3947)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2047)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1972)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:831)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:617)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:256)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
    at com.import(CompleteNotice.java:764)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

Product Class :

@Entity
@Table(name="DBO.PRODUCT")
public class Product{

    private String product_id;
    private String product_name

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="PRODUT_ID")
    public String getProduct_id() {
        return product_id;
    }

    public void setProduct_id(String product_id) {
        this.product_id = product_id;
    }

    @Column(name="PRODUCT_NAME")
    public String getProduct_name() {
        return product_name;
    }

    public void setProduct_name(String product_name) {
        this.product_name = product_name;
    }


}

Version :

@Entity
@Table(name="DBO.VERSION")
public class Version {

    private int version_id;
    private int product_id;
    private Product product;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="VERSION_ID")
    public int getVersion_id() {
        return version_id;
    }

    public void setVersion_id(int Version_id) {
        this.version_id = version_id;
    }


    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="product_id")
    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

Please advise how to execute above query in hibernate ?

11
  • In your table, a product can have one-to-many versions
    – nachokk
    Dec 13, 2013 at 18:24
  • the first thing is that there is a missing qhitespace between product and p.
    – Diversity
    Dec 13, 2013 at 18:28
  • Our business version products are different. We have few more tables there to have one-to-many relationship tables for version.Here apply only one-to-one Dec 13, 2013 at 18:28
  • What kind of error is thrown?
    – Diversity
    Dec 13, 2013 at 18:28
  • could not resolve property: product_id of: Version [from Product p,Version v where p.product_id=v.product_id and productname='stack'] Dec 13, 2013 at 18:33

2 Answers 2

0

At first your class name in this case is Part and not Product. So you have to use also Part within your query.

Secondly i am not sure if there is a hibernate id generator which is able to generate string sequences so you have to assign an customized id generator.

Furthermore the code you have posted doesn't show the Version class and it's association to Product or part.

In my opinion and despite of the id problem your query should look like this

select v.version_id 
from FeatureVersion v, Part p
where v.product.product_id = p.product_id
and p.product_name='"+item+"

When you execute that query. Hibernate creates an instamce of type FeaturedVersion and initializes its properties according to the associated table columns.

Try to avoid using this kind of statements. Use PreparedStatements instead to pass parameters to the query and use named parameters or so to assign them to the query.

Try to use property and class names which meet the Java and JavaBeans convention and Means avoid underscorse and so on. So its simpler to read the query and you avoid frustrating error searches.

4
  • SQL generater generates id for both tables.Because inserting the records perfectly. Now i want to check if particular item alredy exist or not only. Dec 13, 2013 at 18:58
  • Assum Product and version as tables. So query is going to be Select v.version_id from Product p,Version v where v.product_id = p.product_id and name='item'. Dec 13, 2013 at 19:16
  • Please correct me, if i'm wrong. Because again i'm getting the same error. Dec 13, 2013 at 19:17
  • a HQL query differs from SQL in that way that it directly associated with the objects of the Entity classes. Hibernate resolves the mapping to tables and colums within database on it's own So within your query you have to use the classnames and the appropriate property name. References or relationships you resolve using the reference name in this case product followed by a dot and the appropriate proeprty of this class
    – Diversity
    Dec 13, 2013 at 19:21
0

Your mapping is bad. You don't need product_id in FeatureVersion

@Entity
@Table(name="DBO.PRODUCT")
public class Part { // why not call Product..

    private String product_id;
    private String product_name;
    private FeatureVersion version;    

    @OneToOne(mappedBy="product")
    public FeatureVersion getFeatureVersion(){
        return featureVersion;
    }    
      //put others getters and setter 
 }

And in the other class.

@Entity
@Table(name="DBO.VERSION")
public class FeatureVersion {

    private int version_id;
    private Product product;

    //add setter and getter that i leave

   @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="product_id", referencedColumnName= "product_id")
    public Product getProduct() {
        return product;
    }

}

And in client code the query string look like:

String hql = "FROM Part p fetch join p.version Where p.product_name = :myParam" // this is cause you put it lazy if you don't put anything it will be eager 
session.createQuery(hql).setParameter("myParam", productName).uniqueResult() // or list

Some advices:

1)Follow java code conventions for example change product_name to productName or just name. 2) Call classes with the same name in database as convention.

3) For strings i would use like clause rather than = if you don't want exact match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.