附上代码Costomer类
package hiber;
import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;public class Customer implements Serializable
{
int id;
String nm;
public Customer(){}
public int getId(){ return it;}
public void setId(int idd){this.id = idd;} public String getNm(){return nm;}
public void setNm(String name){this.nm=name;}
}
//////////////////////////////////////////////////////////////////////////
BusinessService类
package hiber;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import javax.servlet.*;
import java.io.*;
import java.util.*;
public class BusinessService {
public static SessionFactory sessionFactory;
public BusinessService(){ }
static{
try{
Configuration config = new Configuration();
config.addClass(hiber.Customer.class);
config.addClass(hiber.Order.class);
sessionFactory = config.buildSessionFactory();
}catch(Exception e){e.printStackTrace();}
} public void saveCustomer(Customer customer) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
session.save(customer);
tx.commit(); }catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public void f(){Customer customer=new Customer();customer.setNm("kkkkk");saveCustomer(customer);} public static void main(String[] args)throws Exception{
BusinessService bb=new BusinessService();
bb.f();
}}运行BusinessService类结果:"kkkkk"被持久化到数据库。如果其他类的main()函数有BusinessService bb=new BusinessService();
bb.f();"kkkkk"也会被持久化到数据库。//////////////////////////////////////////////////////////////////////////////////////////////////////////
Servlet类 DBServletpackage hiber;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class DBServlet extends HttpServlet { public void init(ServletConfig config)throws ServletException {super.init(config);} public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {doPost(request, response);} public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
try{
BusinessService bb=new BusinessService();bb.f();
}
catch(Exception e){e.printStackTrace();}
} public void destroy() {}
}服务器tomcat上运行DBServlet,"kkkkk"为什么不会被持久化到数据库?怎么办?
package hiber;
import java.io.Serializable;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;public class Customer implements Serializable
{
int id;
String nm;
public Customer(){}
public int getId(){ return it;}
public void setId(int idd){this.id = idd;} public String getNm(){return nm;}
public void setNm(String name){this.nm=name;}
}
//////////////////////////////////////////////////////////////////////////
BusinessService类
package hiber;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import javax.servlet.*;
import java.io.*;
import java.util.*;
public class BusinessService {
public static SessionFactory sessionFactory;
public BusinessService(){ }
static{
try{
Configuration config = new Configuration();
config.addClass(hiber.Customer.class);
config.addClass(hiber.Order.class);
sessionFactory = config.buildSessionFactory();
}catch(Exception e){e.printStackTrace();}
} public void saveCustomer(Customer customer) throws Exception{
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
session.save(customer);
tx.commit(); }catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
}
public void f(){Customer customer=new Customer();customer.setNm("kkkkk");saveCustomer(customer);} public static void main(String[] args)throws Exception{
BusinessService bb=new BusinessService();
bb.f();
}}运行BusinessService类结果:"kkkkk"被持久化到数据库。如果其他类的main()函数有BusinessService bb=new BusinessService();
bb.f();"kkkkk"也会被持久化到数据库。//////////////////////////////////////////////////////////////////////////////////////////////////////////
Servlet类 DBServletpackage hiber;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class DBServlet extends HttpServlet { public void init(ServletConfig config)throws ServletException {super.init(config);} public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {doPost(request, response);} public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
try{
BusinessService bb=new BusinessService();bb.f();
}
catch(Exception e){e.printStackTrace();}
} public void destroy() {}
}服务器tomcat上运行DBServlet,"kkkkk"为什么不会被持久化到数据库?怎么办?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货