1

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.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Yubi Regmi
  • 11
  • 1

1 Answers1

2

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:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Just a quick follow-up, the ONLY time it is okay to `@Inject` entities is if you create a producer for them. If you let CDI manage them, they will not be persistable. Both CDI and JPA rely on proxies or bytecode manipulation and they don't play well together. – LightGuard Sep 09 '13 at 19:37
  • @LightGuard can you please show an example of this? Just in order to improve the answer with more accurate info. – Luiggi Mendoza Sep 09 '13 at 20:27
  • An example of a producer? – LightGuard Sep 09 '13 at 23:51
  • @LightGuard I haven't injected a JPA entity nor in EJB nor in CDI managed bean. If you can show an example where this is expected, then do it, or even post your own answer. – Luiggi Mendoza Sep 09 '13 at 23:59
  • Well, the classic example would be the currently logged in user entity. Something like `@Inject @LoggedIn User currentUser`. If you do this without a producer it 1) Probably wouldn't be filled in with info you want and 2) would not be persistable for reasons mentioned above. – LightGuard Sep 10 '13 at 21:13