java - How to get data from a form and store it in multiple tables in spring mvc hibernate -


i have these classes:

article class:

@entity @table(name = "imei") public class article {     @id     //@generatedvalue     @column(name = "imei1",nullable = false)     private long imeino;     @column(name = "date_added")     @temporal(javax.persistence.temporaltype.timestamp)         private date addeddate;     public article() {           }         public long getimeino() {         return imeino;     }     public void setimeino(long imeino) {         this.imeino = imeino;     }         public date getaddeddate() {         return addeddate;     }      public void setaddeddate(date addeddate) {         this.addeddate = addeddate;     }    

articledao class:

public interface articledao { // save article detail public void savearticle(article article );  // list of articles public list<article> listarticles(); } 

articledaoimpl class:

@repository("articledao") public class articledaoimpl implements articledao {      @autowired     private sessionfactory sessionfactory;      // save article detail     public void savearticle(article article) {         article.setaddeddate(new date());          sessionfactory.getcurrentsession().saveorupdate(article);     }      // list of articles     @suppresswarnings("unchecked")     public list<article> listarticles() {                return (list<article>) sessionfactory.getcurrentsession().createcriteria(article.class).list();     } } 

articlecontroller:

@controller @requestmapping("/articles") public class articlecontroller {      @autowired     private articleservice articleservice;      @requestmapping(value = "/save", method = requestmethod.post)     public modelandview savearticle(@modelattribute(" article") article  article,             bindingresult result) {          articleservice.addarticle( article);         return new modelandview("redirect:/articles.html");     }      @requestmapping(method = requestmethod.get)     public modelandview listarticles() {         map<string, object> model = new hashmap<string, object>();         model.put("articles",  articleservice.listarticles());          return new modelandview("articleslist", model);     }      @requestmapping(value = "/add", method = requestmethod.get)     public modelandview addarticle(@modelattribute("article") article article,             bindingresult result) {         return new modelandview("addarticle");     }  } 

articleservice:

public interface articleservice {  public void addarticle(article article);  public list<article> listarticles(); } 

articleserviceimpl class:

@service("articleservice") @transactional(propagation = propagation.supports, readonly = true) public class articleserviceimpl implements articleservice {      @autowired     private articledao articledao;      public articleserviceimpl() {     }      @transactional(propagation = propagation.required, readonly = false)     public void addarticle(article article) {         articledao.savearticle(article);     }      public list<article> listarticles() {         return articledao.listarticles();     }  } 

now created class, onlyimei.java

@entity @table(name = "onlyimei") public class onlyimei { @id     @column(name = "imei1",nullable = false) private long imeino;     public onlyimei() {      } public long getimeino() {     return imeino; }       public void setimeino(long imeino) {     this.imeino = imeino; }   } 

can tell me how enter values in 2 tables @ 1 time? have enter imei number in 1 table , imei , date , time in second table. have made second table don't know how enter values @ 1 time in 2 tables.

use service method. write both tables within transaction. don't see second dao class, i'm making 1 example.

@transactional(propagation = propagation.required, readonly = false) public void addarticletobook(article article, book book) {     articledao.savearticle(article);     bookdao.addarticle(article, book.getid()); } 

the work done in same transaction.

actually, above isn't best. given way hibernate supports relationships, should able if have configured hibernate correctly , used spring hibernate template:

@transactional(propagation = propagation.required, readonly = false) public void addarticletobook(article article, long bookid) {     this.sessionfactory.getcurrentsession()                        .loadbookbyid(bookid)                        .getarticles()                        .add(article); } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -