开发者

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

开发者 https://www.devze.com 2023-04-12 02:52 出处:网络
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.

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.

@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. How do I tell the bean instance to retrieve data from DB when bean has been instantiated to read existing LOB ?

Should I retrieve data specific to each bean property inside the getters for each property ?? Not good option, I guess because I would like to read all the data from DB at once.

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.

Can I somehow tell that the instance has been created for reading existing data & not for writing new LOB?


How do I tell the bean instance to retrieve data from DB when bean has been instantiated to read existing LOB ?

Just do the job in the (post)constructor.


Can I somehow tell that the instance has been created for reading existing data & not for writing new LOB?

Check if id is not null.


Note that you're mixing the concepts "managed bean", "entity" and "data access object" into a single class this way. This is tight coupling and not necessarily good design.

0

精彩评论

暂无评论...
验证码 换一张
取 消