-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate throws Named query not known exception
PostPosted: Sun Oct 16, 2016 6:02 am 
Newbie

Joined: Sun Oct 16, 2016 6:00 am
Posts: 1
I have one entity which is the Product entity:

Code:
package com.amitbaz.tradingsystem;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;
import org.hibernate.annotations.Table;

@DynamicUpdate(true)
@Table(appliesTo = "productTable")
@NamedQueries({
    @NamedQuery(name = "com.amitbaz.tradingsystem.product.GetAll", query= "select p from Product p"),
    @NamedQuery(name = "com.amitbaz.tradingsystem.product.GetByName", query= "select p from Product p where p.fullName like :name")

})
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "product_id")
    private int id;

    @Column(name="product_name")
    private String fullName;

    @Column(name="product_info")
    private String info;

    @Column(name="product_price")
    private float price;

    @Column(name="product_base_currency")
    private String baseCurrency;

    public Product(String fullName, String info, float price, String baseCurrency) {
        this.fullName = fullName;
        this.info = info;
        this.price = price;
        this.baseCurrency = baseCurrency;
    }
}


and the DAO looks like this:

Code:
package com.amitbaz.tradingsystem;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import java.util.List;

import io.dropwizard.hibernate.AbstractDAO;

public class ProductDAO extends AbstractDAO<Product>{


    public ProductDAO(SessionFactory sessionFactory) {
        super(sessionFactory);
        // TODO Auto-generated constructor stub
    }

    public List<Product> getAll(){
        return list(namedQuery("com.amitbaz.tradingsystem.product.GetAll"));
    }

    public List<Product> getByName(String name){
        StringBuilder builder = new StringBuilder("%");
        builder.append(name).append("%");
        return list(namedQuery("com.amitbaz.tradinsystem.product.GetByName").setParameter("name", builder.toString()));
    }

}


I also initialized the hibernateBundle in the application class and registered the resource to the environment:

Code:
package com.amitbaz.tradingsystem;


import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import org.hibernate.Hibernate;
import org.skife.jdbi.v2.DBI;

import io.dropwizard.Application;
import io.dropwizard.auth.AuthDynamicFeature;
import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.auth.CachingAuthenticator;
import io.dropwizard.auth.basic.BasicCredentialAuthFilter;
import io.dropwizard.auth.basic.BasicCredentials;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.db.PooledDataSourceFactory;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.jdbi.DBIFactory;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public class TradingSystemApplication extends Application<TradingSystemConfiguration>{

    public static void main(String[] args) throws Exception{
        new TradingSystemApplication().run(args);
    }

    private final HibernateBundle<TradingSystemConfiguration> HibernateBundle = new HibernateBundle<TradingSystemConfiguration>(Product.class){

        @Override
        public DataSourceFactory getDataSourceFactory(TradingSystemConfiguration config) {
            // TODO Auto-generated method stub
            return config.getDataSourceFactory();
        }


    };



    @Override
    public void initialize(Bootstrap<TradingSystemConfiguration> bootstrap) {
        // TODO Auto-generated method stub
        bootstrap.addBundle(HibernateBundle);
    }



    @Override
    public void run(TradingSystemConfiguration config, Environment env) throws Exception {
        // TODO Auto-generated method stub

        //final DBIFactory factory = new DBIFactory();
        //final DBI jdbi = factory.build(env, config.getDataSourceFactory(), "mysql");
        //final TestResource testRes = new TestResource();
        //final UserDAO dao = jdbi.onDemand(UserDAO.class);
        final ProductDAO productDAO = new ProductDAO(HibernateBundle.getSessionFactory());
        env.jersey().register(new ProductResource(productDAO));
        env.jersey().register(new TestResource());
        env.jersey().register(new AuthDynamicFeature(
                new BasicCredentialAuthFilter.Builder<User>()
                .setAuthenticator(new TradingSystemAuthenticator())
                .setAuthorizer(new TradingSystemAuthorizer())
                .setRealm("SHITTT")
                .buildAuthFilter()));

        env.jersey().register(RolesAllowedDynamicFeature.class);
        env.jersey().register(new AuthValueFactoryProvider.Binder<>(User.class));

    }

}


Now, when I'm testing this queries I get an 500 error and Named query not known: <name of the query> error in the server logs


Top
 Profile  
 
 Post subject: Re: dropwizard hibernate Named query not known
PostPosted: Mon Oct 17, 2016 5:28 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That;s because you have a typo in the named query key.

You need to change this:

Code:
return list(namedQuery("com.amitbaz.tradinsystem.product.GetByName").setParameter("name", builder.toString()));


to this:

Code:
return list(namedQuery("com.amitbaz.tradingsystem.product.GetByName").setParameter("name", builder.toString()));


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.