Я получаю следующее исключение при обновлении записи с использованием hibernate
, я очень плохо знаком с hibernate, пожалуйста, посмотрите на это exception trace
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.rasvek.cms.entity.Country
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:146)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
...
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.rasvek.cms.entity.Country
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:279)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:493)
at org.hibernate.type.EntityType.nullSafeSet(EntityType.java:280)
...
Пожалуйста, посмотрите на мой класс сущности:
@Entity
@Table(name = "state", catalog = "cms_db")
public class State implements java.io.Serializable {
private Integer stateId;
private Country country;
private String stateName;
private String status;
private Set<District> districts = new HashSet<District>(0);
public State() {
public State(Country country) {
this.country = country;
}
public State(Country country, String stateName, String status, Set<District> districts) {
this.country = country;
this.stateName = stateName;
this.status = status;
this.districts = districts;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "state_id", unique = true, nullable = false)
public Integer getStateId() {
return this.stateId;
}
public void setStateId(Integer stateId) {
this.stateId = stateId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "country_id", nullable = false)
public Country getCountry() {
return this.country;
}
public void setCountry(Country country) {
this.country = country;
}
@Column(name = "state_name")
public String getStateName() {
return this.stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
@Column(name = "status")
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "state")
public Set<District> getDistricts() {
return this.districts;
}
public void setDistricts(Set<District> districts) {
this.districts = districts;
}
}
Контроллер и его метод
@RestController
@RequestMapping(value="/master")
public class MasterController {
@PutMapping("/updateState")
public ResponseEntity<Map<String, Object>> updateState(@RequestBody State theState) {
ResponseEntity<Map<String, Object>> responseEntity = null;
try {
boolean res = service.updateState(theState) ;
if (res) {
headers.add("head", "State");
message.put("status", true);
message.put("data", "State has been updated successfully!");
responseEntity = new ResponseEntity<Map<String, Object>>
(message, headers, HttpStatus.CREATED);
} else {
headers.add("head", "null");
message.put("status", false);
message.put("errorMessage", "State has not been updated successfully!");
responseEntity= new ResponseEntity<Map<String, Object>>
(message, headers, HttpStatus.NOT_FOUND);
}
} catch (Exception e) { }
return responseEntity;
}
}
DAO:
public boolean updateState(State theState) {
boolean success = false;
try {
currentSession=sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(theState);
success=true;
} catch (Exception e) { }
return success;
}