About the problem that `` `javax.ejb.EJBException``` appears when trying to display database information in the view. I tried to summarize what I did until the solution as a memorandum.
① Save the entered value in the database ② Display the information saved in the database on the page
When I executed ② with the following code, I got an error `` `javax.ejb.EJBException```.
index.xhtml (Below the body tag)
<h2>Data creation</h2>
<h:form>
<h:panelGrid columns="2">
number:
<h:inputText value ="#{backigBean.number}" />
name:
<h:inputText value ="#{backigBean.name}" />
<h:commandButton value="Create" action="#{backigBean.create}" />
<h:commandButton value="cancel" type="reset" />
</h:panelGrid>
<br/>
<br/>
<h2>List of created information</h2>
<h:dataTable value="#{backigBean.getAll}" var="item">
<h:column>#{item.number}</h:column>
<h:column>#{item.name}</h:column>
<h:column><h:commandLink value="Edit" action="#{backigBean.toReturn}" /></h:column>
<h:column><h:commandButton value="Delete" action="#{backigBean.delete(item)}" /></h:column>
</h:dataTable>
<h:commandButton value="Return" action="#{backigBean.toReturn}" />
</h:form>
BackingBean.java (Business logic code)
package backingBeans;
import ejb.DataManager;
import entity.Data;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@SessionScoped
@ManagedBean
public class BackingBean implements Serializable{
private Integer number;
private String name;
@EJB
private DataManager dm;
//Data storage
public String create(){
Data data = new Data();
data.setNumber(number);
data.setName(name);
if(number == null) return null;
try{
dm.create(data);
}catch(javax.ejb.EJBException e){}
clear();
return null;
}
//Get all data from DB
public List<Data> getAll(){
return dm.getAll();
}
//Delete data
public String delete(Data data){
dm.delete(data);
return null;
}
//Clear browser
public void clear(){
number = null;
name = null;
}
//Return to the same page
public String toReturn(){
return null;
}
//Getters & Setters
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Data.java (Data class)
package entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Data implements Serializable{
@Id
private Integer number;
private String name;
public Data(){}
public Data(Integer number, String name){
this.number = number;
this.name = name;
}
//Getters & Setters
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DataManager.java (Code that mediates between DB and data)
package ejb;
import entity.Data;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class DataManager {
@PersistenceContext
private EntityManager em;
//Save input value in DB
public void create(Data data){
em.persist(data);
}
//Delete data on DB
public void delete(Data data){
em.remove(em.merge(data));
}
//Get all the data on the DB
public List<Data> getAll(){
return em.createNamedQuery("backingBean.getAll").getResultList();
}
}
Terminal
$ ~/Library/Caches
$ rm -rf NetBeans
Then restart NetBeans. ... but nothing changed. (Reference site: http://d.hatena.ne.jp/tkrd/20140818/1408315483)
glassfish-resources.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.apache.derby.jdbc.ClientDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="derby_net_test_appPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="localhost"/>
<property name="portNumber" value="1527"/>
<property name="databaseName" value="test"/>
<property name="User" value="testuser"/>
<property name="Password" value="test"/>
<property name="URL" value="jdbc:derby://localhost:1527/test"/>
<property name="driverClass" value="org.apache.derby.jdbc.ClientDriver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="java:app/jdbc/test" object-type="user" pool-name="derby_net_test_appPool"/>
</resources>
Perhaps in this resource tag, it says, "In this project, the server will connect to this database." When I connect to multiple databases, a lot of database information is automatically generated, so the server doesn't know which database to connect to ... I think. So edit the resource tag so that it appears only once in this file.
This solves the server problem!
This time I got another error. It seems that getAll is not good, so change the relevant part of the view file to all.
index.xhtml
...(abridgement)
<h2>List of created information</h2>
<h:dataTable value="#{backigBean.all}" var="item">
Then, `javax.ejb.EJBException`
will appear again.
Therefore, change the code of DataManager to the following function.
DataManager
//...(abridgement)
//Get all the data on the DB
public List<Data> getAll(){
//return em.createNamedQuery("backingBean.getAll").getResultList();Change here to
return em.createQuery("SELECT c FROM Data c").getResultList();
}
moved! !!
Recommended Posts