如何创建内部HQL

时间:2014-12-13 09:41:18

标签: java hibernate hql

我尝试使用HQL查询子查询中的数据。我有2个班:

Supplier.java:

@Entity
@Table(name = "Suppliers")
public class Supplier implements Serializable {

@Id
String id;
String name;

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "Suppliers_Categories", joinColumns = { @JoinColumn(name = "SupplierId") }, inverseJoinColumns = { @JoinColumn(name = "CategoryId") })
Set<Category> categories = new HashSet<Category>();

@OneToMany(mappedBy = "supplier")
Collection<Product> products;

public Set<Category> getCategories() {
    return this.categories;
}

public void setCategories(Set<Category> categories) {
    this.categories = categories;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Collection<Product> getProducts() {
    return products;
}

public void setProducts(Collection<Product> products) {
    this.products = products;
}
}



Category.java:

@Entity
@Table(name = "Categories")
public class Category implements Serializable {
@Id
@GeneratedValue
Integer id;
String name;
String namevn;

@ManyToMany(mappedBy = "categories")
Set<Supplier> suppliers = new HashSet<Supplier>(0);

@OneToMany(mappedBy = "category")
Collection<Product> products;

@OneToOne
@JoinColumn(name = "ProductFeature")
Product featureProduct;

public Set<Supplier> getSuppliers() {
    return this.suppliers;
}

public Product getFeatureProduct() {
    return featureProduct;
}

public void setFeatureProduct(Product featureProduct) {
    this.featureProduct = featureProduct;
}

public String getNamevn() {
    return namevn;
}

public void setNamevn(String namevn) {
    this.namevn = namevn;
}

public void setSuppliers(Set<Supplier> suppliers) {
    this.suppliers = suppliers;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Collection<Product> getProducts() {
    return products;
}

public void setProducts(Collection<Product> products) {
    this.products = products;
}
}

我需要的是选择&#34; supplier.id&#34;在id = 1的类别中。我的查询是:

  

SELECT id FROM Supplier as s where s in(SELECT供应商FROM Category as c WHERE c.id = 1)

但它有一些错误:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2231)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
    at org.hibernate.loader.Loader.list(Loader.java:2120)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at eshop.util.XHibernate.list(XHibernate.java:85)
    at eshop.util.XHibernate.main(XHibernate.java:91)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '-'.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1458)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:388)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:338)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4016)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1414)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:176)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:151)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:281)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
    at org.hibernate.loader.Loader.doQuery(Loader.java:697)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2228)
    ... 9 more

有没有办法用1个hql查询我的想法?任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

您需要了解联接。你所需要的只是

select s.id from Supplier s
join s.categories c
where c.id = :categoryId