我有一个实体是产品实体:
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;
}
}
和DAO看起来像这样:
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()));
}
}
我还在应用程序类中初始化了hibernateBundle并将资源注册到了环境中:
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));
}
}
现在,当我测试此查询时,我在服务器日志中出现500错误和Named query not known: <name of the query>
错误
答案 0 :(得分:1)
尝试将javax.persistence @Entity注释添加到您的产品类中。