English 中文(简体)
JPA 根植于Hibernate,不与IdClass属性合作
原标题:JPA root get in Hibernate not working with IdClass attribute

I m trying to do a multiselect on an entity with an IdClass. I can t get a column that is mapped as part of the ID. It s clear why I can t, as none of the columns that are marked as @Ids are a part of the attributes in the EntityType that hibernate is creating, they are a part of the IdAttributes map.

这部法典在开放的JPA中做了罚款,但我已决定出于各种原因将这种行动转移到解放。

守则失败:

CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Tuple> query = queryBuilder.createTupleQuery();
Root<ProductSearchFilter> productSearchFilterRoot = query.from(ProductSearchFilter.class);
query.multiselect(productSearchFilterRoot.get("productId").alias("productId"),
            productSearchFilterRoot.get("category").alias("category"),
            productSearchFilterRoot.get("name").alias("name"),
            productSearchFilterRoot.get("fdaStatus").alias("fdaStatus"));
    query.distinct(true);

错误:

java.lang.IllegalArgumentException: Unable to resolve attribute [productId] against path

我的制图:

@Table(name = "PRODUCT_SEARCH_FILTER")
@Entity()
@IdClass(ProductSearchFilterPK.class)
public class ProductSearchFilter {

private String source;
private String productId;
private String name;
private String category;
private String searchColumn;
private String fdaStatus;

@Column(name = "SOURCE", length = 11)
public String getSource() {
    return source;
}

public void setSource(String source) {
    this.source = source;
}

@Column(name = "PRODUCT_ID", length = 46, insertable = false, updatable = false)
@Id
public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

@Column(name = "NAME", length = 510)
public String getName() {
    return name;
}

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

@Column(name = "CATEGORY", length = 10)
public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}


@Column(name = "SEARCH_COLUMN", length = 1088, insertable = false, updatable = false)
@Id
public String getSearchColumn() {
    return searchColumn;
}

public void setSearchColumn(String searchColumn) {
    this.searchColumn = searchColumn;
}

@Column(name = "FDA_STATUS", insertable = false, updatable = false)
@Id
public String getFdaStatus() {
    return fdaStatus;
}

public void setFdaStatus(String fdaStatus) {
    this.fdaStatus = fdaStatus;
}
}



public class ProductSearchFilterPK implements Serializable {
private String productId;
private String searchColumn;
private String fdaStatus;

public String getFdaStatus() {
    return fdaStatus;
}

public void setFdaStatus(String fdaStatus) {
    this.fdaStatus = fdaStatus;
}

public String getProductId() {
    return productId;
}

public void setProductId(String productId) {
    this.productId = productId;
}

public String getSearchColumn() {
    return searchColumn;
}

public void setSearchColumn(String searchColumn) {
    this.searchColumn = searchColumn;
}
}
最佳回答

There is workaround, which is using canonical metamodel.

Unfortunately EntityModel we get from productSearchFilterRoot does not help us much, because it only gives us view as a set. So we cannot query Attributes of IdClass right away by name of attribute from there. But there they are anyway:

//following will contain three attributes that are part of id.
Set<SingularAttribute<? super ProductSearchFilter, ?>> s = model.getIdClassAttributes();

Instead we we will go for canonical metamodel. For that we need one new class which we can implement by ourselves, or let Hibenate do it:

@StaticMetamodel(ProductSearchFilter.class)
public abstract class ProductSearchFilter_ {
    public static volatile SingularAttribute<ProductSearchFilter, String> category;
    public static volatile SingularAttribute<ProductSearchFilter, String> fdaStatus;
    public static volatile SingularAttribute<ProductSearchFilter, String> source;
    public static volatile SingularAttribute<ProductSearchFilter, String> name;
    public static volatile SingularAttribute<ProductSearchFilter, String> searchColumn;
    public static volatile SingularAttribute<ProductSearchFilter, String> productId;
}

然后,我们将利用产品SearchFilter领域作为在产品SearchFilterRoot的论据:

CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Tuple> query = queryBuilder.createTupleQuery();
Root<ProductSearchFilter> productSearchFilterRoot = query.from(ProductSearchFilter.class);
query.multiselect(
    productSearchFilterRoot.get(ProductSearchFilter_.productId).alias("productId"),
    productSearchFilterRoot.get(ProductSearchFilter_.category).alias("category"),
    productSearchFilterRoot.get(ProductSearchFilter_.name).alias("name"),
    productSearchFilterRoot.get(ProductSearchFilter_.fdaStatus).alias("fdaStatus"));
query.distinct(true);

由于我们现在已经开会,我也利用会议来为名字进行挑选,但由于其中一人不是id的一部分,我们本可以离开,因为它是:

productSearchFilterRoot.get("name").alias("name"),
问题回答

暂无回答




相关问题
Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Assistance with CakePHP model relationships

How would I represent the following in a CakePHP model? Product ======= product_id .... Cart ==== cart_id .... Carts_Products ============== cart_id product_id quantity

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How are Models (in MVC) and DAOs supposed to interact?

How are models and DAOs supposed to interact? I m in the process of putting together a simple login module and I m unsure where to put the "business logic." If I put the logic with the data in the ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签