2016-03-23 125 views
0

jsf single dataTable。我想從不同實體類的數據庫表列中獲取值。一個實體類是IASLABELS主鍵是LANG_NO和LABELS_NO,另一個實體類是LANGDEF主鍵是LANG_NO。使用netbeans從dataBase實現一對一映射實體類jpa

我需要jsf dataTable列中的LANG_NAME。

@Entity 
@Table(name = "IAS_LABELS") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "IasLabels.findAll", query = "SELECT i FROM IasLabels i"), 
    @NamedQuery(name = "IasLabels.findByLangNo", query = "SELECT i FROM IasLabels i WHERE i.iasLabelsPK.langNo = :langNo"), 
    @NamedQuery(name = "IasLabels.findByLabelNo", query = "SELECT i FROM IasLabels i WHERE i.iasLabelsPK.labelNo = :labelNo"), 
    @NamedQuery(name = "IasLabels.findByCaptionDet", query = "SELECT i FROM IasLabels i WHERE i.captionDet = :captionDet"), 
    @NamedQuery(name = "IasLabels.findByTrnsFlg", query = "SELECT i FROM IasLabels i WHERE i.trnsFlg = :trnsFlg")}) 
public class IasLabels implements Serializable { 

    private static final long serialVersionUID = 1L; 

// @OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL) 
// @PrimaryKeyJoinColumn 
// private LangDef langDef; 
// 
// public LangDef getLangDef() { 
//  return langDef; 
// } 
// 
// public void setLangDef(LangDef langDef) { 
//  this.langDef = langDef; 
// } 
+0

你們知道關於Hibernate一對一的映射? –

+0

我添加了這條評論,以便讓其他人回答,他在實體IasLables中有一個組合鍵,而在實體LangDef中有一個正常的主鍵,所以他發現很難做一對一映射 –

回答

1

1)答: 你必須使用Hibernate實體1日選項添加外鍵的主鍵按你的數據庫表,並用我的答案給了昨天

Datatable displaying 2 different entity tables with relation need to get the another column values from database table

2)回答

現在你有備用選項 1)你需要創建正常像下面的類

public class IasLabelsWithLang implements Serializable { 

    private static final long serialVersionUID = 1L; 

    private IasLabels lasLabels; 
    private LangDef langDef; 


    public LangDef getLangDef() { 
     return langDef; 
    } 

    public void setLangDef(LangDef langDef) { 
     this.langDef = langDef; 
    } 

    public IasLabels getIasLabels() { 
     return iasLabels; 
    } 

    public void setIasLabels(IasLabels iasLabels) { 
     this.iasLabels = iasLabels; 
    } 
} 

2)你需要

@ManagedBean 
@SessionScoped 
public class LabelsMB { 

    static Logger logger = Logger.getLogger(LabelsMB.class); 

    List<IasLabelsWithLang> labelsList = null; 

    @ManagedProperty(value = "#{labelService}") 
    private LabelService labelService; 

    public LabelService getLabelService() { 
     return labelService; 
    } 

    public void setLabelService(LabelService labelService) { 
     this.labelService = labelService; 
    } 

    public List<IasLabelsWithLang> getListData() { 
     if (labelsList == null || labelsList.isEmpty()) { 
      if (this.getLabelService() != null) { 
       labelsList = this.getLabelService().getAllLabels(); 
      } 
     } 
     return labelsList; 
    } 
} 

3)您需要更改以下的事情在你的管理bean來按照你的服務類

@Service 
@Transactional 
public class LabelService { 

    static Logger logger = Logger.getLogger(LabelService.class); 

    @Autowired 
    private ILabelsDAO labelRepo; 

    @Autowired 
    private LangDefDAO langDefDAO; 


    public ILabelsDAO getLabelRepo() { 
     return labelRepo; 
    } 

    public void setLabelRepo(ILabelsDAO labelRepo) { 
     this.labelRepo = labelRepo; 
    } 

    public List<IasLabelsWithLang> getAllLabels() { 
     List<IasLabelsWithLang> list = new ArrayList<IasLabelsWithLang>(); 
     if (this.getLabelRepo() != null) { 
      List<IasLabels> lasLabelsList = this.getLabelRepo().findAll(); 
      for(IasLabels lasLabels : lasLabelsList){ 
       IasLabelsWithLang model = new IasLabelsWithLang(); 
       model.setIasLabels(lasLabels); 
       model.setLangDef(langDefDAO.findByPk(lasLabels.getLangNo())); 

       list.add(model); 
      } 
      return list; 
     }    
     return null; 
    } 

    public Iterable<IasLabels> saveData(List<IasLabels> originalValue) { 
     return labelRepo.save(originalValue); 
    } 
} 

4的變化)你需要在您的XHTML文件中進行流動更改

<p:dataTable id="dataTable" emptyMessage="#{res.NO_RECORDS_FOUND}" var="lab" value="#{labelsMB.listData}" editable="true" editMode="cell" paginator="true" rows="10" paginatorTemplate=" {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15"> 
<p:column headerText="#{res.CAPTION_DET}" sortBy="#{lab.lasLabels.captionDet}" filterBy="#{lab.lasLabels.captionDet}" filterMatchMode="contains" filterStyle="width: 360px;"> 
    <p:cellEditor> 
     <f:facet name="output"> 
      <h:outputText value="#{lab.lasLabels.captionDet}" /> 
     </f:facet> 
     <f:facet name="input"> 
      <h:inputText value="#{lab.lasLabels.captionDet}" style="width:96%"/> 
     </f:facet> 
    </p:cellEditor> 
    </p:column> 
    <p:column headerText="#{res.LABEL_NO}" sortBy="#{lab.lasLabels.iasLabelsPK.labelNo}" filterBy="#{lab.lasLabels.iasLabelsPK.labelNo}"> 
     <p:outputLabel value="#{lab.iasLabelsPK.labelNo}" /> 
    </p:column> 
    <p:column headerText="#{res.LANGUAGE_NO}" sortBy="#{lab.lasLabels.iasLabelsPK.langNo}" filterBy="#{lab.lasLabels.iasLabelsPK.langNo}" width="100"> 
     <p:outputLabel value="#{lab.iasLabelsPK.langNo}" /> 
    </p:column> 
    <p:column headerText="#{res.LANGUAGE_NAME}" sortBy="#{lab.langDef.langName}" filterBy="#{lab.langDef.langName}" width="130"> 
     <p:outputLabel value="#{lab.langDef.langName}" /> 
    </p:column> 
</p:dataTable> 

希望,你會修復的問題.. :)

EDITED 您需要添加朗諾變量您IasLabels內實體

@Entity 
@Table(name = "IAS_LABELS") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "IasLabels.findAll", query = "SELECT i FROM IasLabels i"), 
    @NamedQuery(name = "IasLabels.findByLangNo", query = "SELECT i FROM IasLabels i WHERE i.iasLabelsPK.langNo = :langNo"), 
    @NamedQuery(name = "IasLabels.findByLabelNo", query = "SELECT i FROM IasLabels i WHERE i.iasLabelsPK.labelNo = :labelNo"), 
    @NamedQuery(name = "IasLabels.findByCaptionDet", query = "SELECT i FROM IasLabels i WHERE i.captionDet = :captionDet"), 
    @NamedQuery(name = "IasLabels.findByTrnsFlg", query = "SELECT i FROM IasLabels i WHERE i.trnsFlg = :trnsFlg")}) 
public class IasLabels implements Serializable { 

    // langNo variable you need to add in your IasLabels entity 
    @Column(name = "LANG_NO") 
    private Short langNo; 

    public Short getLangNo() { 
     return langNo; 
    } 

    public void setLangNo(Short langNo) { 
     this.langNo = langNo; 
    } 

} 
+0

model.setLangDef(langDefDAO.findByPk (lasLabels.getLangNo())); ....我在這裏得到錯誤getLangNo() – zia

+0

findByPk這是你的函數名稱哪一個字段PK數據來自實體按照你的DAO類方法,它只是一個例子 * lasLabels。 getLangNo()它的langNo對象從你的lasLabels實體 –

+0

langDefDAO.findBYID()...得到這個在我的netBeans .... – zia