java - Simple application with Spring+Hibernate using annotation and repository -


i want create app use spring annotation configure beans , spring jparepository don't need create queries database. moment have created entity:

@entity public class executor extends abstractentity {      private string company;     private string responsible;     private string supervisor;      public executor() {             }      public executor(string company, string responsible, string supervisor) {         this.company = company;         this.responsible = responsible;         this.supervisor = supervisor;     } ... getters , setters... 

also have created repository:

public interface executorrepository extends jparepository<executor, integer> {      public list<executor> findbycompany(string company);      public list<executor> findbyresponsible(string responsible);      public list<executor> findbysupervisor(string supervisor); } 

the service:

public class executorservice {      @autowired     private executorrepository repository;      @transactional     public executor create(executor calibration) {         return repository.save(calibration);     }      @transactional(rollbackfor = calibrationnotfound.class)     public executor delete(int id) throws executornotfound {         executor deleted = repository.findone(id);         if (deleted == null) {             throw new executornotfound();         }         repository.delete(deleted);         return deleted;     }      @transactional     public list<executor> findall() {         return repository.findall();     }  } 

i have used annotation configure beans:

// informa que é uma classe que define beans. @configuration // cria o bean para o environment. @propertysource("classpath:configuration/configuration.properties")  @enabletransactionmanagement @enablejparepositories("com.metrum.persistence.repository") @componentscan("com.metrum.persistence") public class applicationconfig {       /**      * representa o ambiente da aplicação. modela profiles e propriedades.      * suas propriedades são carregadas através da anotação \@propertysource.      */     @autowired     private environment env;           /**      * produz um bean para datasource.      *      * o datasource carrega informações sobre o banco de dados e permite gerar      * conexões com este.      *      * @return datasource      */     @bean     public datasource datasource() {         drivermanagerdatasource datasource = new drivermanagerdatasource();          datasource.setdriverclassname(env.getrequiredproperty("db.driver"));         datasource.seturl(env.getrequiredproperty("db.url"));         datasource.setusername(env.getrequiredproperty("db.username"));         datasource.setpassword(env.getrequiredproperty("db.password"));          return datasource;     }      /**      * cria e retorna um bean para entitymanagerfactory.      *      * uma entitymanagerfactory cria entitymanagers para o mesmo banco de dados,      * com mesmas configurações. uma entitymanager permite acessar o banco de      * dados em busca de entities.      *      * @return entitymanagerfactorybean.      */     @bean     public localcontainerentitymanagerfactorybean entitymanagerfactory() {         localcontainerentitymanagerfactorybean entitymanagerfactorybean = new localcontainerentitymanagerfactorybean();         entitymanagerfactorybean.setdatasource(datasource());         entitymanagerfactorybean.setpersistenceproviderclass(hibernatepersistenceprovider.class);         entitymanagerfactorybean.setpackagestoscan(env.getrequiredproperty("entitymanager.packages.to.scan"));          entitymanagerfactorybean.setjpaproperties(gethibernateproperties());          return entitymanagerfactorybean;     }      /**      * retorna propriedades orm hibernate.      */     private properties gethibernateproperties() {         properties properties = new properties();         properties.put("hibernate.dialect", env.getrequiredproperty("hibernate.dialect"));         properties.put("hibernate.show_sql", env.getrequiredproperty("hibernate.show_sql"));         return properties;     }      /**      * bean para criação de uma jpatransactionmanager,       * implementação spring para o transações jpa.      *      * @return jpatransactionmanager      */     @bean     public platformtransactionmanager transactionmanager() {         final jpatransactionmanager transactionmanager = new jpatransactionmanager();         transactionmanager.setentitymanagerfactory(entitymanagerfactory().getobject());         return transactionmanager;     } } 

and application main:

public class application {      @autowired     private executorservice service;      public static void main(string[] args) {         annotationconfigapplicationcontext ctx = new annotationconfigapplicationcontext();         ctx.register(applicationconfig.class);         ctx.refresh();          application app = new application();         app.test();     }      public void test() {         executor executor = new executor("metrum sistemas de medição",                  "leandro santos de lima", "levy albuquerque");         service.create(executor);     } 

well, thought doing ok executorservice not injected receiving nullpointerexception.

i know i'm doing wrong.

thanks


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -