java - Using JPA/Hibernate in Play2.6, create a new Entity that has a reference to a foreign key and redirects client once operation is done -


tl;dr problem statement

i building web application using play2.6 framework, jpa/hibernate , mysql want create user has foreign key employee, redirect client destination url can see result.

my actual problem

i complete beginner of jpa, hibernate, play framework 2.6 , java async architecture , having trouble inserting objects database depend on foreign key (or object reference in jpa). having trouble redirecting http client correct landing page vía http redirections using completionstage , results, wrapping result in completionstage.

i still learning jpa/hibernate, java persistence , java async, overwhelming task has me going in circles in inception kind of sense (to learn a, need learn b, ....). such, sure failing understand fundamental rules lead compilation error.

what doing

i trying create async web application using play framework 2, jpa/hibernate , mysql. web application simple employee repository company keeps track of employee history (vacations, job posts, evaluations, etc). have database setup, talk first 2 tables: useraccounts , employee.

for now, let's simplify both tables/entities follows:

    @entity     public class user{      @id     private string username;      private employee employee;      @basic     @column(name = "password", nullable = false, length = 32)     private string password;      @basic     @column(name = "email", nullable = false, length = 255)     private string email;      @onetoone     @joincolumn(name ="emp_fk", nullable = false)     public employee getemployee() {             return empleado;     }//end of getemployee      // other getters, setters     }//class user      @entity     public class employee{      @id     @generatedvalue(strategy = generationtype.auto)     @colum(name = "empid", nullable = false")     private long id;      @basic     @column(name = "firstname", nullable = false)     private string firstname;      @basic     @column(name = "lastname", nullable = false)     private string lastname;      private user user;      @onetoone(mappedby = "employee", fetch = fetchtype.lazy)     public getuser(){ return user;}     //getters, setters     } 

a simplified version of web application's jpa repository

(this part of model in mvc architecture)

    public class userjparepository implements repository{      private final play.db.jpa.jpaapi jpaapi;     private final databaseexecutioncontext excecutioncontext;      @inject     public userjparepository(jpaapi jpaapi, databaseexecutioncontext executioncontext){     this.jpaapi = jpaapi;     this.executioncontext = executioncontext;     }      @overrid     public completionstage<user> add(user object) {             return supplyasync(() -> wrap(em -> insert(em, object)), executioncontext);      //we turning syncronous call asyncronous 1 insert object user database using jpa persistence methodology.      //em entitymanager gives our connections database , handles jpa activities.     }//end of method add      private <t> t wrap(function<entitymanager, t> function) {             return jpaapi.withtransaction(function);         }      private user insert(entitymanager em, user user){      em.gettransaction.begin();     em.persist(user);     em.gettransaction.commit();     em.commit();      return em.find(user.class, user);      }      }//class userjparepository 

this simplified version of web application's businesslogic called controller.

    public class usercontroller extends play.mvc.controller{      private final play.data.formfactory formfactory; //this binds user input each attribute in user class/entity     private final play.libs.concurrent.httpexecutioncontext ec;     private final userjparepository userrepository; //in original project implementing interface called userrepository (if @ play java jpa example project linked @ end of stackoverflow question) @ userjparepository.      @inject     public usercontroller(formfactory formfactory, httpexecutioncontext ec, userrepository userrepository, empleadorepository empleadorepository) {     this.formfactory = formfactory;     this.ec = ec;     this.userrepository = userrepository;     this.empleadorepository = empleadorepository;     }//constructor via injection      /* simulating behavior use case have employee      stored in our database , need assign employee      user account web application.*/      public completionstage<result>  newemployeeuser(employee employee, string username, string email){     return completablefuture.supplyasync(() -> {         user user = new user();          user.setemployee(employee);         user.setusername(username);         user.setemail(email);          return userrepository.add(user).thenapplyasync(newuser ->{             return completablefuture.supplyasync(() -> {                  return redirect(controllers.entitites.routes.usercontroller.getusers());                 //controllers.entities.routes.usercontroller.getusers() play 2 framework reverse url binding                 //that redirects client url "http:myapp.com/userhome"              }, ec.current());         }, ec.current());      }, ec.current()); }       }//class usercontroller 

i failing execute , compile usercontroller method called newemployeeuser following compilation error.

    incompatible types. required completionstage<result> 'supplyasync' inferred completablefuture<u>: no instance(s) of type variable(s) u exist completionstage<result> conforms result. 

where i've tried learning from

as such, have immersed myself in tutorials found in play framework's download page (https://playframework.com/download#seeds); following example called play java jpa example. have begun studying jpa & hibernate books bought via amazon lightweight introduction hibernate madhusudhan konda (i not sponsoring books or anything, wanted show sources). finally, complementing 2 sources several online tutorials , answers handling entity - table relationships, bidirectional jpa relationships , more.

any , help, advice, constructive criticism welcome. thank in advance.

ps. have looked many stackoverflow threads involving jpa/hibernate , connecting datasource, jpa/hibernate foreign key mapping, issues completionstage u , results lead incompatible return types methods , more.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -