如何在Hibernate中运行join HQL查询?我已将值插入到一对一的关系表中。但在我插入表之前,我需要检查值是否存在。如果退出我需要该记录的id。之后我已插入orderdetails表。
所以,我需要运行SQL来从表中获取ID。
表:产品
Product_id
product_name
表:版本
version_id
product_id
date
所以如果记录存在,我需要找到version_id。我想运行SQL:select version_id from product p,version v where p.product_id=v.product_id and product_name='stackoverflow'
我试过这种方式..
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());
}
但是上面的代码给出了错误。
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)
产品类别:
@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;
}
}
版本:
@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;
}
请告知如何在hibernate中执行上述查询?
答案 0 :(得分:0)
首先,在这种情况下,您的类名是Part而不是Product。 因此,您必须在查询中使用Part。
其次我不确定是否有一个能够生成的hibernate id生成器 字符串序列,所以你必须分配一个自定义的id生成器。
此外,您发布的代码并未显示Version类及其类别 与产品或部件的关联。
在我看来,尽管存在id问题,但您的查询应该如下所示
select v.version_id
from FeatureVersion v, Part p
where v.product.product_id = p.product_id
and p.product_name='"+item+"
执行该查询时。 Hibernate创建一个类型为FeaturedVersion的instamce,并根据关联的表列初始化其属性。
尽量避免使用这种语句。请改用PreparedStatements来传递 查询的参数并使用命名参数左右将它们分配给查询。
尝试使用符合Java和JavaBeans约定的属性和类名 意味着避免低调等等。因此,阅读查询更简单,您可以避免错误搜索。
答案 1 :(得分:0)
您的映射很糟糕。 product_id
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
}
在另一堂课。
@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;
}
}
在客户端代码中,查询字符串如下所示:
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
一些建议:
1)遵循java代码约定,例如将product_name
更改为productName
或仅name
。
2)在数据库中调用与约定相同名称的类。
3)对于字符串,我会使用like
子句而不是=如果你不想要完全匹配。