Primefaces Datatable with Eclipselink

UI Components for JSF
Post Reply
davidoff
Posts: 10
Joined: 29 Mar 2011, 01:09

29 Mar 2011, 01:50

Hi there,

I stumbled on following problem:
I'm trying to get results from a postgres database (eclipselink persistence) into p:datatable and calling several rows within p:dialog.
The issue is that p:dialog stays empty.
So I logged the getter and setter methods of the affected selectedVar in my JavaBean and found out that the gettermethods are called BEFORE the setter method is called.

XHTML:

Code: Select all

				<h:form>
					<p:dataTable var="obj" value="#{mybean.objs}"  
						rowSelectListener="#{mybean.onSelectobj}"
						selection="#{mybean.selectedobj}" selectionMode="single"
						onselectComplete="objDialog.show()">
...
...
...
					</p:dataTable>
				</h:form>
					<p:dialog header="#{mybean.selectedobj.value1} || Details" widgetVar="userDialog"
						width="360" resizable="false" closable="true"
						showEffect="fade" hideEffect="fade">
						<h:form>
...
Request Bean:

Code: Select all

...
...
...
	@SuppressWarnings("unchecked")
	public List<myObject> getObjs() {  
		factory = Persistence.createEntityManagerFactory("xxx");
		EntityManager em = factory.createEntityManager();
		em.getTransaction().begin();
		Query q = em.createQuery("select o from ObjTable o");
		objs = q.getResultList(); 
		em.close();
		return objs;
	}  
	
	public myObject getSelectedobj() {
		if(this.selectedobj!=null) {
			logger.log(Level.INFO, "Info - getSelectedobj aufgerufen - " + selectedobj.getValue1());			
		} else {
			logger.log(Level.INFO, "Info - getSelectedobj aufgerufen");			
		}
		return selectedobj;
	}
...
...
...	
	public void onSelectobj(SelectEvent event) {
		logger.log(Level.INFO, "Info - onSelectobj aufgerufen - " + ((myObject) event.getObject()).getValue1());
                setSelectedobj((myObject) event.getObject()).getValue1());
	}	

My Bean works fine, so selectedobj is set when onSelectobj is called, BUT it's no use when the setter method is called after the getter methods.

Well, I googled a possible solution (would it be a solution ?):
- change scope to view and make your bean serializable

In fact, I just can't do that because of JPA persistence.

So, what can I do ?

Thx in advance
David

callahan
Posts: 768
Joined: 27 May 2010, 22:52

29 Mar 2011, 11:55

There are a few issues with the code posted above:

- The onselectComplete attribute of the p:dataTable is set to "objDialog.show()", but the widgetVar for the p:dialog is called "userDialog". It should probably be onselectComplete="userDialog.show()"
- The managed bean class corresponding to object mybean should be ViewScoped, not the JPA entity classes. The JPA entity classes and the managed bean should be serializable.
- The getObjs methods should only return the list of objects. It shouldn't access the database to determine what they are. The database could for example be accessed in the constructor. JSF can call getObjs as often as it wants and this will result in the database being hit on each call currently.
- mybean.objs is the value for the p:dataTable. Assinging a new value to mybean.objs as is done in the getObjs method is not a good idea. The objs list is cached by the UIData corresponding to the p:dataTable and assigning it a new value on the fly messes things up.

davidoff
Posts: 10
Joined: 29 Mar 2011, 01:09

29 Mar 2011, 16:00

- The onselectComplete attribute of the p:dataTable is set to "objDialog.show()", but the widgetVar for the p:dialog is called "userDialog". It should probably be onselectComplete="userDialog.show()"
is because posted code was just renamed
- The getObjs methods should only return the list of objects. It shouldn't access the database to determine what they are. The database could for example be accessed in the constructor. JSF can call getObjs as often as it wants and this will result in the database being hit on each call currently.
- mybean.objs is the value for the p:dataTable. Assinging a new value to mybean.objs as is done in the getObjs method is not a good idea. The objs list is cached by the UIData corresponding to the p:dataTable and assigning it a new value on the fly messes things up.
is done, doesn't help
- The managed bean class corresponding to object mybean should be ViewScoped, not the JPA entity classes. The JPA entity classes and the managed bean should be serializable.
entities are serializable.
making mybean serializable does not effect the problem.
mybean can't be viewscoped, neither with eclipselink nor with hibernate. if you do so, you get following error:
java.io.NotSerializableException: org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 32 guests