开发者

Data not getting stored in database

开发者 https://www.devze.com 2023-04-13 04:32 出处:网络
I was implementing springs and hibernate annotation in netbeans. When i try to save the data in database it doesn\'t happen. Table is getting created when i launch the application from netbeans but fa

I was implementing springs and hibernate annotation in netbeans. When i try to save the data in database it doesn't happen. Table is getting created when i launch the application from netbeans but fails to store data in it.

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
    <property name="url" value="jdbc:derby://localhost:1527/seed"/>
 开发者_C百科   <property name="username" value="root"/>
    <property name="password" value="root"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.vaannila.domain.User</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

dao class:

@Repository("userDao")
public class UserDAOImpl implements UserDAO {

@Autowired
private SessionFactory sessionFactory;

/*public void setSessionFactory(SessionFactory sessionFactory) {
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}*/

@Override
public void saveUser(User user) {
    sessionFactory.getCurrentSession().saveOrUpdate (user);
}

@Override
@SuppressWarnings("unchecked")
public List<User> listUser() {
    return (List<User>) sessionFactory.getCurrentSession().
                    createCriteria ( User.class ).list(); 
}

}

service class:

@Service("userService")
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class UserServiceImpl implements UserService {

    @Autowired
private UserDAO userDAO;

    @Override
public void saveUser(User user) {
    userDAO.saveUser(user);
}

@Override
@SuppressWarnings("unchecked")
public List<User> listUser() {
    return userDAO.listUser();
}

}

Please help me out.

Thanks in advance


The problem is that the transaction you have used is marked are rollback (readOnly) in the Service!

So you need to add @Transaction(readOnly=false) to your controller or service method or dao that triggers the store operation of the item.

For example:

@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class UserServiceImpl implements UserService {

    ...

    //Do not role back this transaction
    @Transactional(propagation=Propagation.SUPPORTS, readOnly=false)
    @Override
    public void saveUser(User user) {
       userDAO.saveUser(user);
    }
}

@See Spring Reference: Chapter 10.5.6 Using @Transactional form some details

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号