I'm learning Java 6 EE and I have a simple web app.
I have UserBean class that uses CurrencyManager class. CurrencyManager is application scoped and is a managed bean. UserBean is managed bean and session scoped.
Here is my UserBean:
#ManagedBean
#SessionScoped
public class UserBean implements Serializable{
private String username;
private ArrayList<Money> ownedMoney;
private CurrencyManager currencyManager;
private BigDecimal credits;
public UserBean() {
currencyManager = new CurrencyManager();
username = "User";
ownedMoney = new ArrayList<>();
ownedMoney.add(new Money(new BigDecimal(15000), currencyManager.getCurrency("CZK")));
ownedMoney.add(new Money(new BigDecimal(100), currencyManager.getCurrency("USD")));
credits = new BigDecimal(150);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public BigDecimal getCredits() {
return credits;
}
public void setCredits(BigDecimal credits) {
this.credits = credits;
}
public ArrayList<Money> getOwnedMoney() {
return ownedMoney;
}
public void setOwnedMoney(ArrayList<Money> ownedMoney) {
this.ownedMoney = ownedMoney;
}
public CurrencyManager getCurrencyManager() {
return currencyManager;
}
public void setCurrencyManager(CurrencyManager currencyManager) {
this.currencyManager = currencyManager;
}
}
And here my CurrencyManager:
#ManagedBean(name = "currencyManager")
#ApplicationScoped
public class CurrencyManager {
private HashMap<String, Currency> currencies;
public CurrencyManager() {
this.currencies = new HashMap<>();
currencies.put("CZK", new Currency("CZK", new BigDecimal("0.0503")));
currencies.put("GBP", new Currency("GBP", new BigDecimal("0.59")));
currencies.put("EUR", new Currency("EUR", new BigDecimal("1.38")));
currencies.put("USD", new Currency("USD", new BigDecimal("1.0")));
}
public Currency getCurrency(String name){
return currencies.get(name);
}
public java.util.Collection<Currency> getCurrencies() {
return currencies.values();
}
public void setCurrencies(HashMap<String, Currency> currencies) {
this.currencies = currencies;
}
}
The code I posted works fine as is. However I don't want to instantiate CurrencyManager in my UserBean class - that's is why I made it ApplicationScoped, since it should be available at all times.
If I remove the instantiation (first line in UserBean constructor) and change declaration to:
#ManagedProperty(value = "#{currencyManager}")
private CurrencyManager currencyManager;
then the first page that queries ownedMoney property in UserBean throws javax.servlet.ServletException: Cant instantiate class: model.UserBean. with root cause of NullPointerException. GlassFish log showed that the NullPtr occurs in UserBean constructor, when I call getCurrency on currencyManager, here:
ownedMoney.add(new Money(new BigDecimal(15000), currencyManager.getCurrency("CZK")));
Can you tell me what I'm doing wrong?
I just came across the same problem, and found out by chance, that it is not working, if I try with firefox (actually icedove under linux), but well working, if I try with the eclipse build-in browser.
Even so this does not make sense to me, have you tried with different browsers already?
Related
I want to use dropdown and getting the value in drop down from database, dropdown should contain company code for saving purpose & company description for display purpose.
Below is my code:
Bean Class:
package com.ims.master.company.bean;
public class CompanyBean {
private String id;
private String cmpCode;
private String cmpDes;
private String cmpStatus;
private String cmpCreated;
public CompanyBean(String cmpCode, String cmpDes) {
super();
this.cmpCode = cmpCode;
this.cmpDes = cmpDes;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCmpCreated() {
return cmpCreated;
}
public void setCmpCreated(String cmpCreated) {
this.cmpCreated = cmpCreated;
}
public String getCmpCode() {
return cmpCode;
}
public void setCmpCode(String cmpCode) {
this.cmpCode = cmpCode;
}
public String getCmpDes() {
return cmpDes;
}
public void setCmpDes(String cmpDes) {
this.cmpDes = cmpDes;
}
public String getCmpStatus() {
return cmpStatus;
}
public void setCmpStatus(String cmpStatus) {
this.cmpStatus = cmpStatus;
}
}
DAO class:
package com.ims.master.company.DAO;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.ims.hibernate.HibernateUtil;
import com.ims.master.company.bean.CompanyBean;
public class CompanyDAO {
SessionFactory factory = HibernateUtil.getFactory();
Session session = factory.openSession();
ArrayList<CompanyBean> recList = new ArrayList<CompanyBean>();
#SuppressWarnings("unchecked")
public ArrayList<CompanyBean> retrieveCmpCode()
{
System.out.println("=====inside DAO======");
Query query = session.createQuery("select b.cmpCode,b.cmpDes from CompanyBean b where b.cmpStatus=:val");
query.setParameter("val", "Y");
recList = (ArrayList<CompanyBean>) query.list();
System.out.println("=====value====="+recList);
return recList;
}
}
Action Class:
package com.ims.master.masterData;
import java.util.ArrayList;
import com.ims.master.company.DAO.CompanyDAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class MasterLookUp extends ActionSupport {
ArrayList companyCode;
public String getCompany()
{
CompanyDAO companyCodeValue = new CompanyDAO();
companyCode = companyCodeValue.retrieveCmpCode();
return SUCCESS;
}
public ArrayList getCompanyCode() {
return companyCode;
}
public void setCompanyCode(ArrayList companyCode) {
this.companyCode = companyCode;
}
}
jsp:
<s:select name="companyName" list="companyCode" key="label.companyName" listKey="cmpCode" listValue="cmpDes"/>
Please suggest me how the value will come in drop down.
Also suggest that how the value in drop down will be displayed selected in edit part.
You can't cast returned value to ArrayList<CompanyBean>, because Hibernate in your case converts the data returned by the query to List<Object[]>. To return List<CompanyBean> you can use another query.
You need to open the Hibernate session to execute the query, and when you have done with it you should close the session. You don't have to close a session only if it's managed by other tool. You can find a detailed explanation how to use Hibernate session in How to display a list of database records (retrieved via Hibernate) to a JSP page in Struts 2, and linked answer.
The query could return List<CompanyBean> if you change the query and property type, so you can assign a value without casting.
public class CompanyDAO {
public List<CompanyBean> retrieveCmpCode() throws Exception
{
System.out.println("=====inside DAO======");
SessionFactory factory = HibernateUtil.getFactory();
Session session = factory.openSession();
List<CompanyBean> recList;
try {
Query query = session.createQuery("from CompanyBean b where b.cmpStatus=:val");
query.setParameter("val", "Y");
recList = query.list();
System.out.println("=====value====="+recList);
return recList;
} finally {
session.close();
}
}
}
Note: #SuppressWarnings("unchecked") no longer needed.
In the JSP you should bind the select tag to the action property that returns a List<CompanyBean>, similar like you have done already.
<s:select name="companyName" list="companyCode" key="label.companyName" listKey="cmpCode" listValue="cmpDes"/>
The action
public class MasterLookUp extends ActionSupport {
private List<CompanyBean> companyCode;
public List<CompanyBean> getCompanyCode() {
return companyCode;
}
public void setCompanyCode(List<CompanyBean> companyCode) {
this.companyCode = companyCode;
}
private String cmpCode;
public String getCmpCode() {
return cmpCode;
}
public void setCmpCode(String companyCode) {
this.cmpCode = cmpCode;
}
public String getCompany() throws Exception
{
CompanyDAO companyCodeValue = new CompanyDAO();
companyCode = companyCodeValue.retrieveCmpCode();
return SUCCESS;
}
}
Note: to get/set default/selected value of the select tag you should provide cmpCode property.
you can use below code in your JSP
<html:select property ="cmpDes">
<html:optionsCollection name ="cmpDes" />
</html:select>
When above code is added in ur JSP , your dropdown will have the cmp description which is fetched from DB.
And below is site which has perfect example to learn struts-1 and is also related to your question for getting some ideas to go on.
http://www.javabeat.net/struts-html-optionscollection-tag-htmloptionscollection/
I want to collect all beans that are produced somewhere. Something like this:
static class Greeting {
public final String text;
public Greeting(String text) {
this.text = text;
}
}
#Produces
#Named("hi")
Greeting hi = new Greeting("Hi");
#Produces
#Named("hello")
Greeting hello = new Greeting("Hello");
#Inject
Instance<Greeting> greetings;
#Test
public void shouldCollectAll() {
Set<String> set = new HashSet<>();
for (Greeting greeting : greetings) {
set.add(greeting.text);
}
assertEquals(2, set.size());
assertTrue(set.contains("Hi"));
assertTrue(set.contains("Hello"));
}
I understand that I can select on the qualifiers like this:
#SuppressWarnings("all")
private static class NamedLiteral extends AnnotationLiteral<Named> implements Named {
private final String name;
public NamedLiteral(String name) {
this.name = name;
}
#Override
public String value() {
return name;
}
}
#Test
public void shouldCollectNamedHi() {
Greeting greeting = greetings.select(new NamedLiteral("hi")).get();
assertEquals("Hi", greeting.text);
}
But I want to access the qualifiers. Something like:
#Test
public void shouldCollectAllWithMeta() {
Map<String, Greeting> map = new HashMap<>();
for (Greeting greeting : greetings) {
Annotated annotated = magic(greeting);
String name = annotated.getAnnotation(Named.class).value();
map.put(name, greeting);
}
assertEquals(2, map.size());
assertEquals("Hi", map.get("hi"));
assertEquals("Hello", map.get("hello"));
}
Is there a way to implement magic without writing a CDI extension? Or is there one out there? Or is this a feature request for CDI 2.0?
It is not possible to access the bean metadata from the bean contextual instances. However, this is possible when working at the level of the beans themselves, using the BeanManager instance and without writting a CDI extension, e.g.:
#Inject
BeanManager manager;
Set<Bean<?>> beans = manager.getBeans(Greeting.class, Named.class);
for (Bean<?> bean : beans) {
String name = getQualifierOfType(bean.getQualifiers(), Named.class).value();
map.put(name, greeting);
}
<Annotation, T extends Annotation> T getQualifierOfType(Set<Annotation> qualifiers, Class<T> type) {
for (Annotation qualifier : qualifiers)
if (type.isAssignableFrom(qualifier.getClass()))
return type.cast(qualifier);
return null;
}
I am building a spring based WebApp including a RESTful method call.
I use RESTeasy and jackson to return the username of the current logged in user
(this information is stored in a session bean called "UserBean")
UserBean:
#Component("userBean")
#Scope("session")
public class UserBean implements Serializable {
#Autowired
private InitApp app;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
OverviewBean is the bean that contains the rest method (including the jackson conversion to json):
#Component("overviewBean")
#Scope("view")
#Path("/repairs")
public class OverviewBean {
#Autowired
private InitApp app;
#Autowired
private UserBean userBean;
private List<Repair> openRepairsClient;
private List<Repair> closedRepairsClient;
#PostConstruct
public void fillRepairs() {
try {
String username = userBean.getUsername();
openRepairsClient = app.repairService.findOpenRepairsByClient((Client) app.userService.getUser(userBean.getUsername()));
closedRepairsClient = app.repairService.findClosedRepairsByClient((Client) app.userService.getUser(userBean.getUsername()));
} catch (UserServiceException ex) {
Logger.getLogger(OverviewBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
//Getters and setters openRepairsClient/closedRepairsClient
#GET
#Path("/getrepairs")
#Produces("application/json")
public String getOpenRepairsInJson() {
String username = userBean.getUsername();
return "test";
}
}
fillRepairs() is able to use userBean without any errors. For example the "String username = userBean.getUsername();" within the try catch returns the username correctly.
My issue is that when getOpenRepairsInJson gets called it throws a nullPointerException
on "String username = userBean.getUsername();". It seems that my userBean is not "linked"
at the moment of the method call. What am I doing wrong?
Thanks in advance!
I am trying to use the automatic versioning of Hibernate but when the update method f of the Session is called I do not see the version field in the where clause of the query nor is the version incremented in the database. I am doing something fundamentally wrong probably, but what? Is calling getCurrentSession of sesssionFactory an issue?
I have the following entity class:
package dslibweb.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Version;
#Entity
#Table(name = "dsXCT_Recalls")
public class DsXCT_Recalls {
#Id
public String recallId;
public int version;
public String status;
//...... more properties.....
#Version
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public String getRecallId() {
return recallId;
}
public void setRecallId(String recallId) {
this.recallId = recallId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
My controller:
package recalls.controller;
#Controller
public class RecallsDataController {
#Autowired
RecallsService recallsManager;
#Autowired
AuthenticationService authService;
private static final Logger logger = Logger.getLogger(RecallsDataController.class);
private static final String SAVE_RECALLS = "MODIFY XCT RECALLS";
RecallsGrid recalls;
#RequestMapping(value = "/showRecallsGrid")
#ResponseBody
public RecallsGrid showRecallsGrid( HttpSession session, HttpServletResponse response) {
recalls = recallsManager.getRecallsDataGrid((String) session.getAttribute("socketToken"), new GridFilters(0, 0, "", "", "", "", ""));
if (recalls.getError() == null || recalls.getError().equals("")) { // no error
return recalls;
} else {
try {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, recalls.getError());
} catch (IOException e) {
e.printStackTrace();
}
return recalls;
}
}
#RequestMapping(value = "/saveRecalls" , method= RequestMethod.POST)
#ResponseBody
public String saveRecalls( HttpSession session, #RequestParam(value="ids[]", required = false) String [] ids, #RequestParam(value="statuses[]", required = false) String [] statuses){
boolean result = authService.validateUserAction((String) session.getAttribute("socketToken"), SAVE_RECALLS);
if(result)
return recallsManager.saveRecalls(ids, statuses, recalls);
else
return "You do not have authority to perform this action.";
}
}
Where I retrieve a collection of DsXCT_Recalls and show them to the user. The collection is stored in the controller. The user then changes status in one or more entities and I call the saveRecalls method of the recallManager which creates a list of only the changed entities (comparing with the collection stored in the controller).
The recallsManager (service layer) is:
package recalls.service.defaultimpl;
#Service("recallManager")
public class HibernateRecallsDataService implements RecallsService {
#Autowired
JsonRpcRequest jsonReq;
#Autowired
JsonRpcSocketWriterReader socketWriterReader;
#Autowired
JsonRpcRequestConstructor reqConstructor;
#Autowired
RecallsDao hibernateRecallsDao;
private static final Logger logger = Logger.getLogger(HibernateRecallsDataService.class);
#Transactional
public RecallsGrid getRecallsDataGrid(String socketToken, GridFilters filters) {
List<DsXCT_Recalls> recalls = hibernateRecallsDao.findRangeOfRecordsFiltered(filters);
return new RecallsGrid(recalls);
}
#Transactional()
public String saveRecalls(String[] ids, String[] statuses, RecallsGrid recalls) {
List<DsXCT_Recalls> recallList = recalls.getRecalls();
List<DsXCT_Recalls> updatedRecallList = new ArrayList<DsXCT_Recalls>();
for (int i = 0; i < ids.length; i++) {
for (DsXCT_Recalls recall : recallList) {
if (recall.recallId.equals(ids[i])) { // recall is found in the list
if (!statuses[i].equals(recall.getStatus())) { // status has changed
recall.setStatus(statuses[i]);
updatedRecallList.add(recall);
}
}
}
}
return hibernateRecallsDao.saveAll(updatedRecallList);
}
}
The saveAll method of my DAO calls one update method of hibernate session by entity changed:
package recalls.dao.hibernate;
#Repository
public class HibernateRecallsDao implements RecallsDao {
#Autowired(required = true)
#Resource(name = "mySessionFactory")
private SessionFactory sessionFactory;
#SuppressWarnings("unchecked")
public List<DsXCT_Recalls> findRangeOfRecordsFiltered(GridFilters filters) {
return sessionFactory.getCurrentSession().createQuery("from DsXCT_Recalls r WHERE SID = 0 ORDER BY Org, Bank, BIC, SetlDate").list();
}
public String saveAll(List<DsXCT_Recalls> recallList){
int count = 0;
for(DsXCT_Recalls recall:recallList){
sessionFactory.getCurrentSession().update(recall);
count++;
}
return count + " recalls were modified.";
}
}
So apparently the #Version must be above the attribute declaration and not above the getter method.. I am sure I saw this somewhere though. So much time wasted :(
I am using JPA, Hibernate and Spring MVC. In the controller class all the methods works greatly. When I test them in the web browser the public String getModuleFormation(long id) method, that returns an object, and it gives me the following error:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
as a root cause, but yesterday I tried it, and it worked without problem in the localhost:45045/GestionModules/detail/xx URL.
What could cause this problem?
My detail.jsp:
<c:if test="${!empty detailModule}">
${detailModule.idModule}
${detailModule.libModule}
</c:if>
POJO Class + JPA :
#Entity
#Table(name="ModuleFormation")
public class ModuleFormation {
private long idModule;
private String libModule;
public ModuleFormation() {
// TODO Auto-generated constructor stub
}
public ModuleFormation(String libModule) {
this.libModule = libModule;
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "seqModule")
#SequenceGenerator(name="seqModule", sequenceName = "seqModuleFormation")
#Column(name="idModule")
public long getIdModule() {
return this.idModule;
}
public void setIdModule(long idModule) {
this.idModule = idModule;
}
#Column(name="libModule", nullable=false, length = 100)
public String getLibModule() {
return this.libModule;
}
public void setLibModule(String libModule) {
this.libModule = libModule;
}
}
DAO Class :
#Repository
public class ModuleFormationDAOImpl implements ModuleFormationDAO {
#Autowired
private SessionFactory sessionFactory;
public void ajouterModuleFormation(ModuleFormation module) {
sessionFactory.getCurrentSession().save(module);
}
public void supprimerModuleFormation(long idModule) {
ModuleFormation module = (ModuleFormation) sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule);
if(module != null)
sessionFactory.getCurrentSession().delete(module);
}
public List<ModuleFormation> listModuleFormation() {
return sessionFactory.getCurrentSession().createQuery("from ModuleFormation")
.list();
}
public ModuleFormation getModuleFormation(long idModule) {
return (ModuleFormation) sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule);
}
public void majModuleFormation(ModuleFormation module) {
sessionFactory.getCurrentSession().merge(module);
}
}
Service Class :
#Service
public class ModuleFormationServiceImpl implements ModuleFormationService {
#Autowired
private ModuleFormationDAO moduleDao;
#Transactional
public void ajouterModuleFormation(ModuleFormation module) {
moduleDao.ajouterModuleFormation(module);
}
#Transactional
public void supprimerModuleFormation(long idModule) {
moduleDao.supprimerModuleFormation(idModule);
}
#Transactional
public List<ModuleFormation> listModuleFormation() {
return moduleDao.listModuleFormation();
}
#Transactional
public ModuleFormation getModuleFormation(long idModule) {
return moduleDao.getModuleFormation(idModule);
}
#Transactional
public void majModuleFormation(ModuleFormation module) {
moduleDao.majModuleFormation(module);
}
}
Controller Class :
#Controller
public class ModuleFormationController {
#Autowired
private ModuleFormationService moduleService;
#RequestMapping("/module")
public String listModulesFormations(Map<String, Object> map) {
map.put("module", new ModuleFormation());
map.put("moduleList", moduleService.listModuleFormation());
return "module";
}
#RequestMapping(value = "/ajouter", method = RequestMethod.POST )
public String ajouterModuleFormation(#ModelAttribute("module")
ModuleFormation module,BindingResult result) {
moduleService.ajouterModuleFormation(module);
return "redirect:/module";
}
#RequestMapping(value = "/supprimer/{idModule}")
public String supprimerModuleFormation(#PathVariable("idModule")
long idModule) {
moduleService.supprimerModuleFormation(idModule);
return "redirect:/module";
}
#RequestMapping(value= "/detail/{idModule}")
public String getModuleFormation(#PathVariable("idModule")
long idModule,Map<String, Object> map) {
map.put("detailModule", moduleService.getModuleFormation(idModule));
return "/detail";
}
#RequestMapping(value= "/detail/modifier", method = RequestMethod.POST )
public String majModuleFormation(#ModelAttribute("detailModule")
ModuleFormation module, BindingResult result) {
moduleService.majModuleFormation(module);
return "detail/{idModule}";
}
}
The Javadoc on the Hibernate Session#load(Class, Serializable) method says:
Return the persistent instance of the given entity class with the given identifier,
assuming that the instance exists. This method might return a proxied instance that
is initialized on-demand, when a non-identifier method is accessed.
When you access a property on the object in your JSP the session which loaded the object has been closed.
Use Session#get(Class, Serializable) to ensure that you don't load a proxy.
Instead of sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule), have you tried sessionFactory.getCurrentSession().get(ModuleFormation.class, idModule)?