JSF :: Persisting Beans In A Database
Apr 8, 2014
This is a general question about best practices for handling persisted data in JSF. My JSF page is going to have several fields that map to a managed bean. Upon a button click the fields of this bean are going to be persisted in a database. Is it better to use another bean with application scope to handle the JDBC code, or should I have a method in the bean itself to handle that? Similarly I'll need a method to retrieve the information upon a user request.
View Replies
ADVERTISEMENT
Jan 13, 2015
I'm trying to understand the concurrent model of each EJB session bean types.
The singleton is well documented and seems clear to me... Only one instance and many threads using it but each method by default is synchronized because @Lock is defaulted to WRITE. We can let multiple threads use on method with @Lock(READ).
The stateless beans are in a pool I think I read somewhere that the container will ensure only one thread is using one instance at a time but this instances are recycled/reused so many threads can use the same instance but one at a time.
Is this correct ? or is it possible that multi-threading occur in one instance of SLSB ?If in the client I obtain a single reference of a SLSB and share this "instance reference" in multiple threads is it true that all the threads could use different instances on the server side ?
The stateful instance I obtain in the client is linked to one server instance and any method call will target the same instace. If many threads are using the same reference, all method calls will be synchronized and waiting for a certain amount of time that can be defined in @AccessTimeout and if the timeout is reach will end with a ConcurrentAccessException.
Can we use @Lock(READ) and let many thread use the same method like in a singleton ?
View Replies
View Related
Nov 24, 2014
I have a primefaces editable datatable with column filtering feature.The datatable has live scrolling feature.The problem that i am facing here is that both filtering and scrolling are happening correctly with Request scoped managed bean but when the scope of the same bean is changed to view scope(javax.faces.bean.ViewScoped) then the filtering happens but on removing the keyed in key word from filter box the table content is not reset to original state and also i am not able to scroll down to next set of records on reaching the end of scrolling.Cell editing feature is working perfectly.
One thing that i observed is ,currently i am querying 5000 odd records to load into datatable.But if the number of records is limited below 5000 scrolling is happening correctly but problem with filtering remains same.I even tested by upgrading to Primefaces 5.1 from 3.Code snippet of xhtml page
<h:form id="form">
<div id="content">
<p:dataTable id="workSpaceList" var="data"
value="#{workSpaceBean.list}" widgetVar="multiSelection"
selection="#{workSpaceBean.selectedRows}" resizableColumns="true"
liveScroll="true" scrollRows="10" scrollWidth="90%"
scrollHeight="70%" styleClass="datatable"
tableStyle="table-layout:auto;width:auto important!;height:auto important!;padding-left:15px;"
scrollable="true" editable="true" editMode="cell"
filteredValue="#{workSpaceBean.filteredWorkSpaceItems}"
rowKey="#{data.lpID}" >
[code]....
View Replies
View Related
Aug 5, 2014
I'm wondering if there's a way to build a template for managed beans which could be extended by a constructor instead of re-writing beans for each entity. I can do that quite easily for Dao objects by creating facades and using those facades to create Dao implementations for specific entities. Not sure if the same concept works for managed beans and haven't really come accross any searches.
I wrote the following but I'm not sure how to implement or even if the concept of generics and templating can be applied to managed beans in the same way it can be applied to Dao classes:
public class BeanTemplate<T> {
private ListDataModel<T> listModel;
@EJB
private GenDao dao;
private Class<T> entityClass;
[Code] .....
The above assumes there's only one method needed in the bean. I thought of extending like this:
public class EmployeeBean extends BeanTemplate<Employee> {
public EmployeeBean() {
super(Employee.class);
}
// how can the methods be called??
Is the same concept for creating dao templates possible for managed beans?
View Replies
View Related
Jan 29, 2015
My code of start.xhtml is:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
[Code] .....
when I deploy the app(glassfish) I get this message only:
hi #{user.minimum} a #{user.maximum} .
Instead of a picture: wave.med.gif and message:
hi 0 a 10 .
file hierarchy is attached
View Replies
View Related
May 24, 2014
If I have the next request scoped JSF bean for example:
public class UserBean {
private String name;
private String surname;
public String saveUser(){
//service is called to save a user
}
public String updateUser(){
//service is called to update a user
[Code] ....
1.In struts for example the Action classes are singletons and I think is the way it has to be because they contain business logic and is the same logic for every user but in JSF because of you mix properties from a form and methods with business logic, these beans have to be request scoped like the above one but is very wierd that a bean which contains business logic(saveUser()....) be request scoped;I dont see it effective, is like creating a new servlet each time you want to save a user but I think is the way JSF works, right?
2 To avoid the mixing of form properties in a bean with business logic, some people say to have the form beans request scoped and actions beans session scoped.
- Is this right?
- How then can you get the request form bean in the action bean?
- The scopes in JSF are request, session and view so you cannot create singleton action beans, the best you can get is a session action bean, right?. Once again I dont see the point of creating action beans with session scoped,they should be application scoped if it existed
View Replies
View Related
Apr 18, 2014
What are the best practices for unit testing MDBs and session beans ?How can they be tested without running an application server ?
View Replies
View Related
Mar 7, 2015
I've spent almost 3 hours on googling about java beans and where it is usable. What I've figured out is that a bean has a public non-arg constructor, properties and getters/setters to manipulate them. I also know that a bean contains no logic, only fields. However, I don't fully understand why I need to use beans instead of normal classes even if a class can do the same things like a bean? Are beans used to store data or what?
View Replies
View Related
May 29, 2014
I have seen in some examples like URL... a good design is to have the model and the action methods in one just single bean and the model not to be a separated class but a few properties like this:
public class CustomerBean implements Serializable{
//DI via Spring
CustomerBo customerBo;
[b]public String name;[/b]
[b]public String address;[/b]
//getter and setter methods
[code]...
Some questions:
1. If you are using hibernate or any other ORM like the above example(URL...), why not to use the hibernate pojo bean directly like it represented the form instead of using properties?:
public class CustomerBean implements Serializable{
//DI via Spring
CustomerBo customerBo;
[b]Customer customer;[/b] //represents the properties of a form
//getter and setter methods
public void setCustomerBo(CustomerBo customerBo) {
this.custom
2. Why is it said that JSF represents the purest MVC? Spring separates the model from the view too and Struts does too. I dont really understand it
View Replies
View Related
Nov 17, 2014
I have an in-cell editable data table with a viewscoped managed bean.I found that the control never goes to the ajax event method onCellEdit when the scope of the bean is @Viewscoped but it works when the scope is changed to request scope.how to get this feature work with viewscope.Below is my code snippet
xhtml snippet
<p:dataTable id="workSpaceList" var="data"
value="#{workSpaceBean.lpInfoList}" widgetVar="multiSelection"
selection="#{workSpaceBean.selectedRows}"
scrollable="true" rowIndexVar="index" editable="true"
editMode="cell".......>
[code]....
View Replies
View Related
Mar 17, 2014
I am working on a java bean, on a stopwatch
private String displayFormat = "%02d:%02d:%02d";// produces 00:00:00 hour:min:seconds
public void timerHasChanged() {
currentTime = System.currentTimeMillis();
// How long has been taken so far?
long secsTaken = (currentTime - startTime) / 1000;
long minsTaken = secsTaken / 60;
secsTaken %= 60;
long hoursTaken = minsTaken/60;
minsTaken %= 60;
Formatter fmt = new Formatter();
fmt.format(displayFormat, hoursTaken, minsTaken, secsTaken);
timerJbl.setText(fmt.toString());
How would i code the get and set method for format, so in property tab a user can choose if they want the timer shown in seconds, or minutes or hours or seconds&minutes
View Replies
View Related
Sep 11, 2014
Viewing this example of pagination [URL] and other similar beans for pagination, why do they do these beans view scoped? These beans dont contain any properties for a form so they could be application scoped, right?
View Replies
View Related
Mar 7, 2014
I remember, it's possible to collect multiple parameters from each line in a .txt file (with commas used as delimiters to separate each parameter on each line). My EmployeeData.txt file looks like this:
EDR2014,Bob,Marley,02/12/2011,1000.00
ARR1234,John,Fuzzy,03/23/2013,12.00
XXX0666,Matt,Pagel,08/10/2011,23.00
DIE4273,John,McLane,07/22/1995,45.00
FUK0330,Kevin,Young,12/02/2003,7.00
NFN7734,Sam,Peterson,08/01/1999,8.00
the parameters of each line are: employeeNum, firstName, lastName, hireDate, payRate
To match the specifications in the following EmployeeInfo.java class:
Java Code:
package employeepunch;
//import org.joda.time.DateTime;
import java.io.*;
public class EmployeeInfo {
[code]...
The validation of making sure the employee uses the correct format while entering his employee number is just the first part. I also need to add validation code to make sure his employee number matches one of the ones in the EmployeeData.txt file (shown earlier above).
View Replies
View Related
Apr 23, 2014
i'm new withh java netbeans, and i want to make program where variable from database. how to get value from database to use in variable/
View Replies
View Related
Jan 4, 2015
I am trying out jsp and servlets now for the first time. I have to insert two data, car code and car name into a mysql table. I got those details using jsp but if I use form, it redirects to another page for insertion. I want the data to be inserted and still remain in same jsp page. I am thinking of using onClick from javascript but as I googled this many places have said it is a bad idea to use jsp inside javascript. If so how can we insert data to mysql table without redirecting to another page?
View Replies
View Related
Mar 10, 2014
I've got another project for a course and am stuck. I've debugged and tried to figure out where it is breaking, but I just can't find it. I've used this connection code block as well as the contstructors before, but this just won't work. I've got a tab that should send all of the information to a MySql database upon the click of 'Add Employee'. I've given my connection string, the addEmployee(); code, and if needed I can include the subclass code. I've got a superclass 'Employee' and a subclass 'Salaried' that uses four attributes from 'Employee'.
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
addEmployee();
}
public void addEmployee() {
int socialSecurity = 0;
boolean error = false;
[code]....
View Replies
View Related
Nov 17, 2014
I have done 90% of the coding however i am stuck with producing the output, i just cant get the user fields to show.
I have attached a picture of the output that is required. The program consists of two classes i have attached the database class, and have inserted my coding below.
import java.sql.*;
import java.io.*;
import java.util.*;
public class QueryDB {
private Connection con = null;
String userID,
[Code] ...
Attached image(s)
View Replies
View Related
Feb 28, 2015
I cannot restore my database that was backup already. This is my code . . .
public boolean restoreDB(String dbUserName, String dbPassword, String source) {
String[] executeCmd = new String[]{"C:Program Files (x86)MySQLMySQL Server 5.1inmysql.exe",
"--user=" + dbUserName, "--password=" + dbPassword,"-e", "source "+source};
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
[Code] .....
View Replies
View Related
Jan 6, 2014
I am relatively new to Java and I am only beginning to learn about SQL. I have some basic's down but I have been wondering is there a way that I can add data to my database using loops instead of having to physically code every row/column individually ?
View Replies
View Related
Jan 15, 2015
I was wondering if I could take the following code and somehow put the connection to database in it's own class without the "public static void main(String[] args)" method. That way I could call it anytime i want to open a connection to the database.
Java Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Sample {
public static void main(String[] args) throws ClassNotFoundException
[Code] ....
View Replies
View Related
Sep 3, 2014
how can i stop this loop(while (res.next() )
example: i hava a table conex wich i inser on it two columns name and password i fill it by
name : yosra password : lol
name : najeh password :mdr
i don't know when i fill jTextfield of password and name correctly for ewp i put yosra as name and password as lol the loop continue to the next row and i show the two message dialog on netbeans about correct acces and refused access and the frame of my chatroom is opened
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String pass=new String(this.pass.getPassword());
String nam=this.name.getText();
acceder(nam,pass);
[code]....
View Replies
View Related
Mar 17, 2014
I want to add this image to database how can i do.
View Replies
View Related
Dec 15, 2014
Creating web application which needs to work online and offline as well with database. There is chances of using the application in offline when there is no internet connection. The application going to be used for getting inputs for some inspection process which may happened remote areas as well.
View Replies
View Related
Jun 9, 2014
How can I get id from db table for each column values with java or oracle query? I did get before each column content from file and now i need get the id of this column values from db. I read csv files from folder and then load the data from these files into subfolder, and here with line li = line.split(","); I get the values that i have in csv file and split them and become in li the value name, and then in else block i create a P objekt where i set this values from csv files, and with [I]p.setLid(li); I set the value for all valuenames, and then get these valuenames in separate column with other content from this csv.
My question is, i need to do this with db, and write these csv file values to the table in db (it will be done with another java class), what i need, that to write the ids of these values into the table in db, instead of valuename, and while there is no ids for valuenames in csv files, i need to get the id from the table in db where these are already listed with sequences, and i don't know how can i write in data table the id of this valuenames and other content, this data table should contain the ids of valuenames timestamp values and the ids of the csv files name, i don't know how to do this with the ids, i must get the ids from the db table and then set these on [I]p.setLid(li); where i set early the valuenames, instead of valuenames i should set ids, that will set ids automatically for all these valuenames.
Java Code:
try {
while ((line = br.readLine()) != null) {
if (lc == 0) {
li = line.split(",");
[Code] ....
with
Java Code : li = line.split(","); mh_sh_highlight_all('java');
I got the values that then will be given to [I]p.setLid(li); and iterate, but now i want to get id from the table in db where this values stored with id instead of this column values, how can i do it?
View Replies
View Related
Feb 27, 2014
I am using mysql database and I have downloaded the library also I have managed to get the database connection and query working. I need to know if I need to create separate classes to add/remove/edit items and view items? Do I need to put my database connection script in every class that I create or should I create methods for both the connection and the queries that will be called in the additional classes or methods that I have?
Below is what I have written so far and it is working, I will change the database and query soon to reflect the task I need to do because I have followed a tutorial.
Java Code:
/**
* cdCollection.java
*/
package org.com.mm00422_prototype;
//Import for the SQL package
import java.sql.*;
//Registering the JDBC driver
//Class.forName("com.mysql.jdbc.Driver");
[code]....
View Replies
View Related
Sep 3, 2014
I'm building an application to save times that I've worked, being build in java. Now. I've made a testdatabse to test if everything works till now. Here is the code of testdatabse
package Controller;
import java.sql.SQLException;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
[Code] ....
what I can't figure out is how to insert a test localtime or DateTime into the database.
Here is my model
package Model;
import java.io.Serializable;
import javax.swing.JTextArea;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
public class TimeModel implements Serializable {
[Code] .....
View Replies
View Related