0

I have a query which works fine when I use NativeQuery but I need to use an EJB query for my problem. Can anyone help me reformat my query so it would work for EJB also?

Here is the Native version

Query query = getEntityManager.createNativeQuery("WITH src as ("
+ "Select distinct TRUNC(CreationDate) a" 
+ "From myTable)"
+ "Select src.a from src Order by src.a desc");

The Class for myTable is an Entity type. The Properties are as followed

    @Id
    private Long Id; 

    @Column(nullable = false)
    private String Name;

    @Column(length = 30, nullable = false)
    private Timestamp CreationDate;

The properties are as above with setters and getters for them. (Except for ID) Thanks

Salman9
  • 245
  • 1
  • 4
  • 17

2 Answers2

0

SELECT DISTINCT 
    TRUNC(CreationDate) as a
        FROM 
            (SELECT CreationDate  FROM myTable ORDER BY CreationDate DESC)

0

Try this:

Query query = getEntityManager.createQuery(
                "select DISTINCT TRUNC(src.CreationDate) 
                   from myTable src 
                  ORDER by src.CreationDate"
              );
List<Object[]> rows = query.list();
for (Object[] row: rows) {
    System.out.println("My date" + row[0]);
}
neshkeev
  • 6,280
  • 3
  • 26
  • 47
  • It does not work, order by would not work when you are using TRUNC in your select statement. It generates a GrammerException – Salman9 Jul 31 '14 at 18:03
  • What is wrong with `order by`? Get rid it during the debugging process. – neshkeev Jul 31 '14 at 18:05