在我的Service中只有一个方法,如下:
public void insertOneNews(String src,Type type) throws Exception {
String temp = src.substring(0,src.lastIndexOf("/"))+"/sign"+src.substring(src.lastIndexOf("/"));
Parser parser = new Parser(temp);
parser.setEncoding("gb2312");
NodeList divList = parser.parse(new HasAttributeFilter("class", "gaishu"));

String title = "";
String content = "";
Div div = (Div) divList.elementAt(0);
Parser parser2 = new Parser(div.getChildrenHTML());
NodeList nodeList = parser2.parse(new TagNameFilter("div"));

Div div2 = (Div)nodeList.elementAt(0);
title = div2.getStringText();
System.out.println(title);
Div div3 = (Div)nodeList.elementAt(1);
content = div3.getStringText();
Item item = new Item();
item.setTitle(title);
item.setContent(content);
item.setType(type);
this.save(item);
}使用save方法使用无法保存到数据库, debug发现Item中不存在空值,到了save这里就无法保存了,一点异常也不报,我怀疑是事务没有提交的问题
但是,同一个包下面的另一个TypeSerivce就一切正常,下面是我的配置文件,和另一个Serivce的源码。
TypeService:
/**
 * 
 */
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.tags.Div;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.NodeVisitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springside.core.dao.HibernateEntityDao;import com.cn.ciecc.search.model.Type;@Service("typeService")
public class TypeService extends HibernateEntityDao<Type> {
@Autowired
private ItemService itemService;
public void insertAllTypes() {  // 这个方法保存 非常正常
String str = new String("A B C D E F G H J K L M N P Q R S T W X Y Z");
String[] typesStr = str.split(" ");
Type[] types = new Type[typesStr.length];
for (int i = 0; i < typesStr.length; i++) {
types[i] = new Type();
types[i].setTypeName(typesStr[i]);
this.save(types[i]);
}
}

public void insertItems(String before,String after, Integer type) throws Exception {
//得到当前新闻所属类别
final Type typeName = this.get(Type.class, type);  //insertOneNews为什么必须是final
String src = before+typeName.getTypeName().toLowerCase()+after;
//使用htmlParser获取每一条新闻的超链接
try {
Parser parser = new Parser(src);
parser.setEncoding("gb2312");
NodeList divList = parser.parse(new HasAttributeFilter("class","letrcon1"));
for (int j = 0; j < divList.size(); j++) {
Div div = (Div) divList.elementAt(j);
String divClass = div.getAttribute("class");
//取得id为news_list的div
if(divClass != null && divClass.equals("letrcon1")) {
div.accept(new NodeVisitor() {
//遍历news_list里面的所有超链接
public void visitTag(Tag tag) {
if(tag instanceof LinkTag) {
String href = ((LinkTag)tag).getLink();
if(!href.startsWith("http")) {
href = "http://。。/" + href;
}
//找到超链接,将这个超链接所在的页面进行处理,抓取数据
try{
itemService.insertOneNews(href,typeName);
}catch(Exception e) {
e.printStackTrace();
}

}
}
});
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" 
default-autowire="byName">

<!-- 属性文件读入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:jdbc/jdbc.properties</value>
</list>
</property>
</bean>

<!-- 配置数据源  dataSource -->
<bean id = "dataSource" class = "org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- <property name="initialPoolSize" value="10"/>
<property name="minPoolSize" value="10"/> 
<property name="maxPoolSize" value="150"/> 
<property name="maxIdleTime" value="25000"/>
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="1800"/> 
<property name="checkoutTimeout" value="2000"/> -->
</bean>

<!-- 配置Session工厂 -->
<!-- annotation.AnnotationSessionFactoryBean 使用标注  进行映射  -->
<bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource"><ref bean = "dataSource" /></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate_dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="com.cn.ciecc.search.model" />    
</bean>

<!-- Hibernate TransactionManager -->
<bean id = "transactionManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean = "sessionFactory" /></property>
</bean>

<!-- 支持 @Transactional 标记 -->
<tx:annotation-driven/> <!-- 支持 @AspectJ 标记-->
<aop:aspectj-autoproxy/> 

<!-- 以AspectJ方式 定义 AOP -->
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.cn.ciecc.search.service..*Service.*(..))" advice-ref="txAdvice"/>
<aop:advisor pointcut="execution(* org.springside.core.dao.*Dao.*(..))" advice-ref="txAdvice"/>
</aop:config>

<!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置.默认的设置请参考Spring文档事务一章. -->
<tx:advice id = "txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

<!-- 启用类扫描机制以启用注释驱动,使用annotation自动注册bean ,并检查@AutoWired @Required 的属性已注入 -->
<context:component-scan base-package="com.cn.ciecc.search" />
</beans>

解决方案 »

  1.   

    有没有人哪   块要hold不住了
      

  2.   

    把hibernate.show_sql改为true,看看控制台有对应的插入语句么?
      

  3.   

    Hibernate: select type0_.id as id1_0_, type0_.typeName as typeName1_0_ from bo_lucene_type type0_ where type0_.id=?
    Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)看,有啊,但是数据库中就是没有。
    而且奇怪的是,一个表是可以正常操作的,一个就不行。
      

  4.   


    Hibernate: select type0_.id as id1_0_, type0_.typeName as typeName1_0_ from bo_lucene_type type0_ where type0_.id=?
    Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)
    Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)
    Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)
    Hibernate: insert into bo_lucene_item (type_id, content, title) values (?, ?, ?)
      

  5.   

    好的 我操作一下Item表和Type表 
    看sql语句有什么不同
      

  6.   

    我操作这个正常的表  Type表  输出语句如下:
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    Hibernate: insert into bo_lucene_type (typeName) values (?)
    没有第一句的查询语句,还是表有问题,但还是不明
      

  7.   

    Item表先查询了一遍多对一的关系 ,这应该是正常的啊
    Hibernate: select type0_.id as id1_0_, type0_.typeName as typeName1_0_ from bo_lucene_type type0_ where type0_.id=?
      

  8.   

    问题应该出在Item和Type的映射关系配置上, 单独保存Type是没有问题。
    insertOneNews(String src,Type type) throws Exception
    这个方法的异常你try catch下,别throws,估计会有异常出现。
      

  9.   

    public void insertOneNews(String src,Type type) throws Exception {
    我把这个方法的  throws Exception  可以保存数据库了 
    但是新的问题又出现了,Item表中有id title content 和typeid字段  content字段中始终没有数据,其他三列都正常,而且,每次测试时,这个表只保存固定的49条记录,就不在保存了。期间抛出了一个异常 ,但不是每次都抛出,org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been ed as rollback-only
    查了一下,好像是因为嵌套事务惹起的
    帖子:http://yidianfengfan.iteye.com/blog/550826
      

  10.   

    你打印下content看有值没有?
      

  11.   

    打印是有值的,但是title可以保存进去,content则为空。
    Item item = new Item();
    item.setTitle(div2.getStringText());
    item.setContent(div3.getStringText());
    item.setType(type);
    this.save(item);
    System.out.println(div3.getStringText());
    我是这样先保存后输出的,明显这个save方法有问题,还有爆出的那个异常,可能是嵌套事务引起的原因。
    又或者是本身表就有问题
      

  12.   

    没有,其实是因为content字段类型是text ,在mysql中text数据显示在备注中了,列里面不显示。
    谢谢上面那位朋友的帮忙。