简体   繁体   中英

Hibernate: Persisting data issue

Having trouble getting my setTestValue method to persist my data in the db i am calling this method from within readcsvfile method .. i do not see any exceptions but data is not hitting the database.. I am using Hibernate and postgres db

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
@Service
public class TestValueService implements ITestValueService
{
  /**
 *
*/
    private static final long serialVersionUID = -1027211428586214287L;

  @Autowired
 GenericDao genericDao;

 private static final Logger LOG = Logger.getLogger(TestValueService.class);
 private TestValue defaultValue;

 public TestValueService()
{

}

@Transactional(readOnly = false)    
**private void setTestValue**(TestValue defaultValue){
    genericDao.makePersistent(defaultValue);
   LOG.info("ballingss");
    }
      /*
     * Method to read the defaults csv file and store into the common table
    */
      @Override
     //@Transactional(readOnly = false)
    public void readCSVFile(String fileLocation, Long clientJobId){
      String csvFile = fileLocation;
      BufferedReader br = null;
       String line = "";
       String cvsSplitBy = ",";

      try {
       br = new BufferedReader(new FileReader(csvFile));
       while ((line = br.readLine()) != null) {

        // use comma as separator
        String[] currentLine = line.split(cvsSplitBy);


        TestValue defaultValue = new TestValue();
        Date date = new Date();              
        defaultValue.setClient_job_id(clientJobId);
        defaultValue.setCreate_dt(date);
        defaultValue.setActive(true);
        defaultValue.setDef_keyfield(currentLine[0].toUpperCase());
        defaultValue.setDef_value(currentLine[1].toUpperCase());
        **setTestValue(defaultValue);**

     }

  } catch (FileNotFoundException e) {
     LOG.error("File Not found ");
  } catch (IOException e) {
     e.printStackTrace();
  } finally {
     if (br != null) {
        try {
           br.close();
        } catch (IOException e) {
           e.printStackTrace();
        }
     }
     }

  }




}

here is the table object:

@Entity
  @Table(name = "TEST_VALUE")
public class TestValue implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "default_values_def_id_seq")
@SequenceGenerator(name = "default_values_def_id_seq", sequenceName = "default_values_def_id_seq")    
   private Long def_id;

   private Long client_job_id;

   @Temporal(TemporalType.TIMESTAMP)
   private Date create_dt;

   @Basic
    private String def_keyfield;

   @Basic
   private String def_value;

    @Basic
   boolean active  = false;

  public Long getDef_id()
  {
     return def_id;
  }

  public void setDef_id(Long def_id)
  {
     this.def_id = def_id;
  }

  public Long getClient_job_id()
 {
     return client_job_id;
  }

   public void setClient_job_id(Long client_job_id)
  {
     this.client_job_id = client_job_id;
  }

  public Date getCreate_dt()
  {
     return create_dt;
   }

   public void setCreate_dt(Date create_dt)
   {
     this.create_dt = create_dt;
  }

  public String getDef_keyfield()
  {
     return def_keyfield;
  }

     public void setDef_keyfield(String def_keyfield)
  {
     this.def_keyfield = def_keyfield;
  }

  public String getDef_value()
  {
     return def_value;
  }

  public void setDef_value(String def_value)
 {
    this.def_value = def_value;
  }

  public boolean isActive()
  {
     return active;
  }

   public void setActive(boolean active)
  {
     this.active = active;
  }

  public static long getSerialversionuid()
  {
     return serialVersionUID;
  }



} 

Because you don't have any Exception, so it's quite hard to know what is the reason that you can't save data to your database. I would like to suggest some steps you can do to check your code:

Step1: Create an instance TestValue (don't read from your csv)
Step2: Test the save method in your GenericDAO <TestValue>

If step2 is successful, I think you can check your method to load from your csv data. If Step2 is not successful, pls double check your save method in GenericDao

If you have Exceptions, that will be the info for us to track the issue and resolve it.

Hope this help.

Figured it out ended up having to move my readCSV method to my UI layer and have it call the setTestValue method defined as below

@Override
   @Transactional(readOnly = false)
   public void setTestValue(TestValue testValue){
      genericDao.makePersistent(testValue);        

   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM