简体   繁体   English

用于读取现有数据时,如何用数据库中的数据填充托管bean属性?

[英]How to fill managed bean properties with data from DB when using for existing data read?

I have a very simple managed bean LOB which is used for reading existing LOBs(from DB) as well as for writing new LOB to DB. 我有一个非常简单的托管bean LOB ,用于读取现有的LOB(从DB)以及将新的LOB写入DB。

@ManagedBean(name = "lob")
@RequestScoped
public class LOB implements Serializable {    
    private int id;
    private String title;
    private String content;    

    public void createNewLOB(){    
       // code for adding new lob goes here
    }

    /** getters and setters for various bean properties go here**/

}

Now, since I am using the same LOB bean to read existing LOB (s) read from DB and the same bean (but ofcourse different instance) to write a new LOB to DB. 现在,由于我使用的是同一个LOB bean来读取从DB读取的现有LOB ,并且要使用同一个bean(当然是不同的实例)将新的LOB写入DB。 How do I tell the bean instance to retrieve data from DB when bean has been instantiated to read existing LOB ? 当实例化bean来读取现有的LOB时,如何告诉bean实例从DB检索数据?

Should I retrieve data specific to each bean property inside the getters for each property ?? 我应该在每个属性的getter中检索特定于每个bean属性的数据吗? Not good option, I guess because I would like to read all the data from DB at once. 我猜这不是一个好选择,因为我想一次从DB读取所有数据。

So , what I want is, bean properties should be filled with data from DB in one single round, well before the getters for bean properties are called by EL expressions used in facelets code. 所以,我想要的是,bean属性应该在一个回合中就用DB中的数据填充,这要早于facelets代码中使用的EL表达式调用bean属性的获取方法。

Can I somehow tell that the instance has been created for reading existing data & not for writing new LOB ? 我能以某种方式告知实例已创建,用于读取现有数据而不是用于编写新的LOB吗?

How do I tell the bean instance to retrieve data from DB when bean has been instantiated to read existing LOB ? 当实例化bean来读取现有的LOB时,如何告诉bean实例从DB检索数据?

Just do the job in the (post)constructor. 只需在(post)构造函数中完成工作即可。


Can I somehow tell that the instance has been created for reading existing data & not for writing new LOB? 我能以某种方式告诉该实例已创建用于读取现有数据而不是用于写入新的LOB吗?

Check if id is not null . 检查id是否为null


Note that you're mixing the concepts "managed bean", "entity" and "data access object" into a single class this way. 请注意,您是以这种方式将“托管bean”,“实体”和“数据访问对象”的概念混合到一个类中。 This is tight coupling and not necessarily good design. 这是紧密耦合,不一定是好的设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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