Jacob
Jacob

Reputation: 14741

How to display date and string when using @NamedQuery

I have the following @NamedQuery in my Entity:

@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c")

I am using the above in DAOImpl class as

TypedQuery<Customer> q =  entityManager.createNamedQuery(
    "Customer.findAll", Customer.class);

When I am displaying date fields in application, all date values are displayed as

2013-07-14 00:00:00.0, 2013-07-28 00:00:00.0 etc and String fields are displayed as

test.entity.Employee@750d24b2

How can I format date and display string data as stored in database?

Edit 1

<p:column>
    <f:facet name="header">
        <h:outputText value="Employee #" />
    </f:facet>
    <h:outputText value="#{req.empNumber}" />
</p:column>

and in Entity empNumber as

it is declared as

private Employee empNumber;

Upvotes: 0

Views: 682

Answers (2)

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

If your receiving test.entity.Employee@750d24b2 your not accessing a String on the JSF, instead your accessing a class via JSF EL.

You should access a field on the Employee class like:

<h:outputText value="#{req.employee.empNumber}" />

This assumes that the req attribute contains get/set methods for employee and employee contains get/set methods for empNumber. With these accessors in place JSF EL should display the value of the empNumber.

For displaying the date, I would recommend using JSF tags.

<h:outputText value="#{req.someDate}" >
              <f:convertDateTime pattern="dd.MM.yyyy HH:mm" />
 </h:outputText>

Documentation

Upvotes: 2

damian
damian

Reputation: 4054

Displaying a date as a formatted string in JSF has nothing to do with @NamedQuery or however you choose to fetch that date from database. Anyways, to show formatted date,

<h:outputText value="#{bean.date}" >
    <f:convertDateTime pattern="dd/MM/yyyy" />
</h:outputText>

replace with actual value and desired pattern.

As for the Employee, when you use an Object (instead of a String) as the value of a h:outputText, the toString() method of the object is invoked. That is why you see what you see. you should be using a property of the employee, like #{req.employee.empNumber}

Upvotes: 1

Related Questions