Hibernate:对象引用未保存的瞬态实例

时间:2012-06-28 13:33:51

标签: 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 transient instance - save the transient instance before flushing: com.project.outer.dataobject.AddressIdentificationType

现在在网上看了一段时间后,我有点理解错误,但是在线的所有错误通常都发生在两个表之间的关系(例如OneToMany),需要添加@OneToMany(cascade = Cascade.ALL)但是我没有这个,所以我不这样做知道出了什么问题,有人知道吗?

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

}

0 个答案:

没有答案