JSF - populate values at runtime - jsf

Halo
My name is Sergie,I working on my school project using jsf spring and jpa to built the school automation system. i am learning jsf.
I need your help, Three header
City | School | Add/Remove
<c:column>
<f:facet name="header">
<c:outputText value="City" />
</f:facet>
<c:outputText id="ukrCity" value="" " />
</c:column>
<c:column>
<f:facet name="header">
<c:outputText value="School" />
</f:facet>
<c:inputText id="school" value=""
maxlength="12" " />
</c:column>
<c:column>
<f:facet name="header">
<c:outputText value="Add/Remove" />
</f:facet>
<c:selectBooleanCheckbox
id="addremove"
value=""
rendered="" />
</c:column>
City | School | Add/Remove
Київ "textbox" "checkbox"
Харків "textbox" "checkbox"
Cities are populated from City class
public class UkrCity {
private List<A> ukrCities;
public List<A> getUkrCities() {
return ukrCities;
}
public void setUkrCities(final List<A> ukrCities) {
this.ukrCities= ukrCities;
}
private void allCities() {
//add all cities in a list
ukrCities.add("Київ");
ukrCities.add("Харків");
}
}
how to show ukrcities on xhtml page under City and blank textbox and checkbox under school and add/remove tav.
thank you
sorry my bad english.

I have refactored your code. Here is the UkrCity Class.
package com.example;
import java.io.Serializable;
public class UkrCity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public UkrCity(String name) {
this.setName(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Here is the backing bean
package com.example;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class TableBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<UkrCity> cities = new ArrayList<UkrCity>(Arrays.asList(new UkrCity("Київ"),new UkrCity("Харків") ));
public ArrayList<UkrCity> getCities() {
return cities;
}
}
Here is your index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>UkrCities Table</title>
</h:head>
<h:body>
<h:form>
<h:dataTable value="#{tableBean.cities}" var="city">
<h:column>
<f:facet name="header">City</f:facet>
<h:outputText value="#{city.name}" />
</h:column>
<h:column>
<f:facet name="header">School</f:facet>
<h:inputText value="" />
</h:column>
<h:column>
<f:facet name="header">Add/Remove</f:facet>
<h:selcectBooleanCheckbox value="" onclick="submit()" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>

Related

JSF Rendered property Issue

So, i have this piece of code, which basically performs CRUD on a database and displays the results in a <h:dataTable>. The problem is, my code is able to generate an edit action, when I provide a fixed ArrayList. But, if i populate the ArrayList from a database, the rendered property does not provide an editable option from the form. I am able to perform delete functionality, so it's not a matter of the object not being read properly in the action method's parameter. In short, the rendered attribute doesn't work when my data source is a table from a database.
Here's the page:
index.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:head>
</h:head>
<h:body>
<h1>JSF 2 dataTable example</h1>
<h:form>
<h:dataTable value="#{order.orderList}" var="o"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row"
>
<h:column>
<f:facet name="header">Order No</f:facet>
<h:inputText value="#{o.orderNo}" size="10" rendered="#
{o.editable}" />
<h:outputText value="#{o.orderNo}" rendered="#{not o.editable}" />
</h:column>
<h:column>
<f:facet name="header">Product Name</f:facet>
<h:inputText value="#{o.productName}" size="20" rendered="#
{o.editable}" />
<h:outputText value="#{o.productName}" rendered="#{not o.editable}"
/>
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
<h:inputText value="#{o.price}" size="10" rendered="#{o.editable}"
/>
<h:outputText value="#{o.price}" rendered="#{not o.editable}" />
</h:column>
<h:column>
<f:facet name="header">Quantity</f:facet>
<h:inputText value="#{o.qty}" size="5" rendered="#{o.editable}"
/>
<h:outputText value="#{o.qty}" rendered="#{not o.editable}" />
</h:column>
<h:column>
<f:facet name="header">Action</f:facet>
<h:commandButton value="Edit" action ="#{order.editAction(o)}">
<f:setPropertyActionListener
target = "#{Orders}" value = "#{o}" />
</h:commandButton>
</h:column>
<h:column>
<f:facet name ="header">Action</f:facet>
<h:commandButton value ="Delete" action="#
{order.delete(o)}"/>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
The orderBean.java:
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
#ManagedBean(name="order")
#RequestScoped
public class orderBean implements Serializable{
private static final long serialVersionUID = 1L;
private static ArrayList<Orders> orderList;
public ArrayList<Orders> getOrderList() {
try{
orderBean.orderList = new ArrayList();
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/orders","root","");
PreparedStatement ps = conn.prepareStatement("Select *
from Orders");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
Orders orders = new
Orders(rs.getString("orderNo"),rs.getString("productName"),
rs.getBigDecimal("price"),rs.getInt("qty"));
orderList.add(orders);
}
}
catch(Exception e)
{
}
return orderList;
}
public void delete(Orders order)
{
try{
orderBean.orderList = new ArrayList();
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/orders","root","");
PreparedStatement ps = conn.prepareStatement("Delete
from orders where orderNo=?");
ps.setString(1,order.orderNo);
int rs = ps.executeUpdate();
}
catch(Exception e)
{
}
}
public void editAction(Orders order) {
order.setEditable(true);
}
public static class Orders{
String orderNo;
String productName;
BigDecimal price;
int qty;
boolean editable;
public Orders(String orderNo, String productName, BigDecimal price, int
qty) {
this.orderNo = orderNo;
this.productName = productName;
this.price = price;
this.qty = qty;
}
public void setOrderNo(String orderNo)
{
this.orderNo = orderNo;
}
public void setProductName(String productName)
{
this.productName = productName;
}
public void setPrice(BigDecimal price)
{
this.price = price;
}
public void setQty(int qty){
this.qty = qty;
}
public String getOrderNo()
{
return this.orderNo;
}
public String getProductName()
{
return this.productName;
}
public BigDecimal getPrice()
{
return this.price;
}
public int getQty()
{
return this.qty;
}
public boolean isEditable() {
return this.editable;
}
public void setEditable(Boolean editable) {
this.editable = editable;
}
//getter and setter methods
}
}

How can I select multiple rows within dataTable in JSF

I am facing some problem to select multiple rows of h:dataTable. My code is below:
<h:dataTable value="#{reportBean.lstchalan}" var="chalan" >
<h:column >
<f:facet name="header">
<h:outputText value="Select" />
</f:facet>
<h:selectBooleanCheckbox value="#{reportBean.checked[chalan.issueNo]}" />
</h:column>
...
</h:dataTable>
<h:commandButton value="submit" action="#{reportBean.submit()}" />
and Below is my backing bean:
public class ReportBean {
List<ChalanVo> checkedItems = new ArrayList<ChalanVo>();
private Map<String, Boolean> checked = new HashMap<String, Boolean>();
........
public List<ChalanVo> getCheckedItems() {
return checkedItems;
}
public void setCheckedItems(List<ChalanVo> checkedItems) {
this.checkedItems = checkedItems;
}
public Map<String, Boolean> getChecked() {
return checked;
}
public void setChecked(Map<String, Boolean> checked) {
this.checked = checked;
}
public String submit() {
checkedItems = new ArrayList<ChalanVo>();
for (ChalanVo dataItem : lstchalan) {
if (checked.get(dataItem.getIssueNo())) {
checkedItems.add(dataItem);
checked.remove(dataItem.getIssueNo());
}
}}
}
But I am getting an exception when execute the line for (ChalanVo dataItem : lstchalan) . lstchalan is giving null.Could you please help me to understand where I am doing wrong?
Here is a working, simplified example:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
#Named("test")
#ViewScoped
public class TestBean implements Serializable{
private static final long serialVersionUID = -1064219566884774973L;
private List<ChalanVo> lstChalans;
private Map<ChalanVo, Boolean> checkedItems = new HashMap<TestBean.ChalanVo, Boolean>();
public TestBean() {
lstChalans = new ArrayList<TestBean.ChalanVo>();
lstChalans.add(new ChalanVo("test1"));
lstChalans.add(new ChalanVo("test2"));
lstChalans.add(new ChalanVo("test3"));
}
public List<ChalanVo> getLstChalans() {
return lstChalans;
}
public void setLstChalans(List<ChalanVo> lstChalans) {
this.lstChalans = lstChalans;
}
public Map<ChalanVo, Boolean> getCheckedItems() {
return checkedItems;
}
public void setCheckedItems(Map<ChalanVo, Boolean> checkedItems) {
this.checkedItems = checkedItems;
}
public void save() {
System.out.println("save");
for (Entry<ChalanVo, Boolean> e : checkedItems.entrySet()) {
if (e.getValue()) {
System.out.println("checked: " + e.getKey().getIssueNo());
}
}
}
public class ChalanVo {
private String issueNo;
public ChalanVo(String issueNo) {
setIssueNo(issueNo);
}
public String getIssueNo() {
return issueNo;
}
public void setIssueNo(String issueNo) {
this.issueNo = issueNo;
}
}
}
With this xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head />
<h:body>
<h:form>
<h:dataTable value="#{test.lstChalans}" var="chalan">
<h:column>
<f:facet name="header">
<h:outputText value="Select" />
</f:facet>
<h:selectBooleanCheckbox value="#{test.checkedItems[chalan]}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Issue No" />
</f:facet>
<h:outputText value="#{chalan.issueNo}"/>
</h:column>
</h:dataTable>
<h:commandButton action="#{test.save()}" value="Submit" />
</h:form>
</h:body>
</html>
The save() method is able to list the selected items. I think your NullPointerException is unrelated to the checkbox thing. But anyway, you can do the selection like this.

Get to specific book using hyperlink in JSF [duplicate]

This question already has answers here:
Creating master-detail pages for entities, how to link them and which bean scope to choose
(2 answers)
Closed 7 years ago.
I have a very strange situation here. I have made my application in JSF and it looks just great, however I am not quite sure about following: I want to have a hyperlink so that once I click on my book title I get to a page with all the details regarding that book. My code looks so far:
My class looks like this:
package com.century.rental;
import javax.ejb.Stateless;
import javax.persistence.*;
import java.util.List;
#Stateless
public class GameEJB {
#PersistenceContext(unitName = "PerUni")
private EntityManager em;
public List<Game> findGames() {
TypedQuery<Game> query = em.createNamedQuery("Game.findAll", Game.class);
return query.getResultList();
}
public List<Game> findGamesByTitle(String title) {
TypedQuery<Game> query = em.createNamedQuery("Game.findByTitle", Game.class);
query.setParameter("title", title);
return query.getResultList();
}
public Game find(Long id) {
return em.find(Game.class, id);
}
public Game createGame(Game game) {
em.persist(game);
System.out.print("game stored");
return game;
}
}
My controller class looks like this:
package com.century.rental;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class GameController {
#EJB
private GameEJB gameEJB;
private Game game = new Game();
private String title = new String();
private List<Game> gameList = new ArrayList<Game>();
private List<Game> sgameList = new ArrayList<Game>();
public GameController() {
}
public String doCreateGame() {
gameEJB.createGame(game);
gameList = gameEJB.findGames();
game = new Game();
return "listGames.xhtml";
}
public Game getGame() {
return this.game;
}
public void setGame(Game game) {
this.game = game;
}
public List<Game> getGameList() {
gameList = gameEJB.findGames();
return gameList;
}
public void setGameList(List<Game> gameList) {
this.gameList = gameList;
}
public String searchGames() {
sgameList = gameEJB.findGamesByTitle(title);
return "resultsGames.xhtml";
}
public List<Game> getSgameList() {
return sgameList;
}
public void setSbookList(List<Game> sgameList) {
this.sgameList = sgameList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Entity:
package com.century.rental;
import java.util.Date;
import javax.persistence.*;
#Entity
#NamedQueries({
#NamedQuery(name = "Game.findAll", query = "SELECT g FROM Game g"),
#NamedQuery(name = "Game.findByTitle", query = "SELECT g FROM Game g WHERE g.title = :title")
})
public class Game extends Product {
#Basic(optional = false)
#Column(name = "DEVELOPER_STUDIO", nullable = false, length = 100)
private String developerStudio;
#Basic(optional = false)
#Column(name = "PLATFORM", nullable = false, length = 100)
private String platform;
public Game() {
}
public Game(String title, String description, String rating, Date releaseDate, String developerStudio, String platform) {
super(title, description, rating, releaseDate);
this.developerStudio = developerStudio;
this.platform = platform;
}
public String getDeveloperStudio() {
return developerStudio;
}
public void setDeveloperStudio(String developerStudio) {
this.developerStudio = developerStudio;
}
public String getPlatform() {
return platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
}
My HTML code looks like this:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>List of All Available Games in Database</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="List of All Available Games in Database"/></h1>
<h:dataTable value="#{gameController.gameList}" var="item" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Id"/>
</f:facet>
<h:outputText value="#{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:outputText value="#{item.title}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Description"/>
</f:facet>
<h:outputText value="#{item.description}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Rating"/>
</f:facet>
<h:outputText value="#{item.rating}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Release Date"/>
</f:facet>
<h:outputText value="#{item.releaseDate}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Developer Studio"/>
</f:facet>
<h:outputText value="#{item.developerStudio}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Platform"/>
</f:facet>
<h:outputText value="#{item.platform}"/>
</h:column>
</h:dataTable>
</h:form>
</f:view>
<br /><br />
Add New -OR- Go to Main Page
</h:body>
</html>
Please, help me I am not quite sure how can I make a hyperlink to a specific book from a list of books( as shown in the code )
Thanks.
P.S.
My specific page specificGame, which is supposed to get values from a game that was clicked in listGames page.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>List specific Game in Database</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="List a specific Game in Database"/></h1>
<h:dataTable value="#{GameController.sGameList}" var="item" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:outputText value="#{item.title}"/>
</h:column
</h:dataTable>
</h:form>
</f:view>
<br /><br />
Add New -OR- Go to Main Page
</h:body>
</html>
Now when I click on a title from the list of games on page list Games I would be redirected to specific Game page which is supposed to look like this: http://i.stack.imgur.com/OHmdp.png
populated by all the values from the particular game.
Try to change the column title in listGames.xhtml to :
<h:column>
<f:facet name="header">
<h:outputText value="Title"/>
</f:facet>
<h:commandLink value="#{item.title}" action="#{gameController.searchGames(item.title)}" />
</h:column>
Then, adapt the action method in the backing bean to :
public String searchGames(String tit) {
sgameList = gameEJB.findGamesByTitle(tit);
return "resultsGames.xhtml";
}

Primefaces dataTable rowEdit event doesn't seem to work

I'm investigating PrimeFaces recently. I try to create editable DataTable. I take some code from demos and created my own Facelets file and managed bean.
products.xhtml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<!-- some headers -->
</h:head>
<h:body>
<h:form>
<p:dataTable id="products" var="product" value="#{productsBean.products}" editable="true" style="margin-bottom: 20px; width: 1000px;">
<f:facet name="header">Products</f:facet>
<p:ajax event="rowEdit" listener="#{productsBean.onRowEdit}" />
<p:ajax event="rowEditCancel" listener="#{productsBean.onRowCancel}" />
<p:column headerText="Nazwa">
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{product.name}" /></f:facet>
<f:facet name="input"><p:inputText value="#{product.name}" style="width:100%" label="Nazwa"/></f:facet>
</p:cellEditor>
</p:column>
<!-- more columns... -->
<p:column style="width:32px">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
ProductsBean.java:
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.event.RowEditEvent;
// more imports...
#SessionScoped #ManagedBean
public class ProductsBean implements Serializable {
private static final long serialVersionUID = -501520863695260180L;
#EJB
private ProductDao productDao;
#EJB
private UnitDao unitDao;
private List<Product> products;
private List<Unit> units;
#PostConstruct
private void init() {
products = productDao.findAll();
units = unitDao.findAll();
}
public void onRowEdit(RowEditEvent event) {
System.out.println("onRowEdit");
}
public void onRowCancel(RowEditEvent event) {
System.out.println("onRowCancel");
}
public List<Product> getProducts() {
return products;
}
public List<Unit> getUnits() {
return units;
}
}
Unfortunatelly, onRowEdit(RowEditEvent) is never invoked, only my table gets red. I don't get any other feedback. onRowCancel(RowEditEvent) is invoked correctly. What am I doing wrong or what I am missing? Thanks for your help.

How to Persist data in One to One Relationship Using JPA JSF

At the First I will let u Know what i want to achieve and Later I will Show u the Problem.
In the Above screen Title and Description is from the AboutUs Entity and rest of the fields are for GeneralImage Entity.
Now what I want to achieve is when I click on create Button I want to persist data in two entities and have one to one relationship with each other
In order to do this I have written the below code which has the problem
AboutUs.java
package com.model;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.FetchType;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
#Entity
#NamedQuery(name = "AboutUs.findAboutUsByIdWithImages", query = "Select s from AboutUs s where
s.aboutusid = :aboutusid")
public class AboutUs implements Serializable
{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
private int aboutusid;
private String aboutustitle;
private String description;
public static final String FIND_ABOUTUS_BY_ID_WITH_IMAGES =
"AboutUs.findAboutUsByIdWithImages";
#OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
#JoinColumn(name = "imageid")
private GeneralImage image;
//Here Below I have Added Setters and Getters
……….
}
GeneralImage
#Entity
public class GeneralImage {
#Id
#GeneratedValue
private int imageid;
private String fileName;
private String categoryid;
private int orderofappearance;
private String mimetype;
private int filesize;
private int foreignkeyid;
#OneToOne(mappedBy="image", cascade=CascadeType.ALL)
private AboutUs aboutus;
//The Below Are The Setters and Getters
}
The below is My Facelets Image
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body>
<p:dialog widgetVar="aboutusCreateDialogWidget"
id="aboutusCreateDialogId" height="400" width="1000" modal="true"
closable="true" draggable="false" resizable="false" showEffect="puff" hideEffect="fold"
header="Create About Us">
<h:form id="aboutusCreateDialogForm" prependId="false">
<h:panelGrid columns="2">
<h:outputText value="* #{msgs.aboutustitle}" />
<h:inputText value="#{aboutUsMB.aboutus.aboutustitle}" required="true" label="#
{msgs.aboutustitle}" >
<f:validateLength minimum="4" />
</h:inputText>
<h:outputText value="* #{msgs.aboutusescription}" />
<h:inputText value="#{aboutUsMB.aboutus.description}" required="true" label="#
{msgs.aboutusescription}" />
<h:outputText value="* #{msgs.categoryName}" />
<h:inputText value="#{generalImageMB.generalImage.categoryid}" required="true"
label="#{msgs.categoryName}" />
<h:outputText value="* #{msgs.orderOfAppearance}" />
<h:inputText value="#{generalImageMB.generalImage.orderofappearance}" required="true"
label="#{msgs.orderOfAppearance}" />
<h:outputText value="* #{msgs.mimetype}" />
<h:inputText value="#{generalImageMB.generalImage.mimetype}" required="true" label="#
{msgs.mimetype}" />
<h:outputText value="* #{msgs.fileSize}" />
<h:inputText value="#{generalImageMB.generalImage.filesize}" required="true" label="#
{msgs.fileSize}" />
</h:panelGrid>
<h:panelGrid columns = "2">
<p:commandButton value="#{msgs.create}" icon="ui-icon-plus"
action="#{aboutUsMB.createAboutus()}"
update=":messageGrowl :aboutusForm:aboutusTable"
oncomplete="closeDialogIfSucess(xhr, status, args, aboutusCreateDialogWidget,
'aboutusCreateDialogId')"
/>
<p:commandButton value="#{msgs.cancel}" icon="ui-icon-cancel" actionListener="#
{aboutusMB.resetAboutus()}" onclick="aboutusCreateDialogWidget.hide();" type="button" />
</h:panelGrid>
</h:form>
</p:dialog>
</h:body>
</html>
Next I have AboutUsMB
AboutUsMB
In AboutUsMB my createAboutus()
public void createAboutus()
{
try
{
aboutus.setImage(generalImage); //Here I am adding the generalImage object to about so that
//it can be persisted
aboutusFacade.save(aboutus);
closeDialog();
displayInfoMessageToUser("Created With Success......!");
loadAboutus();
resetAboutus();
}
catch (Exception e)
{
keepDialogOpen();
displayErrorMessageToUser("OOPS, We Could Not Create...... Try Again Later......!");
e.printStackTrace();
}
}
Later In Façade Classes
Save Method
AboutUsFacade
public interface AboutUsFacade {
//The Below I Am Adding Methods So That I Can Deal With Addition, Deletion and Updation of Users
public abstract void save(AboutUs aboutus);
}
Façade Implimentation Class
AboutUsFacadeImp
public class AboutUsFacadeImp implements AboutUsFacade
{
#EJB
private AboutUsDAO aboutusDAO;
#Override
public void save(AboutUs aboutus)
{
aboutusDAO.save(aboutus);
}
}
GenericDAO Class
public abstract class GenericDAO<T>
{
private final static String UNIT_NAME = "SmartRealtorsPU";
#PersistenceContext(unitName = UNIT_NAME)
private EntityManager em;
private Class<T> entityClass;
public GenericDAO(Class<T> entityClass)
{
this.entityClass = entityClass;
}
public void save(T entity) {
em.persist(entity);
}
}
So After Using the above Code I am not able to have generalImage data persisted only aboutus data has been persisted also u can see that the imageid which is the field of relationship is NULL
So cud any One rectify that What might be the Problem and also where i am wrong and what needs to be done
Is i am using one form to persist is causing the Problem
I had the same problem as you, you are mapping the bidirectional mapping on only one side.
In your AboutUsMB createAboutus() try to do the following:
Instead of
aboutus.setImage(generalImage);
do
aboutus.setImage(generalImage);
generalImage.setAboutus(aboutus);

Resources