【问题标题】:jpa entity mappagin a different valuejpa entity mappagin 不同的值
【发布时间】:2013-04-15 09:34:10
【问题描述】:

我在表中有一个列:Status 具有以下值:P、A、E、C....

因为我在 jsp 页面上显示了这个值,所以我想将这个值映射到:P-> 处理,A->.... E-> 错误,C-> 完成

所以我做了一个用于映射值的类:

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

在实体中我这样做了:

 /**
  * 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;
}

    }

但表格总是显示: E、P、A、C…… 为什么??

为了完整性,这是jsp页面

  <%@ 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>

【问题讨论】:

  • 你可以展示你的jsp吗?
  • 是的,我编辑问题
  • 我想我没能理解问题,哪个表仍然显示 E、A、P...? jsp表OR数据库表?
  • Entity 从 Db 的表中获取值,Jsp 从 Entity 中获取值...
  • 是不是和你往map中插入数据的顺序有关,那么可以使用LinkedHashMap来保留插入顺序

标签: java jpa jsf-1.2


【解决方案1】:

我经常观察到,当访问特定字段时,hibernate不会调用您实体的getter

例如,如果status 是一个字段,并且有人尝试调用getStatus(),则hibernate 不会调用您实体的getStatus() 方法。

因此,最好在实体类中声明一个名为 longStatus(例如)的 transient 字段(未映射到列),并为其设置一个 getter,如下所示。

private transient longStatus;   

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

你可能需要修改你的jsp来访问entity.longStatus而不是entity.status.

【讨论】:

  • 你是个天才!!!请你解释一下为什么需要 Transient 关键字??
  • transient 关键字使休眠在映射时忽略该字段。如果一个字段被声明为transient,那么休眠将不会尝试将它映射到表中的任何列,实体指向。由于 longStatus 只是您的实体类中的一次性非持久字段,因此将其设置为 transient 是有意义的
  • 如果我再次回答这个问题,我很抱歉,但是.. 让它持久化什么剂量变化? (非持久性意味着它不存储在文件或某处,并且可以在运行时更改?)
猜你喜欢
  • 1970-01-01
  • 2016-08-27
  • 2014-06-15
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 2014-12-24
  • 1970-01-01
  • 2018-03-11
相关资源
最近更新 更多