写入:
改造测试用例
tfw.integration_demo._04_spring_web_mvc._02_crud._04_hibernate_crud.user_mgr_testcase._01_basic_way._01_Hibernate_Operation
,添加方法,测试:
……
public class _01_Hibernate_Operation
{
……
@Before
public void init() throws IOException
{
……
}
@Test
public void testHibernate_create()
{
// [S] Preparing.
User user;
Session session;
Transaction ts;
{
String strDate = DateToolE.simpleFormat(new Date(), "HH-mm-ss.SSS");
String strPasswd = null;
// "Password";
user = // null; // [!][X] Always exception.
new User(null, "Hibernate_N_" + strDate, strPasswd,
new Byte((byte) 0), "Hibernate_R_" + strDate);
System.out.println("User:\n\t" + user);
session = sf.openSession();
System.out.println("Session:\n\t" + session);
ts = session.beginTransaction();
System.out.println("Transaction:\n\t" + ts);
}
// [E] Preparing.
// [S] Main.
{
Serializable srRtn = session.save(user);
System.out.println("Result:\t" + srRtn);
ts.commit();
System.out.println("Transaction Commited:\n\t" + ts);
session.close();
System.out.println("Session Closed:\n\t" + session);
System.out.println("User:\n\t" + user);
}
// [E] Main.
}
@Test
public void testHibernate_findById()
{
……
}
……
@Test
public void testHibernate_listByRange()
{
……
}
@Test
public void testHibernate_update()
{
// [S] Preparing.
Integer itgUserId;
User user;
Session session;
{
itgUserId = // null; // [!] Always exception.
10017;
// 99999;
session = sf.openSession();
System.out.println("Session:\n\t" + session);
user = (User) session.get(User.class, itgUserId);
System.out.println("User (before):\n\t" + user);
session.close();
System.out.println("Session Closed:\n\t" + session);
session = sf.openSession();
System.out.println("Session:\n\t" + session);
String strDate = DateToolE.simpleFormat(new Date(),
"yyyy-MM-ss_HH-mm-ss.SSS");
user = // null; // [!] Always exception.
new User(itgUserId, "Name_" + strDate, "aaa", (byte) 2,
"Remark_" + strDate);
System.out.println("User (new):\n\t" + user);
}
// [E] Preparing.
// [S] Main.
{
Transaction ts = session.beginTransaction();
System.out.println("Transaction:\n\t" + ts);
session.update(user);
System.out.println("Result:\n\tvoid");
ts.commit();
System.out.println("Transaction Commited:\n\t" + ts);
}
// [E] Main.
// [S] Confirmation.
{
user = (User) session.get(User.class, itgUserId);
session.close();
System.out.println("Session Closed:\n\t" + session);
System.out.println("User (updated):\n\t" + user);
}
// [E] Confirmation.
}
@Test
public void testHibernate_discardById()
{
// [S] Preparing.
Integer itgUserId;
User user;
Session session;
{
itgUserId = // null; // [!] Always exception.
10017;
// 99999;
session = sf.openSession();
System.out.println("Session:\n\t" + session);
user = (User) session.get(User.class, itgUserId);
System.out.println("User (before):\n\t" + user);
// session.close();
// System.out.println("Session Closed:\n\t" + session);
//
// session = sf.openSession();
// System.out.println("Session:\n\t" + session);
}
// [E] Preparing.
// [S] Main.
{
Transaction ts = session.beginTransaction();
System.out.println("Transaction:\n\t" + ts);
user.setStatus((byte) -1);
System.out.println("User (new):\n\t" + user);
ts.commit();
System.out.println("Transaction Commited:\n\t" + ts);
}
// [E] Main.
// [S] Confirmation.
{
user = (User) session.get(User.class, itgUserId);
session.close();
System.out.println("Session Closed:\n\t" + session);
System.out.println("User (discarded):\n\t" + user);
}
// [E] Confirmation.
}
private void showUsers(List<User> lstUsers)
{
……
}
}
说明
/ 注意事项:
- 时间有限,
Hibernate
的参数验证方式和参数预处理未做研究,交由
DAO
实现类中的代码完成。
- “Transaction ts = session.beginTransaction();”、“ts.commit();”:
默认情况下,需要用程序代码开启和提交事务,才能令写入操作生效;
时间有限,暂不考虑操作失败时回滚。
- “user.setStatus(……);”:
在从
Hibernate
Session
中提取的实体对象和
Session
脱离关系之间,对实体对象内容的修改即是对数据库内容的修改;但依然需要提交事务才能生效。