0

在我们的软件中,我们要求一个 UI 的显示名称可以根据用户进行配置。因此,一个用户想要显示一个表中的另一列,而另一个用户想要显示同一个表中的另一列。我想我可以使用 SqlResultSetMappings 和自动名称查询来做到这一点。display_name 列不在数据库中。我认为我可以将列名(根据用户选择)映射到 displayName。问题是,Jpa 2.0 在 Columnresult 注释中没有类型。所以显示名称返回字符。这是我的实体定义。在此示例中,用户希望在 UI 中显示 short_name。但是另一个用户可能想要在 UI 中显示名称。

@SqlResultSetMappings({
    @SqlResultSetMapping(name = "findAllByTypeList", entities = @EntityResult(entityClass = Element.class, fields = {
            @FieldResult(name = "id", column = "ID"), @FieldResult(name = "variant", column = "VARIANT_ID"),
            @FieldResult(name = "elementType", column = "ELEMENT_TYPE_ID"),
            @FieldResult(name = "name", column = "NAME"), 
            @FieldResult(name = "shortName", column = "SHORT_NAME"),
            @FieldResult(name = "displayName", column = "display_name")

    }), columns = {
            @ColumnResult(name = "display_name")
    })
})
 public class Element implements Serializable {
/**
 *
 */
private static final long serialVersionUID = 2892631423222044026L;

public Element () {
    super();
}

@Id
@Column(name = "ID")
private long id;
@ManyToOne
@JoinColumn(name = "VARIANT_ID")
private Variant variant;
@Column(name = "ELEMENT_TYPE_ID")
private int elementType;
@Column(name = "NAME")
private String name;
@Column(name = "SHORT_NAME")
private String shortName;
@Transient
private String displayName;

public long getId() {
    return id;
}

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

public Variant getVariant() {
    return variant;
}

public void setVariant(Variant variant) {
    this.variant = variant;
}

public ElementTypeConstants getElementType() {
    return ElementTypeConstants .getInstance(elementType);
}

public void setElementType(NetworkElementTypeConstants elementType) {
    this.elementType = elementType.getId();
}

public String getName() {
    return name;
}

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

public String getShortName() {
    return shortName;
}

public void setShortName(String shortName) {
    this.shortName = shortName;
}

public boolean equals(Object obj) {
    if (obj == null || !(obj instanceof Element)) {
        return false;
    }
    if (obj == this) {
        return true;
    }
    return ((Element) obj).getName().equals(this.getName());
}

public String toString() {
    StringBuffer buf = new StringBuffer("NE ID:");
    buf.append(getId());
    buf.append(", Name:");
    buf.append(getName());
    buf.append(", ShortName:");
    buf.append(getShortName());
    return buf.toString();
}

@Override
public int hashCode() {
    return Long.valueOf(id).intValue();
}

public String getDisplayName() {
    return displayName;
}

public void setDisplayName(String displayName) {
    this.displayName = displayName;
}

这是我的原生查询

/** adding table name */
        sqlQuery = "select a.id, a.variant_id, a.ELEMENT_TYPE_ID, a.name, a.short_name,  a.short_name as display_name from " + conf_element a;
    Query query = em.createNativeQuery(sqlQuery
                    + " where a.variant_id=:variantId and a.element_type_id in (:elementTypeIdList)  order by a.name",
            "findAllByTypeList");
    query.setParameter("variantId", variantId);
    query.setParameter("elementTypeIdList", elementTypeList);
    return query.getResultList();
    }

PS.:一开始,我认为我可以在不使用 ColumnResult 的情况下实现。以下行可以完成这项工作。

        @FieldResult(name = "displayName", column = "display_name")
4

0 回答 0