@ColumnResult maps to Character instead of String in JPA 2.0

640 views Asked by At

In our software, we had a requirement that display name of one UI will be configurable according to user. So one user want to display another column from one table and another user want to display another column from same table. I think I can do this using SqlResultSetMappings and automatic name query. display_name column is not in database . I think that I can map column name(according to user selection) to displayName. The problem, Jpa 2.0 does not have type in Columnresult annotation. So display name returns Character. This is my entity definition. In this eaxample user want to display short_name in the UIs. But another user can want to show name in the UIs.

@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;
}

This is my native query

/** 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.: In the beginning, I think that I can implement without using ColumnResult.Following line can do the job.

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

There are 0 answers