显示2个不同实体表的数据表需要从数据库表中获取另一列值

Posted

技术标签:

【中文标题】显示2个不同实体表的数据表需要从数据库表中获取另一列值【英文标题】:Datatable displaying 2 different entity tables with relation need to get the another column values from database table 【发布时间】:2016-07-08 20:24:17 【问题描述】:

我需要从 UI jsf 的单个数据表中的 LangDef 实体类中获取语言名称的列值。请让我尽快

<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.captionDet" filterBy="#lab.captionDet" filterMatchMode="contains" filterStyle="width: 360px;">
                                        <p:cellEditor>
                                            <f:facet name="output">
                                                <h:outputText value="#lab.captionDet" />
                                            </f:facet>
                                            <f:facet name="input">
                                                <h:inputText value="#lab.captionDet" style="width:96%"/>
                                            </f:facet>
                                        </p:cellEditor>
                                    </p:column>
                                    <p:column headerText="#res.LABEL_NO" sortBy="#lab.iasLabelsPK.labelNo" filterBy="#lab.iasLabelsPK.labelNo">
                                        <p:outputLabel value="#lab.iasLabelsPK.labelNo" />
                                    </p:column>
                                    <p:column headerText="#res.LANGUAGE_NO" sortBy="#lab.iasLabelsPK.langNo" filterBy="#lab.iasLabelsPK.langNo" >
                                        <p:outputLabel value="#lab.iasLabelsPK.langNo" />
                                    </p:column>
                                    <p:column headerText="#res.LANGUAGE_NAME" sortBy="#lab.???????" filterBy="#lab.?????" >
                                        <p:outputLabel value="#lab...?????" />
                                    </p:column>
 </p:dataTable>

IAS_LABELS DAO 实体:

public interface ILabelsDAO extends CrudRepository<IasLabels, IasLabelsPK>        
    @Query List<IasLabels> findAll(); 

LabelService 服务类

@Service
@Transactional
public class LabelService 

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

    @Autowired
    private ILabelsDAO labelRepo;

    public ILabelsDAO getLabelRepo() 
        return labelRepo;
    

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

    public List<IasLabels> getAllLabels() 
        if (this.getLabelRepo() != null) 
            return this.getLabelRepo().findAll();
        return null;
    

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


LabelsMB ManagedBean 实施

@ManagedBean
@SessionScoped
public class LabelsMB 

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

    List<IasLabels> labelsList = null;

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

    public LabelService getLabelService() 
         return labelService;
    

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


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

LangDef 实体类

@Entity
@Table(name = "LANG_DEF")
@XmlRootElement
@NamedQueries(
        @NamedQuery(name = "LangDef.findAll", query = "SELECT l FROM LangDef l"),
        @NamedQuery(name = "LangDef.findByLangNo", query = "SELECT l FROM LangDef l WHERE l.langNo = :langNo"),
        @NamedQuery(name = "LangDef.findByLangName", query = "SELECT l FROM LangDef l WHERE l.langName = :langName"),
        @NamedQuery(name = "LangDef.findByRepExt", query = "SELECT l FROM LangDef l WHERE l.repExt = :repExt"),
        @NamedQuery(name = "LangDef.findByLangDflt", query = "SELECT l FROM LangDef l WHERE l.langDflt = :langDflt"),
        @NamedQuery(name = "LangDef.findByLangDir", query = "SELECT l FROM LangDef l WHERE l.langDir = :langDir"),
        @NamedQuery(name = "LangDef.findByFlgSt", query = "SELECT l FROM LangDef l WHERE l.flgSt = :flgSt"),
        @NamedQuery(name = "LangDef.findByLangExt", query = "SELECT l FROM LangDef l WHERE l.langExt = :langExt"))
public class LangDef implements Serializable 
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "LANG_NO")
    private Short langNo;
    @Size(max = 60)
    @Column(name = "LANG_NAME")
    private String langName;
    @Size(max = 10)
    @Column(name = "REP_EXT")
    private String repExt;
    @Column(name = "LANG_DFLT")
    private Short langDflt;
    @Column(name = "LANG_DIR")
    private Short langDir;
    @Column(name = "FLG_ST")
    private Short flgSt;
    @Size(max = 10)
    @Column(name = "LANG_EXT")
    private String langExt;

    public LangDef() 
    

    public LangDef(Short langNo) 
     this.langNo = langNo;
    

    public Short getLangNo() 
        return langNo;
    

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

    public String getLangName() 
        return langName;
    

    public void setLangName(String langName) 
        this.langName = langName;
    

我需要从 UI jsf 的单个数据表中的 LangDef 实体类中获取语言名称的列值。请让我尽快

【问题讨论】:

您需要将 List 与 LangDef 类结合起来吗? 是的...在 LangDef 实体类中我只需要 DB 的 LANG_NAME 列值 IasLabels 实体与 LangDef 实体之间有什么关系吗? IASLABELS 具有列名 LANG_NO 和 LANGDEF 也具有相同的列名 LANG_NO IasMsgsPK 实体是 LANG_NO,LANGDEF 实体primerykey 是 LANG_NO 【参考方案1】:

你只需要像下面这样改变你的代码

在 xhtml 中:

<p:column headerText="#res.LANGUAGE_NAME" sortBy="#lab.langDef.langName"    filterBy="#lab.langDef.langName" >
              <p:outputLabel value="#lab.langDef.langName" />
</p:column>

实体IasLabels

@Entity
@Table(name = "LAS_LABELS")
public class IasLabels implements Serializable 

    private LangDef langDef;


    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "LANG_NO", nullable = false)
    public LangDef getLangDef() 
        return this.langDef;
    

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

希望对你有所帮助..

已编辑:

你需要在你的spring配置文件中添加以下内容

@Autowired
SessionFactory sessionFactory;

@Override
public void addInterceptors(InterceptorRegistry registry) 
            registry.addWebRequestInterceptor(openSessionInViewInterceptor());


@Bean
public OpenSessionInViewInterceptor openSessionInViewInterceptor()
        OpenSessionInViewInterceptor openSessionInterceptor = new OpenSessionInViewInterceptor();
        openSessionInterceptor.setSessionFactory(sessionFactory);
        return openSessionInterceptor;

【讨论】:

运行我的项目时出现此错误...HTTP 状态 500 - 无法提取 ResultSet; SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet 你需要从代码中移除 fetch = FetchType.LAZY 并尝试一下 在做之后...得到相同的异常无法提取结果集; SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: could not extract ResultSet 我想你忘了在你的配置中写这行请看我的编辑答案 因为它对我有用,我在单个表中得到了结果

以上是关于显示2个不同实体表的数据表需要从数据库表中获取另一列值的主要内容,如果未能解决你的问题,请参考以下文章

嵌套查询与连接查询的区别是啥

另一种多线程核心数据使用

比较两个列并从同一个表中的另一列获取数据

sql存储过程从一张表中查询到的值作为另一张表的新的字段

mysql 获取一张表中, 另一张表不存在的数据

怎么从一张表中查询数据插入到另一张表中