Q: JSF/JPA value binding expression not setting entity bean's property I am learning JSF/EJB and I have run into a problem.
I am trying to write a code that takes a string from user and store that string to database.
Here's my code:
Entity bean:
@Entity

public class TestTable implements Serializable {


private static final long serialVersionUID = 1L;

public TestTable() {
    super();
}

@Id
@GeneratedValue
private int firstcolumn;
private String secondcolumn;

private String testphrase = "test phrase";


public String getTestphrase() {
    return testphrase;
}
public void setTestphrase(String testphrase) {
    this.testphrase = testphrase;
}
public int getFirstcolumn() {
    return firstcolumn;
}
public void setFirstcolumn(int firstcolumn) {
    this.firstcolumn = firstcolumn;
}
public String getSecondcolumn() {
    return secondcolumn;
}
public void setSecondcolumn(String secondcolumn) {
    this.secondcolumn = secondcolumn;
}



}



*

*Table has three columns, first column is primary key, second column stores string entered by user and third column stores "test phrase".


Controller bean:
@Named
public class TestController  implements Serializable {

private static final long serialVersionUID = 1L;

@EJB
DataAccess dacc;


@Inject
TestTable testTable;

public TestController()
{

}



public TestTable getTestTable() {
    return testTable;
}



public void setTestTable(TestTable testTable) {
    this.testTable = testTable;
}



public void test()
{

    System.out.println("string secondcolumn= "+ testTable.getSecondcolumn());
    dacc.addtodb(testTable);


}

}



*

*I used System.out.println("string secondcolumn= "+ testTable.getSecondcolumn()); in method test() to check data before it is written to database. My problem is, it is always null. Output in console : 
INFO :string secondcolumn= null .


secondcolumn is not set by value binding expression in JSF.
Now, JSF:
        <h:outputText value="Second column:">
        </h:outputText>


        <h:inputText label="Second column" value="#{testController.testTable.secondcolumn}">                
        </h:inputText>


        <h:outputText value="#{testController.testTable.getTestphrase()}">
        </h:outputText>

        <h:commandButton action="#{testController.test}" value="Save">
    </h:commandButton>

I checked database and rows are being added. Entries in Column SECONDCOLUMN are NULL.
Entries in TESTPHRASE are "test phrase". I get no error messages and I have tried everything I can to solve the problem and now I am stuck. Any feedback are welcome.   

A: Your problem is that you're injecting an entity class. The best will be initializing it manually using the new keyword, retrieving the entity from database, etc. One way to do this would be using a @PostConstruct method in your CDI bean:
@Named
//here you should define the scope of your bean
//probably @RequestScoped
//if you're working with JSF 2.2 there's already a @ViewScoped
public class TestController  implements Serializable {

    private static final long serialVersionUID = 1L;
    @EJB
    DataAccess dacc;
    //this musn't be injected since it's not a business class but an entity class
    //@Inject
    TestTable testTable;

    public TestController() {
    }

    @PostConstruct
    public void init() {
        //basic initialization
        testTable = new TestTable();
    }

    //rest of your code...
}

With this change, JSF will be able to set the values from the <h:form> into the bounded fields. Note that JSF code will just invoke the necessary getters and setters accordingly to the defined in your EL, it won't create a new instance of the bounded fields. The getters are invoked when generating the view and the setters when submitting the form to the server.
More info:


*

*Creating new entities while enabling injection

*Why use @PostConstruct?

*StackOverflow Expression Language wiki
