简体   繁体   中英

jpa entity mappagin a different value

i have a column in a table: Status which has the following values: P, A, E, C....

because i show this values on a jsp page i would like to map this values to: P-> processing, A->.... E-> errror , C-> Completed

so i did this a class for mapping the values:

import java.util.HashMap;

public class FactoryStatusValue {

private static final HashMap<String, String> statusValues;

static{
    statusValues = new HashMap<String, String>(){{
        put("C","Completed");
        put("E","Errore");
        put("T","Terminato");
        put("P","Processing");
        put("N","Da processare");
        put("A","Attivà manuale");
    }};
}

public static String getValue(String key){
    return statusValues.get(key);
}
}

and in the entity i did this:

 /**
  * The persistent class for the SETT_TB_BATCHREQUEST database table.
  * 
  */
 @Entity
 @Table(name="SETT_TB_BATCHREQUEST")
 public class SettTbBatchrequest implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@SequenceGenerator(name="SETT_TB_BATCHREQUEST_UIDBATCHREQUEST_GENERATOR", sequenceName="SEQ_SETT_TB_BATCHREQUEST" , allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SETT_TB_BATCHREQUEST_UIDBATCHREQUEST_GENERATOR")
@Column(unique=true, nullable=false, precision=19)
private Long uidbatchrequest;

@Column(length=64)
private String note;

@Column(name="\"PARAMETER\"", length=1024)
private String parameter;

@Column(length=64)
private String pdr;

@Column(length=64)
private String plant;

@Column(precision=19)
private BigDecimal prognum;

@Column(length=64)
private String ratecode;

@Column(length=64)
private String relco;

@Temporal( TemporalType.DATE)
@Column(nullable=false)
private Date scheddate;

@Column(name="SETT_DISCO", nullable=false, length=64)
private String settDisco;

@Temporal( TemporalType.TIMESTAMP)
@Column(name="SETT_TIME")
private Date settTime;

@Column(name="SETT_USER", length=64)
private String settUser;

@Temporal( TemporalType.DATE)
private Date starttime;

@Column(nullable=false, length=1)
private String status;

@Temporal( TemporalType.TIMESTAMP)
private Date stoptime;

//bi-directional many-to-one association to SettTbBatchprocess
@ManyToOne
@JoinColumn(name="PROCESSNUM", nullable=false)
private SettTbBatchprocess settTbBatchprocess;

//bi-directional many-to-one association to SettTbStoricoRettifica
@OneToMany(mappedBy="settTbBatchrequest")
private List<SettTbStoricoRettifica> settTbStoricoRettificas;

public SettTbBatchrequest() {
}

public long getUidbatchrequest() {
    return this.uidbatchrequest;
}

public void setUidbatchrequest(long uidbatchrequest) {
    this.uidbatchrequest = uidbatchrequest;
}

public String getNote() {
    return this.note;
}

public void setNote(String note) {
    this.note = note;
}

public String getParameter() {
    return this.parameter;
}

public void setParameter(String parameter) {
    this.parameter = parameter;
}

public String getPdr() {
    return this.pdr;
}

public void setPdr(String pdr) {
    this.pdr = pdr;
}

public String getPlant() {
    return this.plant;
}

public void setPlant(String plant) {
    this.plant = plant;
}

public BigDecimal getPrognum() {
    return this.prognum;
}

public void setPrognum(BigDecimal prognum) {
    this.prognum = prognum;
}

public String getRatecode() {
    return this.ratecode;
}

public void setRatecode(String ratecode) {
    this.ratecode = ratecode;
}

public String getRelco() {
    return this.relco;
}

public void setRelco(String relco) {
    this.relco = relco;
}

public Date getScheddate() {
    return this.scheddate;
}

public void setScheddate(Date scheddate) {
    this.scheddate = scheddate;
}

public String getSettDisco() {
    return this.settDisco;
}

public void setSettDisco(String settDisco) {
    this.settDisco = settDisco;
}

public Date getSettTime() {
    return this.settTime;
}

public void setSettTime(Date settTime) {
    this.settTime = settTime;
}

public String getSettUser() {
    return this.settUser;
}

public void setSettUser(String settUser) {
    this.settUser = settUser;
}

public Date getStarttime() {
    return this.starttime;
}

public void setStarttime(Date starttime) {
    this.starttime = starttime;
}

public String getStatus() {
    return FactoryStatusValue.getValue(this.status);
}

public void setStatus(String status) {
    this.status = FactoryStatusValue.getValue(status);
}

public Date getStoptime() {
    return this.stoptime;
}

public void setStoptime(Date stoptime) {
    this.stoptime = stoptime;
}

public SettTbBatchprocess getSettTbBatchprocess() {
    return this.settTbBatchprocess;
}

public void setSettTbBatchprocess(SettTbBatchprocess settTbBatchprocess) {
    this.settTbBatchprocess = settTbBatchprocess;
}

public List<SettTbStoricoRettifica> getSettTbStoricoRettificas() {
    return this.settTbStoricoRettificas;
}

public void setSettTbStoricoRettificas(List<SettTbStoricoRettifica> settTbStoricoRettificas)         {
    this.settTbStoricoRettificas = settTbStoricoRettificas;
}

    }

but the table shows always : E, P, A, C..... why??

for completenss this is the jsp page

  <%@ taglib uri="http://www.sun.com/webui/webuijsf" prefix="webuijsf"%>
  <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
  <%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
  <%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
  <f:view>
<f:loadBundle basename="it.eni.italgas.sam.web.dictionary"
    var="bundle" />
<webuijsf:page id="page1"  rendered="#{UserBean.userEnabled}">
    <webuijsf:html id="html1">
        <webuijsf:head id="head1" parseOnLoad="false"
            title="Pagina di prova2" webuiOnLoad="false">
            <webuijsf:link id="link1" url="/resources/stylesheet.css" />
        </webuijsf:head>
        <webuijsf:body id="body1" style="-rave-layout: grid"
        styleClass="pageLayout">

        <webuijsf:form id="form1">
            <webuijsf:label id="label3" 
            style="font-size: 14px; left: 24px; top: 24px; position: absolute" 
            text="#{bundle.TitoloPaginaRisultatiRettifiche_02}"/>

            <webuijsf:button id="button3" actionExpression="#{guiRisultati_02.button1_action}" style="height: 24px; left: 48px; top: 120px; position: absolute; width: 96px" text="#{bundle.refresh}"/>

            <!-- inizio tabella -->
            <webuijsf:table id="table1" paginateButton="true"
                paginationControls="true"
                style="height: 52px; left: 48px; top: 168px; position: absolute; width: 1840px"
                title="#{bundle.TitoloTabella}">
                <webuijsf:tableRowGroup id="tableRowGroup2" rows="10"
                    sourceData="#{GUISessionBeanDisplayRett.provider}" sourceVar="richiesta">
                    <webuijsf:tableColumn headerText="#{bundle.UidBatchrequest}"
                        id="tableColumn13" sort="uidbatchrequest">
                        <webuijsf:staticText id="staticText11"
                            text="#{richiesta.value.uidbatchrequest}" />
                    </webuijsf:tableColumn>
                    <webuijsf:tableColumn headerText="#{bundle.Scheddate}"
                        id="tableColumn7" sort="scheddate">
                        <webuijsf:staticText id="staticText12"
                            converter="#{guiRisultati_02.sqlDateConverter1}"
                            text="#{richiesta.value.scheddate}" />
                    </webuijsf:tableColumn>
                    <webuijsf:tableColumn headerText="#{bundle.DataInizio}"
                        id="tableColumn5" sort="starttime">
                        <webuijsf:staticText id="staticText13"
                            converter="#{guiRisultati_02.sqlDateConverter1}"
                            text="#{richiesta.value.starttime}" />
                    </webuijsf:tableColumn>
                    <webuijsf:tableColumn headerText="#{bundle.DataFine}"
                        id="tableColumn6" sort="stoptime">
                        <webuijsf:staticText id="staticText14"
                            converter="#{guiRisultati_02.sqlDateConverter2}"
                            text="#{richiesta.value.stoptime}" />
                    </webuijsf:tableColumn>
                    <webuijsf:tableColumn headerText="#{bundle.Status}"
                        id="tableColumn8" sort="status">
                        <webuijsf:staticText id="staticText15"
                            text="#{richiesta.value.status}" />
                    </webuijsf:tableColumn>
                    <webuijsf:tableColumn headerText="#{bundle.SdV}"
                        id="tableColumn9" sort="relco">
                        <webuijsf:staticText id="staticText16"
                            text="#{richiesta.value.relco}" />
                    </webuijsf:tableColumn>
                    <webuijsf:tableColumn headerText="#{bundle.Impianto}"
                        id="tableColumn10" sort="plant">
                        <webuijsf:staticText id="staticText17"
                            text="#{richiesta.value.plant}" />
                    </webuijsf:tableColumn>
                    <webuijsf:tableColumn headerText="#{bundle.Pdr}"
                        id="tableColumn11" sort="pdr">
                        <webuijsf:staticText id="staticText18"
                            text="#{richiesta.value.pdr}" />
                    </webuijsf:tableColumn>
                    <webuijsf:tableColumn headerText="#{bundle.Parametri}"
                        id="tableColumn12" sort="parameter">
                        <webuijsf:staticText id="staticText19"
                            text="#{richiesta.value.parameter}" />
                    </webuijsf:tableColumn>

                    <webuijsf:tableColumn headerText="#{bundle.DataUltimaModifica}"
                        id="tableColumn200" sort="settTime">
                        <webuijsf:staticText id="staticText200"
                            converter="#{guiRisultati_02.sqlDateConverter2}"
                            text="#{richiesta.value.settTime}" />
                    </webuijsf:tableColumn>

                    <!-- colonne di modifica -->
                    <webuijsf:tableColumn id="tableColumn2" styleClass="buttonColumn">
                        <webuijsf:button actionExpression="#{guiRisultati_02.button4_action}"
                            id="button5" text="#{bundle.BottoneDettagli}">
                            <f:setPropertyActionListener
                                target="#{GUISessionBeanDisplayRett.uidBatchrequest}"
                                value="#{richiesta.value.uidbatchrequest}" />
                        </webuijsf:button>
                    </webuijsf:tableColumn>
                    <!-- parte colonna con eliminazione -->


                </webuijsf:tableRowGroup>
            </webuijsf:table>





        </webuijsf:form> 


        </webuijsf:body>
    </webuijsf:html>
</webuijsf:page>

I have often observed that hibernate doesn't invoke your entity's getter while accessing value for a particular field .

For example, if status is a field, and someone tries to call getStatus() , hibernate doesn't invoke your entity's getStatus() method.

So it would be better to declare a transient field (not mapped to a column) called longStatus (say) in your entity class and have a getter for it like below.

private transient longStatus;   

public String getLongStatus() {
  return FactoryStatusValue.getValue(this.status);
}

You may need to modify your jsp to access the entity.longStatus instead of entity.status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM