我有以下2週休眠的entites:Hibernate的集合緩存問題
@Entity
@Table(name = "VEHICLE_BRAND")
public class VehicleBrand implements java.io.Serializable {
...
@Column(name = "NAME", nullable = false, length = 1000)
public String getName() {
return name;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "vehiclebrand")
public Set<VehicleModel> getVehicleModels() {
return vehicleModels;
}
...
}
和
@Entity
@Table(name = "VEHICLE_MODEL")
public class VehicleModel implements java.io.Serializable {
...
@Column(name = "NAME", nullable = false, length = 1000)
public String getName() {
return name;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="VECHILE_BRAND_ID")
public VehicleBrand getVehicleBrand() {
return this.vehicleBrand;
}
...
}
而且我有哪些測試VehicleBrand和VehicleModel以下的單元測試:
DefaultTransactionDefinition txDef = new DefaultTransactionDefinition();
txDef.setName("test1");
txDef.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus txStatus = txManager.getTransaction(txDef);
// Load brand "1" object.
VehicleBrand brand = (VehicleBrand) factory.getVehicleBrandDAO().findFirstByName("1");
assertNotNull(brand);
// Check if model "X" exists for brand "1" through collection.
Set<VehicleModel> models = brand.getVehicleModels();
for (final VehicleModel model : models) {
assertFalse(model.getName().equals("X"));
}
// Add model "X" to brand "1".
VehicleModel model = new VehicleModel();
model.setName("X");
model.setValidFrom(new Date());
model.setVehicleBrand(brand);
factory.getVehicleModelDAO().create(model);
txManager.commit(txStatus);
txDef = new DefaultTransactionDefinition();
txDef.setName("test2");
txDef.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
txStatus = txManager.getTransaction(txDef);
// Check that model is added to database.
model = (VehicleModel) factory.getVehicleModelDAO().findFirstByName("X");
assertNotNull(model);
assertEquals(model.getVehicleBrandId().longValue(), 1L);
// Check if model X exists for brand "1" through collection.
brand = (VehicleBrand) factory.getVehicleBrandDAO().findFirstByName("1");
models = brand.getVehicleModels();
boolean found = false;
for (final VehicleModel model2 : models) {
if (model2.getName().equals("X")) {
found = true;
}
}
assertTrue(found);
txManager.commit(txStatus);
有人能解釋我爲什麼最後一行失敗嗎?
你可以使用'getNavn'的片段或將存儲在數據庫中的關聯成員更新代碼嗎? –
對不起,這是一個錯字,它被稱爲getName,並且我已經在問題中爲實體添加了getter/annotation定義 –
我已更新測試用例以確保在請求之間啓動新的事務 –