spring + hibernate + struts 多数据问题,实在搞不通!请指教

forwarddream 2007-12-19 04:34:23
spring + hibernate + struts 实现跨数据库,现实现oracle可以,但却换到MYSQL服务类时,怎么都出错,不知道为什么。
如下

取得服务类:(ArticleService) AppContext.getInstance().getAppContext().getBean("articleService");
mysql出错,ORALCE却正常

applicationContext.xml 配置

<bean id="articleService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="articleTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="proxyTargetClass">
<value>true</value>
</property>
</bean>

oracle:<bean id="articleTarget" class="com.eweb.business.ArticleService" singleton="true">
<property name="articleDAO">
<ref local="articleDAO" />
</property>
</bean>
MYSQL::<bean id="articleTarget" class="com.eweb.business.ArticleSQLService" singleton="true">
<property name="articleDAO">
<ref local="articleSQLDAO" />
</property>
</bean>

ArticleSQLService即使和ArticleService一样,都报:
java.lang.ClassCastException: com.eweb.business.ArticleSQLService$$EnhancerByCGLIB$$4d6bdd81
com.eweb.util.BaseAction.getArticleService(BaseAction.java:37)
com.eweb.struts.action.ArticleAction.list(ArticleAction.java:101)
实在费解!

类:大致
public class ArticleSQLService implements IArticleService {
private IArticleDAO articleDAO;

public ArticleSQLService() {
super();
}

public IArticleDAO getArticleDAO() {
return articleDAO;
}

public void setArticleDAO(IArticleDAO articleDAO) {
this.articleDAO = articleDAO;
}
public List findByPage(Pager pager, Long catalogId){
int firstResult = (pager.getCurrentPage()-1)*pager.getPageSize();
int maxResults = pager.getPageSize();
return this.getArticleDAO().findByPage(firstResult, maxResults, catalogId);
}

public Long findByPageCount(Long catalogId) {
return this.getArticleDAO().findByPageCount(catalogId);
}
}
搞通宵都不通,实在晕,请各位指教。
...全文
138 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
forwarddream 2007-12-25
  • 打赏
  • 举报
回复
(ArticleService)AppContext.getInstance().getAppContext().getBean("articleService");
这里忘了改为接口,强行装换出错。
  • 打赏
  • 举报
回复
应该是方言了

另外MYSQL的ORACLE对二进制的操作也不同的,这点要注意
victoryzll 2007-12-19
  • 打赏
  • 举报
回复
2楼正解
yangxccom 2007-12-19
  • 打赏
  • 举报
回复
换不同的数据库,应该只改变datasource和映射文件就可以了的,
傻根她弟 2007-12-19
  • 打赏
  • 举报
回复
方言?

不太清楚
forwarddream 2007-12-19
  • 打赏
  • 举报
回复
抛出的错误
javax.servlet.ServletException: com.eweb.business.ArticleSQLService$$EnhancerByCGLIB$$be7a4243
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:423)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1158)
com.eweb.util.MyActionServlet.process(MyActionServlet.java:12)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.ClassCastException: com.eweb.business.ArticleSQLService$$EnhancerByCGLIB$$be7a4243
com.eweb.util.BaseAction.getArticleService(BaseAction.java:37)
com.eweb.struts.action.ArticleAction.list(ArticleAction.java:101)
com.eweb.struts.action.ArticleAction.execute(ArticleAction.java:49)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1158)
com.eweb.util.MyActionServlet.process(MyActionServlet.java:12)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

forwarddream 2007-12-19
  • 打赏
  • 举报
回复
不清楚吗?就是想实现切换数据库的功能,我只想改配置就可以实现切换不同的数据库功能,
2楼说的部分对,我也这样实现了,只不过ORACLE的CLOB的字段实在特别,与MYSQL的LONGTEXT实现方法不用,所以server类有些不同的,一般的基本都不用改,只改数据库链接就可以了。

我现在是有两个服务类oracle的com.eweb.business.ArticleService,MYSQL的com.eweb.business.ArticleSQLService

现在纳闷的是为什么我在applicationContext.xml切换到ArticleSQLService服务类时却出错,即使
和ArticleService类的内容是一样的。难道SPRING的配置跟名字有些关系??

错误com.eweb.util.BaseAction.getArticleService(BaseAction.java:37)
调用的就是:AppContext.getInstance().getAppContext().getBean("articleService");

oracle applicationContext.xml 配置

<bean id="articleService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="articleTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*"> PROPAGATION_REQUIRED </prop>
</props>
</property>
<property name="proxyTargetClass">
<value> true </value>
</property>
</bean>
<bean id="articleTarget" class="com.eweb.business.ArticleService" singleton="true">
<property name="articleDAO">
<ref local="articleDAO" />
</property>
</bean>
<bean id="articleDAO" class="com.eweb.dao.ArticleDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>

mysql applicationContext.xml 配置

<bean id="articleService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="articleTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="*"> PROPAGATION_REQUIRED </prop>
</props>
</property>
<property name="proxyTargetClass">
<value> true </value>
</property>
</bean>
<bean id="articleTarget" class="com.eweb.business.ArticleSQLService" singleton="true">
<property name="articleDAO">
<ref local="articleDAO" />
</property>
</bean>
<bean id="articleDAO" class="com.eweb.dao.ArticlesqlDAO">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>

为何单切换MYSQL server类就出错,大家注意到DAO原理是一样的,切换了都正常的。

dao接口类
public interface IArticleDAO {

public void save(Article transientInstance);

public void update(Article transientInstance);
}
ArticleDAO类(oracle)
public class ArticleDAO extends HibernateDaoSupport implements IArticleDAO{

public void save(Article transientInstance) {
log.debug("saving Article instance");
try {
//CLOB clob = (CLOB)transientInstance.getContent();
Session session = this.getSession();
transientInstance.setContent(Hibernate.createClob(" "));
session.save(transientInstance);
session.flush();
session.refresh(transientInstance, LockMode.UPGRADE);
CLOB clob = (oracle.sql.CLOB)((org.hibernate.lob.SerializableClob)transientInstance.getContent()).getWrappedClob();
clob.putString(1, transientInstance.getContentString());
//this.getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (Exception re) {
log.error("save failed", re);
re.printStackTrace();
//throw re;
}
}
}

ArticlesqlDAO类(mysql)

public class ArticlesqlDAO extends HibernateDaoSupport implements IArticleDAO{

public void save(Article transientInstance) {
log.debug("saving Article instance");
try {
this.getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (Exception re) {
log.error("save failed", re);
re.printStackTrace();
//throw re;
}
}

}

service接口类
public interface IArticleService {

public List findByPage(Pager pager, Long catalogId);

public Long findByPageCount(Long catalogId);

public void save(Article article);
}

ArticleService (orcale)
public class ArticleService implements IArticleService{
private IArticleDAO articleDAO;

public IArticleDAO getArticleDAO() {
return articleDAO;
}
public void save(Article article) {
this.getArticleDAO().save(article);
}
public Article findByTitleCatalog(String title,Long catalogId){
List list = this.getArticleDAO().findByTitleCatalog(title, catalogId);
if(list.size()>0)
{
Article article = (Article)list.get(0);
String content = "";
try {

if(article.getContent()!=null)
{
System.out.print("content :" + article.getContent().getSubString(1, (int)article.getContent().length()));
content = article.getContent().getSubString(1, (int)article.getContent().length());
article.setContentString(content);
}
else
{
article.setContentString("");
System.out.print("content :null");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return article;
}
else
{
return null;
}
}
}
ArticleSQLService (mysql)
public class ArticleSQLService implements IArticleService {
private IArticleDAO articleDAO;

public ArticleSQLService() {
super();
}

public IArticleDAO getArticleDAO() {
return articleDAO;
}
public void save(Article article) {
this.getArticleDAO().save(article);
}
public Article findByTitleCatalog(String title,Long catalogId){
List list = this.getArticleDAO().findByTitleCatalog(title, catalogId);
if(list.size()>0)
{
Article article = (Article)list.get(0);
return article;
}
else
{
return null;
}
}
}

就是ORACLE和mysql大字段实现不同,所以得切换service类和dao类。为什么dao类无论什么情况下都成功,而service类切换到MYSQL服务类时却出错了,如果换成ORACLE服务类,DAO还是使用MYSQL DAO类不会出错的。

不知道大家看明白了没?

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧