无法在dropwizard应用程序中使用基本JPA

时间:2015-07-20 14:52:41

标签: hibernate session jpa dropwizard named-query

我正在尝试使用Dropwizard的JPA注释将对象持久化到数据库中。

要保留的对象

TheObject.java

    @JsonIgnoreProperties(ignoreUnknown = true)
    @Entity
    @Table(name = "TheObject")
    @NamedQuery(name = "com.comany.TheObject.findAll", query = "SELECT o FROM TheObject o")
    public classTheObjectimplements  Serializable{

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @JsonProperty
        String name;

        @OneToMany(cascade = CascadeType.ALL, mappedBy = "TheObject", fetch = FetchType.EAGER)

        public void setName(String name) {
            this.name = name;
        }

        public String getName(Long id) {
            return this.name;
        }

        public TheObject (String name) {
            this.name = name;
        }

    The data access object 

public TheObjectDAO(SessionFactory factory) {
        super(factory);
    }

    public List<TheObject> findAll() {

        return list(namedQuery("com.comapny.TheObject.findAll"));
    }

    public TheObject create(TheObject o) {
        return persist(o);

    }
}

The Application class 

    public class TheApplication extends Application<AppConfiguration> {
    private final static Logger log = Logger.getLogger(DeployerApplication.class.getName()); 

    private final HibernateBundle<AppConfiguration> hibernate = new HibernateBundle<AppConfiguration>(
        TheObject.class) {

        public DataSourceFactory getDataSourceFactory(
                AppConfiguration configuration) {
            return configuration.getDataSourceFactory();
        }

    };
    @Override
    public void run(AppConfiguration configuration, Environment environment) throws SQLException {



        final TheObjectDAO dao = new   TheObjectDAO(hibernate.getSessionFactory());
    environment.jersey().register(new TheObjectResource(dao));


 And finally the resource class

    public ObjectResource(TheObjectDAO edao) {
        this.dao = dao;
    }

    @GET
    @Timed
    @UnitOfWork
    public List<TheObject> getAllObjss() {
        return dao.findAll();

    }

当我获取此资源时,我总是收到错误“命名查询未知”。我错过了什么

1 个答案:

答案 0 :(得分:0)

知道了,namedQuery语法错了。应该是的        @NamedQueries({            @NamedQuery(                   name =&#34;&#34;,                   query =&#34;&#34;           )   })