Reputation: 137
I am getting an exception while saving persisting the object into database bellow is code base model:
@Entity
@Table(name = "customer_data")
public class BsslpCustomerInfo {
@Id
@Column(name = "id", length = 10)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "service_Id", length = 50, unique = true, nullable = false)
private String serviceId;
@Column(name = "quarter", length = 255, nullable = false)
private String quarter;
@Column(name = "year")
private int year;
@Column(name = "activated_Customer_Name")
private String activatedCustomerName;
@Column(name = "deactivated_Customer_Name")
private String deactivatedCustomerName;
@Column(name = "quarter_Start_Date")
private String quarterStartDate;
@Column(name = "quarter_End_Date")
private String quarterEndDate;
public String getServiceId() {
return serviceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
public String getQuarter() {
return quarter;
}
public void setQuarter(String quarter) {
this.quarter = quarter;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getActivatedCustomerName() {
return activatedCustomerName;
}
public void setActivatedCustomerName(String activatedCustomerName) {
this.activatedCustomerName = activatedCustomerName;
}
public String getDeactivatedCustomerName() {
return deactivatedCustomerName;
}
public void setDeactivatedCustomerName(String deactivatedCustomerName) {
this.deactivatedCustomerName = deactivatedCustomerName;
}
public String getQuarterStartDate() {
return quarterStartDate;
}
public void setQuarterStartDate(String quarterStartDate) {
this.quarterStartDate = quarterStartDate;
}
public String getQuarterEndDate() {
return quarterEndDate;
}
public void setQuarterEndDate(String quarterEndDate) {
this.quarterEndDate = quarterEndDate;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
...and this is the "Controller" code :
for (BsslpParty bsslp : data) {
Date activationDate = sdf.parse(bsslp.getAssocStartDate());
Date deactivationDate = sdf.parse(bsslp.getAssocEndDate());
if(activationDate.compareTo(startDate)>=0 && activationDate.compareTo(endDate)<=0){
activeCount++;
BsslpCustomerInfo bsslpCustomerInfo = new BsslpCustomerInfo();
bsslpCustomerInfo.setServiceId(bsslp.getBsslpName());
bsslpCustomerInfo.setActivatedCustomerName(bsslp.getEntitledPartyName());
bsslpCustomerInfo.setQuarter(quarterDateRange.getStartdate());
bsslpCustomerInfo.setYear(quarterDateRange.getFinYear());
bsslpCustomerInfo.setQuarterStartDate(quarterDateRange.getStartdate());
bsslpCustomerInfo.setQuarterEndDate(quarterDateRange.getEndDate());
listOfActDeActCust.add(bsslpCustomerInfo);
}
usageMetricschartService.saveAllCustomerData(listOfActDeActCust);
}
"Service" implementation:
@Override
@Transactional
public void saveAllCustomerData(List<BsslpCustomerInfo> customersData) {
// TODO Auto-generated method stub
for (BsslpCustomerInfo bsslpCustomerInfo : customersData) {
usageMetricsCustomersRepository.save(bsslpCustomerInfo);
}
}
It is throwing an error like:
java.sql.BatchUpdateException: ORA-01722: invalid number
oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343) org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute batch; SQL [insert into customer_data (activated_Customer_Name, deactivated_Customer_Name, quarter, quarter_End_Date, quarter_Start_Date, service_Id, year, id) values (?, ?, ?, ?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute batch
I've tried in all the possible ways couldn't figure this out; can anyone please help me on this?
Upvotes: 2
Views: 2048
Reputation: 19421
You have defined your id to be auto-generated, but for every entity you create, you pass a 0 value to the database. This is because your are using the primitive int
type which is per default initialized to 0. If you set a value to the id field, there is no autogeneration.
Change you id property to type Integer
so that on persisting a null value is sent to JPA which will cause the id to be generated.
Upvotes: 1