报错如下:
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: xxxxxxxxxxxxxxxxx; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: xxxxxxxxxxxxxxxx
经过排查实体类中主键id为自动生成字段,而在代码中手动给id赋值了因此导致了hibernate报错。
具体实现类如下所示:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@JsonInclude(Include.NON_NULL)
@Entity
@Table(
name = "lsvofplncmdccfitembusitype"
)
public class Lsvofplncmdccfitembusitype implements Serializable {
@Id
@GeneratedValue(
generator = "uuid"
)
@GenericGenerator(
name = "uuid",
strategy = "uuid2"
)
@Column(
name = "id",
nullable = false,
length = 36
)
private String id;
@Column(
name = "busiscopecode",
nullable = true,
length = 20
)
private String busiscopecode;
@Column(
name = "busitypecode",
nullable = true,
length = 100
)
private String busitypecode;
@Column(
name = "busitypename",
nullable = true,
length = 240
)
private String busitypename;
@Column(
name = "creatby",
nullable = true,
length = 240
)
private String creatby;
@JsonFormat(
pattern = "yyyy-MM-dd HH:mm:ss.SSS"
)
@Column(
name = "createdon",
nullable = true
)
private Date createdon;
@Column(
name = "creator",
nullable = true,
length = 36
)
private String creator;
@Column(
name = "lastchangedby",
nullable = true,
length = 240
)
private String lastchangedby;
@JsonFormat(
pattern = "yyyy-MM-dd HH:mm:ss.SSS"
)
@Column(
name = "lastchangedon",
nullable = true
)
private Date lastchangedon;
@Column(
name = "lastchanger",
nullable = true,
length = 36
)
private String lastchanger;
@Column(
name = "parentid",
nullable = true,
length = 36
)
private String parentid;
@Column(
name = "remark",
nullable = true,
length = 512
)
private String remark;
@Column(
name = "tenantid",
nullable = true
)
private Integer tenantid;
public Lsvofplncmdccfitembusitype() {
}
public String getId() {
return this.id;
}
public String getBusiscopecode() {
return this.busiscopecode;
}
public String getBusitypecode() {
return this.busitypecode;
}
public String getBusitypename() {
return this.busitypename;
}
public String getCreatby() {
return this.creatby;
}
public Date getCreatedon() {
return this.createdon;
}
public String getCreator() {
return this.creator;
}
public String getLastchangedby() {
return this.lastchangedby;
}
public Date getLastchangedon() {
return this.lastchangedon;
}
public String getLastchanger() {
return this.lastchanger;
}
public String getParentid() {
return this.parentid;
}
public String getRemark() {
return this.remark;
}
public Integer getTenantid() {
return this.tenantid;
}
public void setId(final String id) {
this.id = id;
}
public void setBusiscopecode(final String busiscopecode) {
this.busiscopecode = busiscopecode;
}
public void setBusitypecode(final String busitypecode) {
this.busitypecode = busitypecode;
}
public void setBusitypename(final String busitypename) {
this.busitypename = busitypename;
}
public void setCreatby(final String creatby) {
this.creatby = creatby;
}
@JsonFormat(
pattern = "yyyy-MM-dd HH:mm:ss.SSS"
)
public void setCreatedon(final Date createdon) {
this.createdon = createdon;
}
public void setCreator(final String creator) {
this.creator = creator;
}
public void setLastchangedby(final String lastchangedby) {
this.lastchangedby = lastchangedby;
}
@JsonFormat(
pattern = "yyyy-MM-dd HH:mm:ss.SSS"
)
public void setLastchangedon(final Date lastchangedon) {
this.lastchangedon = lastchangedon;
}
public void setLastchanger(final String lastchanger) {
this.lastchanger = lastchanger;
}
public void setParentid(final String parentid) {
this.parentid = parentid;
}
public void setRemark(final String remark) {
this.remark = remark;
}
public void setTenantid(final Integer tenantid) {
this.tenantid = tenantid;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Lsvofplncmdccfitembusitype)) {
return false;
} else {
Lsvofplncmdccfitembusitype other = (Lsvofplncmdccfitembusitype)o;
if (!other.canEqual(this)) {
return false;
} else {
label167: {
Object this$tenantid = this.getTenantid();
Object other$tenantid = other.getTenantid();
if (this$tenantid == null) {
if (other$tenantid == null) {
break label167;
}
} else if (this$tenantid.equals(other$tenantid)) {
break label167;
}
return false;
}
Object this$id = this.getId();
Object other$id = other.getId();
if (this$id == null) {
if (other$id != null) {
return false;
}
} else if (!this$id.equals(other$id)) {
return false;
}
label153: {
Object this$busiscopecode = this.getBusiscopecode();
Object other$busiscopecode = other.getBusiscopecode();
if (this$busiscopecode == null) {
if (other$busiscopecode == null) {
break label153;
}
} else if (this$busiscopecode.equals(other$busiscopecode)) {
break label153;
}
return false;
}
Object this$busitypecode = this.getBusitypecode();
Object other$busitypecode = other.getBusitypecode();
if (this$busitypecode == null) {
if (other$busitypecode != null) {
return false;
}
} else if (!this$busitypecode.equals(other$busitypecode)) {
return false;
}
label139: {
Object this$busitypename = this.getBusitypename();
Object other$busitypename = other.getBusitypename();
if (this$busitypename == null) {
if (other$busitypename == null) {
break label139;
}
} else if (this$busitypename.equals(other$busitypename)) {
break label139;
}
return false;
}
Object this$creatby = this.getCreatby();
Object other$creatby = other.getCreatby();
if (this$creatby == null) {
if (other$creatby != null) {
return false;
}
} else if (!this$creatby.equals(other$creatby)) {
return false;
}
label125: {
Object this$createdon = this.getCreatedon();
Object other$createdon = other.getCreatedon();
if (this$createdon == null) {
if (other$createdon == null) {
break label125;
}
} else if (this$createdon.equals(other$createdon)) {
break label125;
}
return false;
}
label118: {
Object this$creator = this.getCreator();
Object other$creator = other.getCreator();
if (this$creator == null) {
if (other$creator == null) {
break label118;
}
} else if (this$creator.equals(other$creator)) {
break label118;
}
return false;
}
Object this$lastchangedby = this.getLastchangedby();
Object other$lastchangedby = other.getLastchangedby();
if (this$lastchangedby == null) {
if (other$lastchangedby != null) {
return false;
}
} else if (!this$lastchangedby.equals(other$lastchangedby)) {
return false;
}
label104: {
Object this$lastchangedon = this.getLastchangedon();
Object other$lastchangedon = other.getLastchangedon();
if (this$lastchangedon == null) {
if (other$lastchangedon == null) {
break label104;
}
} else if (this$lastchangedon.equals(other$lastchangedon)) {
break label104;
}
return false;
}
label97: {
Object this$lastchanger = this.getLastchanger();
Object other$lastchanger = other.getLastchanger();
if (this$lastchanger == null) {
if (other$lastchanger == null) {
break label97;
}
} else if (this$lastchanger.equals(other$lastchanger)) {
break label97;
}
return false;
}
Object this$parentid = this.getParentid();
Object other$parentid = other.getParentid();
if (this$parentid == null) {
if (other$parentid != null) {
return false;
}
} else if (!this$parentid.equals(other$parentid)) {
return false;
}
Object this$remark = this.getRemark();
Object other$remark = other.getRemark();
if (this$remark == null) {
if (other$remark != null) {
return false;
}
} else if (!this$remark.equals(other$remark)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof Lsvofplncmdccfitembusitype;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $tenantid = this.getTenantid();
result = result * 59 + ($tenantid == null ? 43 : $tenantid.hashCode());
Object $id = this.getId();
result = result * 59 + ($id == null ? 43 : $id.hashCode());
Object $busiscopecode = this.getBusiscopecode();
result = result * 59 + ($busiscopecode == null ? 43 : $busiscopecode.hashCode());
Object $busitypecode = this.getBusitypecode();
result = result * 59 + ($busitypecode == null ? 43 : $busitypecode.hashCode());
Object $busitypename = this.getBusitypename();
result = result * 59 + ($busitypename == null ? 43 : $busitypename.hashCode());
Object $creatby = this.getCreatby();
result = result * 59 + ($creatby == null ? 43 : $creatby.hashCode());
Object $createdon = this.getCreatedon();
result = result * 59 + ($createdon == null ? 43 : $createdon.hashCode());
Object $creator = this.getCreator();
result = result * 59 + ($creator == null ? 43 : $creator.hashCode());
Object $lastchangedby = this.getLastchangedby();
result = result * 59 + ($lastchangedby == null ? 43 : $lastchangedby.hashCode());
Object $lastchangedon = this.getLastchangedon();
result = result * 59 + ($lastchangedon == null ? 43 : $lastchangedon.hashCode());
Object $lastchanger = this.getLastchanger();
result = result * 59 + ($lastchanger == null ? 43 : $lastchanger.hashCode());
Object $parentid = this.getParentid();
result = result * 59 + ($parentid == null ? 43 : $parentid.hashCode());
Object $remark = this.getRemark();
result = result * 59 + ($remark == null ? 43 : $remark.hashCode());
return result;
}
public String toString() {
return "Lsvofplncmdccfitembusitype(id=" + this.getId() + ", busiscopecode=" + this.getBusiscopecode() + ", busitypecode=" + this.getBusitypecode() + ", busitypename=" + this.getBusitypename() + ", creatby=" + this.getCreatby() + ", createdon=" + this.getCreatedon() + ", creator=" + this.getCreator() + ", lastchangedby=" + this.getLastchangedby() + ", lastchangedon=" + this.getLastchangedon() + ", lastchanger=" + this.getLastchanger() + ", parentid=" + this.getParentid() + ", remark=" + this.getRemark() + ", tenantid=" + this.getTenantid() + ")";
}
}
在代码中,id字段是该实体类主键,其值为自动生成的randomUUID。
因此在给实体对象赋值时,应注意避免给自动生成字段赋值。