java - Hibernate - fetch data from multple tables using many to one annotation -


i have 3 tables like:

car

carid|carname|carprice| 100|bmw|10l| 200|honda|5l| .. .. 

cartype

cartypeid|type| 1|suv| 2|xuv| 3|sedan| 

carconfig

carid|cartypeid| 100|3| 200|3| 

and have entities 3 tables.

below have pasted code joining table carconfig:

@entity @table(name = "carconfig", uniqueconstraints = {          @uniqueconstraint(columnnames = { "carid" } ) }) public class carconfig {      @id     long carid;      @onetoone     @joincolumn(name = "carid", insertable = false, updatable = false)     car car;      @manytoone(optional = false)     @joincolumn(name = "cartypeid")     cartype cartype;      public carconfig() {         super();     }      public carconfig(long carid, cartype cartype) {         super();         this.cartype = cartype;         this.carid = carid;     }      public car getcar() {         return car;     }      public void setcar(car car) {         this.car = car;     }      public cartype getcartype() {         return cartype;     }      public void setcartype(cartype cartype) {         this.cartype = cartype;     }      public long getcarid() {         return carid;     }      public void setcarid(long carid) {         this.carid = carid;     }  } 

my problem :

i want know how can data both tables car , cartype , write csv.

below code data.

public string downloadcars(carform form, model model,httpservletrequest request, httpservletresponse response)throws ioexception {         printwriter pw = null;          try {             logger.debug("+++++++++++++++++++++++++++++++++++++++");             response.setcontenttype("application/vnd.ms-excel");             response.setheader("content-disposition","attachment; filename=car" + ".csv");             response.setheader("expires", "0");             response.setheader("cache-control","must-revalidate, post-csheck=0, pre-check=0");             response.setheader("pragma", "public");             pw = response.getwriter();             // get, write , flush data.             pw.println(" id,name,price,type");             list<car> carfromdb = collections.emptylist();                (car car : carfromdb) {                              pw.println(string.format("%s,%s,%s,%s,%s",              // need              }             pw.flush();          } catch (exception ex) {             logger.error("error while downloading car", ex);             model.addattribute("failuremsg","error while downloading cars");         }         return null;     } 

you can using hql join query, this:

list<object[]> carswithdetails = session.createquery(     "select distinct car.carid, car.carname, car.carprice, cartype.type " +     "from car  car join car.cartype cartype ")     .list(); 

then iterate through list of object[] array extract car details using iterator:

iterator<object[]> = carswithdetails.iterator(); while(it.hasnext()){   object[] car = (object[]) it.next();   system.out.println("car details:  ");   system.out.println("id: "+car[0]);   system.out.println("name: "+car[1]);   system.out.println("price: "+car[2]);   system.out.println("type: "+car[3]); } 

and of course change loop fit needs, add code write csv instead of system.out.println lines.

it replace loop:

for (car car : carfromdb) {                          pw.println(string.format("%s,%s,%s,%s,%s",          // need          } 

edit:

if join 3 tables need add join reference join car.carconfig carconfigto query, this:

list<object[]> carswithdetails = session.createquery(     "select distinct car.carid, car.carname, car.carprice, cartype.type " +     "from car  car join car.cartype cartype join car.carconfig carconfig")     .list(); 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -