简述一下,有Users表和Store表 Store表里的user_id是外键,对应Users表里的users_id
一对多的关系就是Users:Store=1:n,一个用户可以有多个商铺
2012-6-21 18:43:34 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:183)
at com.demo.hibernate.dao.StoreDAO.isInsert(StoreDAO.java:87)
at com.demo.struts.actions.RegstoreAction.insert(RegstoreAction.java:45)
at com.demo.struts.actions.RegstoreAction.execute(RegstoreAction.java:30)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
StoreDAO是我自己写的
这是其中的一个方法:
public boolean isInsert(int user_id,String store_name, String store_type,
String huoyuan, String store_intro, String zip_code, String address)
throws HibernateException{
Session session=null;
Transaction tx=null;
Users users=null;
boolean isInsert=true;
try{
session=HibernateSessionFactory.getSession();
tx=session.beginTransaction();
UsersDAO usersDAO=new UsersDAO();
users=usersDAO.queryUsers(user_id);
Store store=new Store();
store.setUsers(users);
users.getStores().add(store);
store.setStoreName(store_name);
store.setStoreType(store_type);
store.setHuoyuan(huoyuan);
store.setStoreIntro(store_intro);
store.setZipCode(zip_code);
store.setAddress(address);
session.save(store);
tx.commit();
tx=null;
}catch(HibernateException e){
isInsert=false;
throw e;
}
finally{
if(tx!=null){
tx.rollback();
}
HibernateSessionFactory.closeSession();
}
return isInsert;
}
配置文件应该没问题。
store的:
<many-to-one name="users" class="com.demo.hibernate.beans.Users" fetch="select">
<column name="user_id" not-null="true" />
</many-to-one>
users的:
<set name="stores" inverse="true" cascade="all" lazy="false">
<key>
<column name="user_id" not-null="true" />