无法让我的setTestValue方法在db中保存我的数据我从readcsvfile方法中调用此方法..我没有看到任何异常,但数据没有访问数据库..我正在使用Hibernate和postgres db < / p>
@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();
}
}
}
}
}
这是表对象:
@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;
}
}
答案 0 :(得分:1)
因为您没有任何异常,所以很难知道您可以将数据保存到数据库的原因是什么。我想建议您检查代码的一些步骤:
Step1: Create an instance TestValue (don't read from your csv)
Step2: Test the save method in your GenericDAO <TestValue>
如果step2成功,我认为您可以检查从csv数据加载的方法。如果Step2不成功,请在GenericDao中仔细检查您的保存方法
如果您有例外情况,那么这将是我们跟踪问题并解决问题的信息。
希望得到这个帮助。
答案 1 :(得分:0)
想出它最终必须将我的readCSV方法移动到我的UI层并让它调用下面定义的setTestValue方法
@Override
@Transactional(readOnly = false)
public void setTestValue(TestValue testValue){
genericDao.makePersistent(testValue);
}