Java Hibernate:对象引用未保存的临时实例
我收到以下错误:Java Hibernate:对象引用未保存的临时实例,java,spring,hibernate,Java,Spring,Hibernate,我收到以下错误: object references an unsaved transient instance - save the transient instance before flushing: com.project.outer.dataobject.AddressIdentificationType; nested exception is org.hibernate.TransientObjectException: object references an unsaved t
object references an unsaved transient instance - save the transient instance before flushing: com.project.outer.dataobject.AddressIdentificationType; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.project.outer.dataobject.AddressIdentificationType
在网上查找一段时间后,我现在有点了解错误,但所有在线错误通常发生在两个表之间的关系上,例如OneToMany,需要添加@OneToManycascade=Cascade。但我没有这个,所以我不知道出了什么问题,有人知道吗
AddressIdentificationType.js
Ext.apply(AddressIdentificationType, {
valueProvider: {
getValues: AddressIdentificationTypeService.getAddressIdentificationTypes
},
AddressIdentificationType.java
@Entity
@DataTransferObject(params={
@Param(name = "match", value = "com.project.outer.dataobject.AddressIdentificationType"),
@Param(name = "javascript", value = "AddressIdentificationType")})
public class AddressIdentificationType extends BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long addressIdentificationTypeId;
@Column(length = 250, nullable=false)
private String description;
@Enumerated(EnumType.STRING)
@Column(length = 20)
private Region region;
public AddressIdentificationType() {
}
@RemoteProperty
public Long getAddressIdentificationTypeId() {
return addressIdentificationTypeId;
}
public void setAddressIdentificationTypeId(Long addressIdentificationTypeId) {
this.addressIdentificationTypeId = addressIdentificationTypeId;
}
@RemoteProperty
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
protected Long getId() {
return addressIdentificationTypeId;
}
AddressIdentificationTypeService.java
@Secured(value={"ROLE_GENERAL"})
@RemoteMethod
public List<IdentificationType> getAddressIdentificationTypes(Region region) {
if (region == null) {
region = Region.France;
}
return repository.getData(NamedQuery.AddressIdentificationTypePropertySelection, region);
}
如果需要查看更多的代码,只需要求添加它,尽量使问题简短即可
编辑基实体
BaseEntity.java
@MappedSuperclass
public abstract class BaseEntity implements Cloneable {
@Transient
private String internalUUID;
@Version
@Column(columnDefinition = "int default 0")
protected Integer version;
@Column(length = 50)
protected String modifiedBy;
@Override
public int hashCode() {
return getUUID().hashCode();
}
@Override
public boolean equals(Object o) {
return (o == this || (o instanceof BaseEntity && getUUID().equals(((BaseEntity) o).getUUID())));
}
private String getUUID() {
// use the internalUUID if it has been set, otherwise create-->persist-->compare sequence will not work.
//
if (internalUUID != null) {
return internalUUID;
}
if (getId() != null) {
return getId().toString();
}
if (internalUUID == null) {
internalUUID = randomUUID().toString();
}
return internalUUID;
}
protected abstract Object getId();
//protected abstract void setId(Object obj);
protected BaseEntity clone() throws CloneNotSupportedException {
BaseEntity object = CloneMap.get(this.getClass().getName() + ":" + this.getId());
if (object != null) {
return object;
}
BaseEntity obj = (BaseEntity)super.clone();
obj.version = 0;
try {
Field field = getIdField(obj.getClass());
if (field != null) {
field.setAccessible(true);
field.set(obj, null);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
CloneMap.put(this.getClass().getName() + ":" + this.getId(), obj);
return obj;
}
private static Field getIdField(Class clazz) {
try {
String name = clazz.getName();
name = name.substring(name.lastIndexOf(".") + 1);
return clazz.getDeclaredField(name.substring(0, 1).toLowerCase() + name.substring(1) + "Id");
} catch (NoSuchFieldException e) {
if (clazz.getSuperclass() == null) {
return null;
}
return getIdField(clazz.getSuperclass());
}
}
private static Field getField(Class clazz, String propertyName) {
try {
return clazz.getDeclaredField(propertyName);
} catch (NoSuchFieldException e) {
if (clazz.getSuperclass() == null) {
return null;
}
return getField(clazz.getSuperclass(), propertyName);
}
}
protected BaseEntity clone(Agent agent) throws CloneNotSupportedException {
BaseEntity obj = clone();
try {
Field field = getField(obj.getClass(), "agent");
if (field != null) {
field.setAccessible(true);
field.set(obj, agent);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
protected <T extends BaseEntity> List<T> clone(List<T> list, Agent agent) throws CloneNotSupportedException {
List<T> newList = new ArrayList<T>();
for (T e: list) {
newList.add((T)e.clone(agent));
}
return newList;
}
protected <T extends BaseEntity> Set<T> clone(Set<T> list, Agent agent) throws CloneNotSupportedException {
Set<T> newList = new HashSet<T>();
for (T e: list) {
newList.add((T)e.clone(agent));
}
return newList;
}
}
你能和我们分享你的基本实体吗?你还需要什么吗?这很有趣!好的,根据直觉,你能注释掉区域字段和它的访问器吗,尝试一下,看看它是否仍然给出相同的异常?当我这样做时,它会给出错误:命名查询中的错误:AddressIdentificationTypePropertySectionorg.hibernate.QueryException:无法解析属性:区域:com.project.outer.dataobject.AddressIdentificationType[from AddressIdentificationType identificationType where identificationType.region=?]……它无法执行查询,因为在我取出访问器时找不到区域