问题
I want to fill a simple h:datatable tag with some values from a database. But i get an exception and i cant find what is the reason:
java.lang.NumberFormatException: For input string: "url"
This is how i create the datatable:
<h:form>
<h:dataTable value="#{managementBB.retrieveRecords()}" var="record">
<h:column>
<f:facet name="header">URL</f:facet>
#{record.url}
</h:column>
<h:column>
<f:facet name="header">Submition date</f:facet>
#{record.submitionDate}
</h:column>
<h:column>
<f:facet name="header">Unacceptable</f:facet>
#{record.unnaceptableContent}
</h:column>
<h:column>
<f:facet name="header">Option</f:facet>
Something
</h:column>
</h:dataTable>
</h:form>
This is the backing bean behind this page:
@Named("managementBB")
@SessionScoped
public class ManagementBB implements Serializable{
@EJB
private ILinkManagerEJB linkManagerEJB;
public List<Record> retrieveRecords() {
return linkManagerEJB.retrieveRecords();
}
}
This is the EJB that accesses the database to get the data:
@Stateless(name = "ejbs/LinkManagerEJB")
public class LinkManagerEJB implements ILinkManagerEJB {
@PersistenceContext
private EntityManager entityManager;
public List<Record> retrieveRecords() {
Query allRecords = entityManager.createNamedQuery("allrecordinfo");
return (List<Record>) allRecords.getResultList();
}
}
And finally this is the JPA entity that represents a row in the database:
@Entity
@NamedQueries({@NamedQuery(name = "allrecordinfo",
query = "SELECT r.url, r.submitionDate, r.unnaceptableContent FROM Record r")})
public class Record {
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String url;
@Column(nullable = false)
private String submitionDate;
@Column(nullable = false)
private boolean unnaceptableContent;
//Get set methods ...
}
As you see it looks simple, and I've done this before but now I am confused, I don't know why is not working. Could you help me find my error?
Note: I am pretty confident that the query syntax is ok (I tested it in eclipse's scrapbook)
回答1:
The query is not correct.
@NamedQueries({ @NamedQuery(name = "allrecordinfo", query = "SELECT r FROM Record r") })
@Entity
public class Record {
}
回答2:
java.lang.NumberFormatException: For input string: "url"
#{record.url}
This suggests that Record
is actually an Object[]
because it is expecting an integer index like so
#{record[0]}
Indeed, your query is wrong, it is not selecting the Record
, but only individual fields, which would return a List<Object[]>
with the individual fields instead of List<Record>
. The cast (which would have generated a warning) doesn't prevent/change that.
回答3:
To prevent an error like this in the future, you should use a typed query instead of a cast.
Casting the query result from a typeless query is a rather old practice (from 2006 actually) that should not be used anymore.
Some extra hints: you don't have to specify an explicit name for your EJB. Just @Stateless is enough. The same holds for the CDI bean, which will get a default name.
Then, you might want to rethink the name of the EJB's interface. The initial I is a bit frowned upon lately.
Also, it's more common to create a getter for the data you're returning from the backing bean. After that you can use a value binding (ommit the () in the EL experssion).
Finally, you may want to move the call to your EJB to an @PostConstruct method, as JSF might call your backing bean's methods many times, which will now result in a DB call every time. If you do that, your bean should also be best in view scope, but that means you either have to go from CDI beans to regular JSF managed beans, or use seam 3.
来源:https://stackoverflow.com/questions/7216488/number-format-exception-when-returning-values-from-databasejpa