我已经为延迟加载实现了 Spring/Hibernate 的 OpenSessionInView 模式。我面临“没有 Sessoon”或“2 个或更多会话”的问题。如果我遗漏任何步骤,请告诉我:
以下是详细代码片段:
Web.XML
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>flushModeName</param-name>
<param-value>FLUSH_AUTO</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
应用程序上下文.xml
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan"
value="com.mypack.common.model, com.mypack.school.model" />
.
.
<!-- Other properties -->
.
</bean>
客户.xhtml
XHTML 用户界面具有显示客户代码的自动完成框。自动完成查询使用 CompleteCustomer 方法,而 getCustomerbyCode 由转换器 bean 调用。
选择客户代码后,屏幕将由选定的客户详细信息填充。允许应用程序用户进行更改并将更新保存在数据库中。
CustomerBean.java
@ManagedBean (name="customerBean")
@ViewScoped
public class CustomerBean implements Serializable {
public List<Customer> completeCustomer(String query) {
List<Customer> suggestions;
suggestions = (List<Customer>) customerService.getCustomerslikeCodeOrName(query, authenticationBean.getTenantId());
return suggestions;
}
public void saveCustomerDetails() {
// Save or Update User Information in Database
customerService.saveCustomer(custMaster);
return;
}
CustomerServiceImpl.java
@Service("customerService")
@Transactional(readOnly = true, propagation=Propagation.REQUIRED)
public class CustomerServiceImpl implements CustomerService {
@Autowired
CustomerDAO custMasterDAO;
@Override
@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
public void saveCustomer(Customer custDetails) {
custMasterDAO.saveCustomer(custDetails);
}
@Override
public Customer getCustomerbyCode(String custCode, TenantId tenantId) {
return custMasterDAO.getCustomerbyCode(custCode, tenantId);
}
@Override
public List<Customer> getCustomerslikeCodeOrName(String custIDName, TenantId tenantId) {
return custMasterDAO.getCustomerslikeCodeOrName(custIDName, tenantId);
}
}
CustomerMasterDAO.java
@SuppressWarnings("unchecked")
@Override
public List<Customer> getCustomerslikeCodeOrName(String custIDName, TenantId tenantId) {
List<Customer> listCustomers = null;
custIDName = "%" + custIDName + "%";
Session session = SessionFactoryUtils.getSession(sessionFactory, false);
Transaction tx = session.beginTransaction();
try {
query = session.createQuery("from Customer " +
"where (custId like :codename " +
"or custFirstName like :codename " +
"or custMiddleName like :codename " +
"or custLastName like :codename)")
.setParameter("codename", custIDName);
listCustomers = query.list();
} catch(Exception ex) {
tx.rollback();
System.out.println("*** getCustomerslikeCodeOrName ***" + ex.getMessage());
}
return listCustomers;
}
@SuppressWarnings("unchecked")
@Override
public Customer getCustomerbyCode(String custCode, TenantId tenantId) {
List<Customer> list = null ;
Query query;
Session session = SessionFactoryUtils.getSession(sessionFactory, Boolean.FALSE);
Transaction tx = session.beginTransaction();
Filter sessionFilter = session.enableFilter("BankRecordFilter");
sessionFilter.setParameter("bankCode", tenantId.getBankCode());
try {
query = session.createQuery("from Customer where custId = :custCode");
list = query.setParameter("custCode", custCode).list();
} catch (Exception ex) {
tx.rollback();
System.out.println("*** getCustomerbyCode ***" + ex.getMessage());
}
if (list == null || list.size() < 1){
// System.out.println("No Records Found");
return null;
}
return (Customer)list.get(0);
}
@Override
public void saveCustomer(Customer custDetails) {
Session session = getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
try {
session.saveOrUpdate("bkCustomer", custDetails);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
e.printStackTrace();
}
}
表定义存储在 .hbm.xml 文件中。
我尝试了很多组合放置以下语句来获取会话: session = getSessionFactory().getCurrentSession(); 会话会话 = SessionFactoryUtils.getSession(sessionFactory, Boolean.FALSE);
也以不同的方式和不同的地点进行交易。我要么收到 No Session 的错误消息,要么收到两个或多个会话附加错误消息。
你能告诉我我的代码有什么问题吗?
问候,
希里什语