有人可以解释以下内容。我有代码:
@Entity
public class Model {
@Id
@GeneratedValue(strategy = AUTO)
@Column
private long id;
@Column(length = 200, nullable = false)
private String field0;
@Column(length = 200, nullable = false)
private String field1;
@Column(length = 200, nullable = false)
private String field2;
@Column(length = 200, nullable = false)
private String field3;
@Column(length = 200, nullable = false)
private String field4;
@Column(length = 200, nullable = false)
private String field5;
@Column(length = 200, nullable = false)
private String field6;
@Column(length = 200, nullable = false)
private String field7;
@Column(length = 200, nullable = false)
private String field8;
@Column(length = 200, nullable = false)
private String field9;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getField0() {
return field0;
}
public void setField0(String field0) {
this.field0 = field0;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public String getField3() {
return field3;
}
public void setField3(String field3) {
this.field3 = field3;
}
public String getField4() {
return field4;
}
public void setField4(String field4) {
this.field4 = field4;
}
public String getField5() {
return field5;
}
public void setField5(String field5) {
this.field5 = field5;
}
public String getField6() {
return field6;
}
public void setField6(String field6) {
this.field6 = field6;
}
public String getField7() {
return field7;
}
public void setField7(String field7) {
this.field7 = field7;
}
public String getField8() {
return field8;
}
public void setField8(String field8) {
this.field8 = field8;
}
public String getField9() {
return field9;
}
public void setField9(String field9) {
this.field9 = field9;
}
@Override
public String toString() {
return "Model{" + "id=" + id + ", field0='" + field0 + '\''
+ ", field1='" + field1 + '\'' + ", field2='" + field2 + '\''
+ ", field3='" + field3 + '\'' + ", field4='" + field4 + '\''
+ ", field5='" + field5 + '\'' + ", field6='" + field6 + '\''
+ ", field7='" + field7 + '\'' + ", field8='" + field8 + '\''
+ ", field9='" + field9 + '\'' + '}';
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/testContext.xml")
public class MainTest {
@Autowired
private SessionFactory sessionFactory;
private Session session;
private Transaction tx;
@Before
public void before() {
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.setFlushMode(FlushMode.COMMIT);
}
@After
public void after() {
tx.commit();
session.close();
}
@Test
public void shouldFindModelByField() {
Model model = createRandomModel();
session.save(model);
model.setField0("TEST1");
session.save(model);
assertTrue(null != session.createSQLQuery(
"select id from model where field0 = '" + model.getField0()
+ "'").uniqueResult());
}
private Model createRandomModel() {
Model ret = new Model();
ret.setField0(RandomStringUtils.randomAlphanumeric(10));
ret.setField1(RandomStringUtils.randomAlphanumeric(10));
ret.setField2(RandomStringUtils.randomAlphanumeric(10));
ret.setField3(RandomStringUtils.randomAlphanumeric(10));
ret.setField4(RandomStringUtils.randomAlphanumeric(10));
ret.setField5(RandomStringUtils.randomAlphanumeric(10));
ret.setField6(RandomStringUtils.randomAlphanumeric(10));
ret.setField7(RandomStringUtils.randomAlphanumeric(10));
ret.setField8(RandomStringUtils.randomAlphanumeric(10));
ret.setField9(RandomStringUtils.randomAlphanumeric(10));
return ret;
}
}
如果我按原样运行测试,测试失败,我收到错误java.lang.AssertionError。
我有三个varint来改变@Test方法以成功运行测试:
1)
@Test
public void shouldFindModelByField() {
Model model = createRandomModel();
session.save(model);
session.evict(model);
model.setField0("TEST1");
session.save(model);
assertTrue(null != session.createSQLQuery(
"select id from model where field0 = '" + model.getField0()
+ "'").uniqueResult());
}
2)
@Test
public void shouldFindModelByField() {
Model model = createRandomModel();
session.save(model);
model.setField0("TEST1");
session.save(model);
tx.commit();
tx = session.beginTransaction();
assertTrue(null != session.createSQLQuery(
"select id from model where field0 = '" + model.getField0()
+ "'").uniqueResult());
}
3)
@Test
public void shouldFindModelByField() {
Model model = createRandomModel();
session.save(model);
model.setField0("TEST1");
session.save(model);
session.flush();
assertTrue(null != session.createSQLQuery(
"select id from model where field0 = '" + model.getField0()
+ "'").uniqueResult());
}
问题: 1)如果按原样运行,为什么测试失败? 2)哪种变体是正确的? 3)如果没有,如何更正代码?
答案 0 :(得分:2)
SELECT
语句之前刷新(否则你将无法找到你刚才持有的记录)。save()
的结果是PERSISTED
实体,必须有ID)。因此,无论您选择哪个FlushMode,ORM都会发出INSERT
语句,如果这是生成ID所需的内容。如果您要使用序列生成器 - INSERT
可能会被推迟,但您使用身份 - 由于在INSERT
期间数据库生成了ID,因此无法推迟使用身份。将FlushMode设置为COMMIT,这意味着Hibernate在事务提交之前执行SQL。因此,当您更新您的实体时,不会调用UPDATE语句。只有在你提交事务时才会调用它(你永远不会这样做)。
即使使用FlushMode COMMIT,新实体的原始INSERT语句仍然会执行 - 必须生成ID,对吗?
在evict()
实体后,Hibernate不再了解它。当你再次save()
时,Hibernate认为这是一个全新的对象,因此它需要再次插入它,因为它需要生成新的ID。
因此,不是更新原始实体,而是实际插入一个新行,最后在DB中有两个对象。您的测试通过,因为它选择了第二个(字段已设置为正确的值)。
好吧,你实际上提交了事务,所以Hibernate刷新了所有的SQL,包括UPDATE语句。您的FlushMode = COMMIT按预期工作。
在这种情况下,您手动flush()
更改 - 无论您选择哪种刷新模式,它都会执行SQL语句。
首先SpringJUnit4ClassRunner
支持测试的@Transactional
注释。因此,而不是在@Before
& @After
您可以使用注释。
其次,为了确保测试确实有效,您需要手动刷新和清除第一级缓存。否则,您可能会使用缓存实体而不是真正的数据库。所以你的测试看起来像这样:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/testContext.xml")
@Transactional
public class MainTest {
...
@Test public void canUpdateAllTheFields() {
Model original = createRandomModel();
session.save(original);
session.flush();
session.clear();
Model updates = createRandomModel();
model.setId(original.getId());
session.update(updates);
session.flush();
session.clear();
assertReflectionEquals(updates, session.get(Model.class, original.getId()));
}
}
注意:
assertReflectionEquals()
来自Unitils lib。