为什么我的hibernate4 session.save()只能被调用一次,不能连续调用

时间:2013-11-24 08:09:36

标签: java spring hibernate

这些是我的相关代码。我有hibernate4.2.6 + spring3.2.4

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="#{proConfig.driverClass}"/>
        ......
        <property name="testConnectionOnCheckin" value="#{proConfig.testConnectionOnCheckin}"/>
</bean>  
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource"/>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.current_session_context_class">
                org.springframework.orm.hibernate4.SpringSessionContext
            </prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <value>
            com.xxx.xxx.dao.FilmUrl
        </value>
    </property>
</bean>

<bean id="transactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="downloadTaskWith" propagation="REQUIRED"/>
        ......
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="daoMethods" expression="execution(* com.xxxx.xxxx.service.impl.*.*(..))" />
    <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
    <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
</aop:config>  

My FilmUrl:

@Entity
@Table(name="tb_download_film_url",catalog="crawlertest")
public class FilmUrl implements Serializable {
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id",unique = true,nullable = false)
    private int id;
    @Column(name="url",length = 45)
    private String url;
    @Column(name="isHandle")
    private int isHandle;
    @Column(name="type")
    private int type;
    @Id
    public int getId() {
    ....
    public void setType(int type) {
        this.type = type;
    }
}

我的道:

@Repository
public abstract class GenericDaoImpl<T, ID extends Serializable> implements GenericDao<T, ID> {
    private Class<T> entityClass;
    @Autowired
    private SessionFactory sessionFactory;

    protected GenericDaoImpl(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    private Session currentSession(){
        return sessionFactory.getCurrentSession();
    }

    @Override
    public ID saveEntity(T entity) {
        return (ID)currentSession().save(entity);
    }

    @Override
    public void saveEntityBatch(Collection<T> objects) {
        for (T entity:objects){
            currentSession().save(entity);
//            currentSession().flush();
//            currentSession().clear();
        }
    }

这就是全部。在我的DAO.saveEntity函数中运行良好,saveEntityBatch无法正常工作。 如果我让currentSession()。flush();和currentSession()。clear()在currentSession()之后.save(实体),没关系。但是对它们进行评论,它会被阻止。 为什么?所以我在这里寻求帮助。 谢谢〜

1 个答案:

答案 0 :(得分:0)

看看代码。在这里,我通过检查在设置Hibernate的其他属性期间设置的批量大小来清除和刷新会话。

/**
 * @param entityList
 * @param batchSize
 * @return
 */
public long batchUpdate(final List<DomainObject> entityList, long batchSize) {
    Long insertedCount = 0L;
    for (int i = 0; i < entityList.size(); ++i) {
        update(entityList.get(i));
        if (++insertedCount % batchSize == 0) {
            flushAndClear();
        }
    }
    flushAndClear();
    return insertedCount;
}

/**
 * Flush and clear.
 */
void flushAndClear() {
    if (getSession().isDirty()) {
        getSession().flush();
        getSession().clear();
    }
}