@ColumnResult在JPA 2.0中映射为Character而不是String

时间:2018-11-20 08:07:29

标签: java jpa nativequery sqlresultsetmapping

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

@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")

0 个答案:

没有答案