У меня есть следующие 2 Hibernate Entites:
@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);
Может кто-нибудь объяснить мне, почему последняя строка терпит неудачу?