7 Replies Latest reply on Sep 10, 2009 7:56 AM by sathish0707

    how to get values from database from Session file entitynamehome.java

    sathish0707

      i converting a specific table into csv file so i need to get values.i try to get by declaring a list we need without declaring get method

        • 1. Re: how to get values from database from Session file entitynamehome.java
          tmalatinszki

          Could You show this part of Your code (and the entity/entities also)?


          Regards,
          Tamas

          • 2. Re: how to get values from database from Session file entitynamehome.java
            sathish0707
            Click HELP for text formatting instructions. Then edit this text and check the preview.

            home.java page i used ur code in convertcsv method

            package com.sipl.hrms.session.admin;

            import java.io.FileWriter;
            import java.lang.annotation.Annotation;
            import java.lang.reflect.Field;
            import java.lang.reflect.Method;
            import java.text.DateFormat;
            import java.text.SimpleDateFormat;
            import java.util.Date;
            import java.util.List;

            import javax.mail.Session;
            import javax.persistence.EntityManager;

            import com.sipl.hrms.entity.admin.*;
            import com.sipl.hrms.entity.employee.Empaddress;

            import org.hibernate.persister.entity.AbstractEntityPersister;
            import org.jboss.seam.annotations.In;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.framework.EntityHome;

            @Name("housekeepingHome")
            public class HousekeepingHome extends EntityHome<Housekeeping> {

                 public void setHousekeepingReferenceId(String id) {
                      setId(id);
                 }

                 public String getHousekeepingReferenceId() {
                      return (String) getId();
                 }

                 
                 
                 @Override
                 protected Housekeeping createInstance() {
                      Housekeeping housekeeping = new Housekeeping();
                      return housekeeping;
                 }

                 public void wire() {
                      getInstance();
                 }

                 public boolean isWired() {
                      return true;
                 }

                 public Housekeeping getDefinedInstance() {
                      return isIdDefined() ? getInstance() : null;
                 }
                 List<Sysaudit> sysauditgettablevalues=null;
                 public void convertcsv()
                 {
                      int i=0;
                      DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                      Date save=new Date();
                      String s=dateFormat.format(save);
                      try
                      {
                           
                           org.hibernate.Session session = null;
                           AbstractEntityPersister aep=((AbstractEntityPersister)session.getSessionFactory().getClassMetadata(Sysaudit.class));
                           String[] properties=aep.getPropertyNames();
                                          
                           for(int nameIndex=0;nameIndex!=properties.length;nameIndex++){
                              System.out.println("Property name: "+properties[nameIndex]);
                              String[] columns=aep.getPropertyColumnNames(nameIndex);
                              for(int columnIndex=0;columnIndex!=columns.length;columnIndex++){
                                 System.out.println("Column name: "+columns[columnIndex]);
                              }
                           }
                           String eq = "select e from Sysaudit e ";
                           sysauditgettablevalues =this.getEntityManager().createQuery(eq).getResultList();
                           String path="c:\\audit"+s+".csv";
                           FileWriter writer = new FileWriter(path);
                           System.out.println(sysauditgettablevalues.size());
                           for(Sysaudit syaudi:sysauditgettablevalues)
                           {
                                     
                                          writer.append(syaudi.getEmpId());
                                          writer.append(',');
                                          writer.append(syaudi.getClientId());
                                          writer.append(',');
                                          writer.append(syaudi.getActions());
                                          writer.append(',');
                                          writer.append(syaudi.getActionShortDesc());
                                          writer.append(',');
                                          writer.append(syaudi.getProgramId());
                                          writer.append(',');
                                          writer.append(syaudi.getErrorInd());
                                          writer.append(',');
                                          writer.append(syaudi.getAuditCategory());
                                          writer.append(',');
                                          writer.append(syaudi.getRemarks());
                                          writer.append(',');
                                          writer.append(syaudi.getUserId());
                                          writer.append(',');
                                          writer.append(syaudi.getRemarks());
                                          writer.append(',');
                                          writer.append(syaudi.getAuditId());
                                          writer.append(',');
                                          writer.append(syaudi.getRemarks());
                                          writer.append(',');
                                          writer.append(syaudi.getRemarks());
                                          writer.append('\n');
                                                    
                                               
                                     
                                
                           }
            //               for(Sysaudit syaudi:sysauditgettablevalues)
            //               {
            //                                                          
            //                         writer.append(resultset.getString(i));
            //                         if(i<numberOfColumns)
            //                         {
            //                              writer.append(',');
            //                         }
            //                         else
            //                         {
            //                              writer.append('\n');
            //                         }
            //                                        
            //               }
                           writer.close();
                           
                           //zipDirectory();
                           
                           
                      }
                      catch(Exception e)
                      {
                           System.out.println("Check Database name"+e);
                      }
                      
                 }
            }

            Entity page

            package com.sipl.hrms.entity.admin;
            // Generated May 21, 2009 5:12:41 PM by Hibernate Tools 3.2.2.GA

            import java.util.Date;
            import java.util.List;

            import javax.persistence.Column;
            import javax.persistence.Entity;
            import javax.persistence.GeneratedValue;
            import javax.persistence.Id;
            import javax.persistence.JoinColumn;
            import javax.persistence.ManyToOne;
            import javax.persistence.Table;
            import javax.persistence.Temporal;
            import javax.persistence.TemporalType;
            import javax.persistence.Transient;

            import org.hibernate.annotations.GenericGenerator;
            import org.hibernate.validator.Length;
            import org.hibernate.validator.NotNull;
            import org.jboss.seam.annotations.Logger;
            import org.jboss.seam.log.Log;

            import com.sipl.hrms.entity.employee.Empmaster;

            /**
            * Sysaudit generated by hbm2java
            */
            @Entity
            @Table(name = "sysaudit")
            public class Sysaudit implements java.io.Serializable {

                 @Logger
                 private Log log;
                 //SysauditHome
                 private String auditId;
                 private String empId;
                 private String clientId;
                 private String actions;
                 private String actionShortDesc;
                 private String programId;
                 private String errorInd;
                 private String auditCategory;
                 private Date dateStamp;
                 private String userId;
                 private String remarks;

                 private boolean empCodeSelect;
                 
                 public Sysaudit() {
                 }

                 public Sysaudit(String auditId, Date dateStamp) {
                      this.auditId = auditId;
                      this.dateStamp = dateStamp;
                 }
                 public Sysaudit(String auditId, String empId, String clientId,
                           String actions, String actionShortDesc, String programId,
                           String errorInd, String auditCategory, Date dateStamp,
                           String userId, String remarks) {
                      this.auditId = auditId;
                      this.empId = empId;
                      this.clientId = clientId;
                      this.actions = actions;
                      this.actionShortDesc = actionShortDesc;
                      this.programId = programId;
                      this.errorInd = errorInd;
                      this.auditCategory = auditCategory;
                      this.dateStamp = dateStamp;
                      this.userId = userId;
                      this.remarks = remarks;
                 }

                 @Id
                 @GeneratedValue(generator="AutoId")
                 @GenericGenerator(name="AutoId",strategy="uuid")
                 @Column(name = "AuditId", unique = true, nullable = false, length = 32)
                 @NotNull
                 @Length(max = 32)
                 public String getAuditId() {
                      return this.auditId;
                 }

                 public void setAuditId(String auditId) {
                      this.auditId = auditId;
                 }

                 @Column(name = "EmpId", length = 32)
                 @Length(max = 32)
                 public String getEmpId() {
                      return this.empId;
                 }

                 public void setEmpId(String empId) {
                      this.empId = empId;
                 }

                 @Column(name = "ClientId", length = 32)
                 @Length(max = 32)
                 public String getClientId() {
                      return this.clientId;
                 }

                 public void setClientId(String clientId) {
                      this.clientId = clientId;
                 }

                 @Column(name = "Actions", length = 20)
                 @Length(max = 20)
                 public String getActions() {
                      return this.actions;
                 }

                 public void setActions(String actions) {
                      this.actions = actions;
                 }

                 @Column(name = "ActionShortDesc", length = 50)
                 @Length(max = 50)
                 public String getActionShortDesc() {
                      return this.actionShortDesc;
                 }

                 public void setActionShortDesc(String actionShortDesc) {
                      this.actionShortDesc = actionShortDesc;
                 }

                 @Column(name = "ProgramId", length = 20)
                 @Length(max = 20)
                 public String getProgramId() {
                      return this.programId;
                 }

                 public void setProgramId(String programId) {
                      this.programId = programId;
                 }

                 @Column(name = "ErrorInd", length = 5)
                 @Length(max = 5)
                 public String getErrorInd() {
                      return this.errorInd;
                 }

                 public void setErrorInd(String errorInd) {
                      this.errorInd = errorInd;
                 }

                 @Column(name = "AuditCategory", length = 20)
                 @Length(max = 20)
                 public String getAuditCategory() {
                      return this.auditCategory;
                 }

                 public void setAuditCategory(String auditCategory) {
                      this.auditCategory = auditCategory;
                 }
                 @Temporal(TemporalType.TIMESTAMP)
                 @Column(name = "DateStamp", length = 19)
                 public Date getDateStamp() {
                      return this.dateStamp;
                 }

                 public void setDateStamp(Date dateStamp) {
                      this.dateStamp = dateStamp;
                 }

                 @Column(name = "UserId", length = 32)
                 @Length(max = 32)
                 public String getUserId() {
                      return this.userId;
                 }

                 public void setUserId(String userId) {
                      this.userId = userId;
                 }

                 @Column(name = "Remarks", length = 250)
                 @Length(max = 250)
                 public String getRemarks() {
                      return this.remarks;
                 }

                 public void setRemarks(String remarks) {
                      this.remarks = remarks;
                 }
                 
                 
                 private Date fromDate;
                 @Transient
                 public Date getFromDate() {
                      //Date D=retDate(this.fromDate);
                      //log.info("dATE={0}",D);
                      System.out.println(" from date : "+fromDate);
                      return this.fromDate;
                      
                 }
                 public void setFromDate(Date fromDate) {
                      this.fromDate = fromDate;
                 }
                      
                 

                 
                 
                 private Date toDate;
                 @Transient
                 public Date getToDate() {
                      System.out.println(" to date : "+toDate);
                      //Date D=retDate(this.fromDate);
                      //log.info("dATE={0}",D);
                      return this.toDate;
                      
                 }
                 
                 public void setToDate(Date toDate) {
                      this.toDate = toDate;
                 }
                 //EmpmasterHome
                 
                 private Empmaster empmasterid;
                 @ManyToOne(targetEntity=Empmaster.class)
                 @JoinColumn(name="RegionId",insertable=false,updatable=false)
                 public Empmaster getSysAuditEmpId(){
                      return this.empmasterid;
                 }
                 public void setSysAuditEmpId(Empmaster empmasterid){
                      this.empmasterid = empmasterid;
                 }
            }


            • 3. Re: how to get values from database from Session file entitynamehome.java
              tmalatinszki

              The main problem is Your hibernate session is null.
              In the try block replace org.hibernate.Session session = null; with org.hibernate.Session session=(org.hibernate.Session)this.getEntityManager().getDelegate();


              Could You test if it's working?


              Regards,
              Tamas

              • 4. Re: how to get values from database from Session file entitynamehome.java
                tmalatinszki

                If You want to put column names into csv file add this code after FileWriter writer = new FileWriter(path); row:


                AbstractEntityPersister aep=((AbstractEntityPersister)session.getSessionFactory().getClassMetadata(Sysaudit.class));
                writer.append(aep.getPropertyColumnNames("empId")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("clientId")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("actions")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("actionShortDesc")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("programId")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("errorInd")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("auditCategory")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("remarks")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("userId")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("remarks")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("auditId")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("remarks")[0]);
                writer.append(',');
                writer.append(aep.getPropertyColumnNames("remarks")[0]);
                writer.append('\n');



                Regards,
                Tamas

                • 5. Re: how to get values from database from Session file entitynamehome.java
                  sathish0707

                  Thanks Tamas Its Working Fine

                  • 6. Re: how to get values from database from Session file entitynamehome.java
                    tmalatinszki

                    Not at all!
                    Just don't forget to rate my answer if it was helpful... :)

                    • 7. Re: how to get values from database from Session file entitynamehome.java
                      sathish0707

                      i can't able to get my primary key.