我有一个错误,让我疯了。当我尝试创建一个新对象时,Hibernate会返回一个错误:
对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:EquipmentType;嵌套异常是org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 保存瞬态实例
我有以下Spring服务:
CommandService
public class CommandService implements ICommandService {
@Override
public Command createCommand(String name, String eqpName) {
Command cmd = new Command();
Equipment eqp = getEquipment(eqpName);
cmd.setEquipment(eqp);
cmd.setName(name);
return cmd;
}
@Override
public Equipment getEquipment(String eqpName) {
IEquipmentService eqpService = (IEquipmentService) SPRING_CONTEXT.getBean("EquipmentService");
Equipment eqp = eqpService.findEquipmentByName(eqpName);
EquipmentType eqpType = new EquipmentType();
eqpType.setName("MyType");
eqp.setType(eqpType);
Model model = new Model();
model.setName("MyModel");
eqp.setModel(model);
eqpService.saveEquipment(eqp);
return eqp;
}
}
EquipmentService
public class EquipmentService implements IEquipmentService {
private DAO dao; // This DAO is autowired
@Override
public void saveEquipment(Equipment eqp) {
completeModel(eqp);
completeEqpType(eqp);
((EquipmentDAO) dao).merge(eqp);
}
private void completeModel(Equipment eqp) {
IModelService modelService = (IModelService) SPRING_CONTEXT.getBean("ModelService");
Model result = modelService.findModelByName(eqp.getModel().getName()); // Fails here
eqp.setModel(result);
}
private void completeEqpType(Equipment eqp) {
IEquipmentTypeService eqpTypeService = (IEquipmentTypeService) SPRING_CONTEXT.getBean("EquipmentTypeService");
EquipmentType result = eqpTypeService.findEquipmentTypeByName(eqp.getType().getName());
eqp.setType(result);
}
}
请注意,IEquipmentService
和ICommandService
的所有方法都配置为事务性的。
我的对象Equipment
由Model
和EquipmentType
组成。
这些对象引用数据库中已经创建的对象,这就是为什么我不想再次保存它们。
这是Hibernate配置:
<class name="Equipment" table="equipment" lazy="false">
<id name="equipmentId" type="java.lang.Integer">
<column name="EQUIPMENT_ID" />
<generator class="native" />
</id>
<many-to-one name="type" class="EquipmentType" fetch="join" not-null="true">
<column name="TYPE_ID" not-null="true" />
</many-to-one>
<many-to-one name="model" class="Model" fetch="join" not-null="true">
<column name="MODEL_ID" not-null="true" />
</many-to-one>
</class>
编辑:根据要求,以下是方法equals(..)
和hashcode(..)
装备
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((equipmentId == null) ? 0 : equipmentId.hashCode());
result = prime * result + ((type == null) ? 0 : type.hashCode());
result = prime * result + ((model == null) ? 0 : model.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Equipment) {
Equipment eqp = (Equipment) obj;
return new EqualsBuilder().append(this.equipmentId, eqp.getEquipmentId()).isEquals();
}
return false;
}
对于Model和EquipmentType,未明确定义这些方法。
我可以帮助我弄清楚为什么在拨打createCommand(..)
时会返回此错误?
答案 0 :(得分:1)
我认为问题是您在EquipmentType
方法中设置了新的Model
和getEquipment
。由于您还没有在这两个类中定义equals()和hashcode方法,因此hibernate无法将这些与已持久化的对象进行比较。它将它们视为分离(因为equals()和hashcode()方法指示新对象而不是现有对象)。这就是为什么它要求您首先保存设备类型和模型对象。
我认为你应该重写equals()&amp; hashcode()正确地在EquipmentType&amp;模型。
修改
我认为你不应该在completeType()方法中再次覆盖它时添加新的EquipmentType。为什么不将您的API更改为
EquipmentService#saveEquipment(Equipment e, Sting type, String modelName){
EquipmentType t = findType();
Model m = findModel();
e.setType(t);
e.setModel(m);
}
如果您无法更改API,则必须先触发新类型和模型,然后再触发hibernate flush。