Manihtraa
Manihtraa

Reputation: 978

Using Entity manager and JPA, returns object only

When i run the following code it was return only object list. i want the list in vehicle. what is the error in my code?

My Dao Class Is:

public class VehicleDao {

HibernateTemplate template;  
private EntityManager emManager;

@PersistenceContext
public void setEm(EntityManager emManager) {
    this.emManager = emManager;
}
public List<Vehicle> getVehicleDataUsing_Sp()
{
Properties prop= new Properties();
    try {
        InputStream input = new FileInputStream("E:\\spring4AndHibernate5\\src\\Query.properties");
        prop.load(input);
        Query query = emManager.createNativeQuery(prop.getProperty("vehicleQuery"));
        List<Vehicle> list = query.getResultList();
        return list;
        }
}

Upvotes: 1

Views: 2000

Answers (3)

srp321
srp321

Reputation: 116

You either need to define the result class in your native query definition (use resultClass=Vehicle.class where you have defined your named query)

and/or

you need to map the native class when using createNamedQuery (use createNativeQuery(prop.getProperty("vehicleQuery"), Vehicle.class)).

Upvotes: 1

coladict
coladict

Reputation: 5095

Because it's a native query, it doesn't know what to try mapping it to, unless you tell it.

Try:

Query query = emManager.createNativeQuery(prop.getProperty("vehicleQuery"), Vehicle.class);

Upvotes: 1

Robo Mop
Robo Mop

Reputation: 3553

You can't return List<Vehicle> when you have specified the return type as void.

Try this instead:

public List<Vehicle> getVehicleDataUsing_Sp()

And you also need to add a catch block after the try block, like this:

catch(Exception e)
{
}

Upvotes: 1

Related Questions