gpt4 book ai didi

java.lang.IllegalStateException : org. hibernate.TransientObjectException(更新记录时)

转载 作者:行者123 更新时间:2023-12-02 11:15:12 25 4
gpt4 key购买 nike

在使用 hibernate 更新记录时,我不断收到以下异常,我对 hibernate 非常陌生,请查看该异常跟踪

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;
}
}

Controller 及其方法

@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;
}

最佳答案

您可以使用级联。

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)

CascadeType.ALL 是持久性会将所有 EntityManager 操作(PERSIST、REMOVE、REFRESH、MERGE、DETACH)传播到相关实体。

另一种方式

public boolean updateState(State theState) {
boolean success = false;
try {
currentSession=sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(theState.getCountry());
currentSession.saveOrUpdate(theState);
success=true;
} catch (Exception e) { }
return success;
}

关于java.lang.IllegalStateException : org. hibernate.TransientObjectException(更新记录时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50325173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com