[JAVA] What I did when JSF couldn't display database information in the view

image.png

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.


What you are using

What you want to do & problems

① Save the entered value in the database ② Display the information saved in the database on the page

image.png

problem

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&nbsp;(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&nbsp;(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&nbsp;(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();
    }
    
}

3 things I tried to solve

1. Clear the cache

Terminal


$ ~/Library/Caches
$ rm -rf NetBeans 

Then restart NetBeans. ... but nothing changed. (Reference site: http://d.hatena.ne.jp/tkrd/20140818/1408315483)

2. Narrow down the resources tag in `` `glassfish-resources.xhtml``` to one

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!

3. Change the function to use

This time I got another error. image.png 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! !!

Summary

Recommended Posts

What I did when JSF couldn't display database information in the view
[Rails] How to display information stored in the database in view
I want to display an error message when registering in the database
[Java] Integer information of characters in a text file acquired by the read () method
What I did when JSF couldn't display database information in the view
Japanese characters described in MessageResources.properties are garbled
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
[java] What I did when comparing Lists in my own class
About what I did when creating a .clj file in Clojure
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
What did I prepare for when I entered an Android application development project while developing the Web in Java?
What to do when the value becomes null in the second getSubmittedValue () in JSF Validator
What to do when rails db: seed does not reflect in the database
What I did when I converted java to Kotlin
[Summary] What I noticed and did when I was told that the Oracle JDK was paid
What I learned when building a server in Java
[Rilas] What I learned in implementing the pagination function.
[Rails] How to display an image in the view
What I stumbled upon in the ActiveModel :: Serializer test
What I was addicted to when introducing the JNI library
What happens when I do new => build => save! In ActiveRecord
What I did when I stumbled on IntelliJ gradle 2.2 → 2.6 environment migration
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
What Android development beginners did before releasing the app in 2 weeks
What if I write a finally clause in the try-with-resources syntax?
What to do when the changes in the Servlet are not reflected
[rails] How to display parent information in child view in nested relationships
I want to display the images under assets/images in the production environment
How to write the view when Vue is introduced in Rails?
What I checked when I installed Docker Hub in a Windows 10 home environment but it did not start
What I did when I was addicted to the error "Could not find XXX in any of the sources" when I added a Gem and built it