成功保存后无法更新

时间:2014-10-13 23:33:50

标签: java spring hibernate spring-mvc

我是Spring MVC的新手。在我的uploadPub.java文件中dService.saveDocument(doc);方法运行成功。我称之为获取记录的ID。然后我想用新值更新相同的记录。虽然失败了。没有错误消息或异常。我在Spring 4.0.5和hibernate 4.3.5上。这是文件。谢谢你的帮助。

uploadPub.java

    public int uploadPub(MultipartFile filea) { 
    String originalName = null;
    String path = null;
    Documents doc = new Documents("", "", 'N', DateUtils.getNow());

    int docId = dService.saveDocument(doc);

    try {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        if (filea != null && filea.getSize() > 0) {
            File targetFile = new File("C:\\yayinDoc");
            if (!targetFile.exists()) {
                targetFile.mkdir();
            }
            originalName = filea.getOriginalFilename();
            String extension = FilenameUtils.getExtension(originalName);
            if (extension.equals("doc") || extension.equals("docx")) {
                path = targetFile.getPath() + "\\" + docId + "." + extension;
                inputStream = filea.getInputStream();
                outputStream = new FileOutputStream(path);

                int readBytes = 0;
                byte[] buffer = new byte[8192];
                while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
                    outputStream.write(buffer, 0, readBytes);
                }
                outputStream.close();
                inputStream.close();

                Documents docUpdate = new Documents(docId, originalName, path, 'Y', DateUtils.getNow());

                dService.updateDocument(docUpdate);

                return docId;
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

DocumentsService.java

@Transactional
@Service("documentsService")
public class DocumentsService extends GenericManagerImp<Documents> {

@Autowired
private DocumentsDao documentsDao;

public DocumentsService() {
    super();
}

public DocumentsService(DocumentsDao documentsDao) {
    this.documentsDao = documentsDao;
}


public int saveDocument(Documents doc) {
    documentsDao.saveDocument(doc);
    return doc.getDocId();
}


public void updateDocument(Documents doc) {
    documentsDao.updateDocument(doc);
}


public void deleteDocument(Documents doc) {
    documentsDao.deleteDocument(doc);
}

}

DocumentsDao.java

@Repository("documentsDAO")
public class DocumentsDao extends GenericDaoImp<Documents> {

public DocumentsDao() {
    super(Documents.class);
    this.sessionFactory = HibernateUtil.getSessionFactory();

}


public void saveDocument(Documents documents) {
    save(documents);
}


public void updateDocument(Documents doc) {
    update(doc);
}


public void deleteDocument(Documents doc) {
    delete(doc);
}
}

GenericDaoImp.java

public class GenericDaoImp<T> implements GenericDao<T> {

private Class<T> persistentClass;

protected SessionFactory sessionFactory;



public GenericDaoImp(final Class<T> persistentClass) {
    this.persistentClass = persistentClass;
    this.sessionFactory = HibernateUtil.getSessionFactory();
}

public GenericDaoImp(final Class<T> persistentClass, SessionFactory sessionFactory) {
    this.persistentClass = persistentClass;
    this.sessionFactory = sessionFactory;
}

public SessionFactory getSessionFactory() {
    return this.sessionFactory;
}

public Session getSession() {
    Session currentSession = getSessionFactory().openSession();

    return currentSession;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}


public T save(T object) {
    getSession().save(object);
    return object;
}

public T persist(T object) {
    getSession().persist(object);
    return object;
}

public T update(T object) {
    getSession().update(object);
    return object;
}


public void delete(T object) {
    getSession().delete(object);
}
}

调度-servlet.xml中

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


<!-- Enables the Spring MVC @Controller programming model -->
<tx:annotation-driven transaction-manager="hibernateTransactionManager"  proxy-target-class="true"/>
<mvc:annotation-driven />

<context:annotation-config />
<context:component-scan base-package="com.pub.controller" />
<context:component-scan base-package="com.pub.service" />
<context:component-scan base-package="com.pub.dao" />
<context:component-scan base-package="com.pub.model" />


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

<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
   <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://xxx"></property>
    <property name="username" value="xxx"></property>
    <property name="password" value="xxx"></property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="hibernateProperties">
    <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>

</bean>

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="order" value="2" />
    <property name="prefix">
        <value>/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="order" value="1" />
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>

<mvc:default-servlet-handler />

2 个答案:

答案 0 :(得分:1)

了解hibernate object states.

问题是您正在使用update,但您的对象已分离。你可以用两种方式解决它;

  • 使用merge方法更新新创建的文档

    了解您的使用案例并使用mergeThis article

  • 使用相同的会话,您始终会创建一个新会话。所以相反,

    Session currentSession = getSessionFactory().openSession();

    使用类似的东西,

    Session currentSession = getSessionFactory().getCurrentSession();

<强>更新

尝试更新GenericDaoImpl课程

public T update(T object) {
    Session session = getSession();
    session.update(object);
    session.flush();
    return object;
}

希望有所帮助

答案 1 :(得分:0)

感谢您的回复。我应该使用相同的对象进行保存和更新。所以我保存了对象并更改了从db检索的相同对象并更新它。我用merge而不是update。再次感谢。工作代码:

public int uploadPub(MultipartFile filea, String ipAddress) {
    String originalName = null;
    String path = null;
    Documents doc = new Documents("eser", "eser", 'N', DateUtils.getNow());
    Documents docUpdate = dService.saveDocument(doc);

    try {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        if (filea != null && filea.getSize() > 0) {
            File targetFile = new File("C:\\PubFolder");
            if (!targetFile.exists()) {
                targetFile.mkdir();
            }
            originalName = filea.getOriginalFilename();
            String extension = FilenameUtils.getExtension(originalName);
            if (extension.equals("pdf")) {
                path = targetFile.getPath() + "\\" + docUpdate.getDocId() + "." + extension;
                inputStream = filea.getInputStream();
                outputStream = new FileOutputStream(path);

                int readBytes = 0;
                byte[] buffer = new byte[8192];
                while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) {
                    outputStream.write(buffer, 0, readBytes);
                }
                docUpdate.setActive('Y');
                docUpdate.setUploadDate(DateUtils.getNow());

                dService.mergeDocument(docUpdate);
                outputStream.close();
                inputStream.close();
                return docUpdate.getDocId();
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;