/**
* 采用UUID策略生成主键
*
*/
public void testUUID() {
Session session = null;
Transaction tran = null;
try {
session = HibernateUtils.getSession();
tran = session.getTransaction();
tran.begin();
//session.beginTransaction();
User1 user1 = new User1();
user1.setName("张三1");
session.save(user1);
tran.commit();
}catch(Exception e) {
e.printStackTrace();
tran.rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 采用native策略生成主键
*
*/
public void testNative() {
Session session = null;
Transaction tran = null;
try {
session = HibernateUtils.getSession();
tran = session.getTransaction();
tran.begin();
User2 user2 = new User2();
user2.setName("李四");
session.save(user2);
tran.commit();
}catch(Exception e) {
e.printStackTrace();
tran.rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 采用assigned手动生成主键
*
*/
public void testAssigned() throws Exception {
Session session = null;
Transaction tran = null;
try {
session = HibernateUtils.getSession();
tran = session.getTransaction();
tran.begin();
User3 user3 = new User3();
user3.setId("A0001");
user3.setName("王五");
session.save(user3);
tran.commit();
}catch(Exception e) {
e.printStackTrace();
tran.rollback();
throw new Exception();
}finally {
HibernateUtils.closeSession(session);
}
}
* 采用UUID策略生成主键
*
*/
public void testUUID() {
Session session = null;
Transaction tran = null;
try {
session = HibernateUtils.getSession();
tran = session.getTransaction();
tran.begin();
//session.beginTransaction();
User1 user1 = new User1();
user1.setName("张三1");
session.save(user1);
tran.commit();
}catch(Exception e) {
e.printStackTrace();
tran.rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 采用native策略生成主键
*
*/
public void testNative() {
Session session = null;
Transaction tran = null;
try {
session = HibernateUtils.getSession();
tran = session.getTransaction();
tran.begin();
User2 user2 = new User2();
user2.setName("李四");
session.save(user2);
tran.commit();
}catch(Exception e) {
e.printStackTrace();
tran.rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
/**
* 采用assigned手动生成主键
*
*/
public void testAssigned() throws Exception {
Session session = null;
Transaction tran = null;
try {
session = HibernateUtils.getSession();
tran = session.getTransaction();
tran.begin();
User3 user3 = new User3();
user3.setId("A0001");
user3.setName("王五");
session.save(user3);
tran.commit();
}catch(Exception e) {
e.printStackTrace();
tran.rollback();
throw new Exception();
}finally {
HibernateUtils.closeSession(session);
}
}