Spring加Hibernate saveorUpdate不能正常工作?

时间:2013-06-01 07:20:05

标签: spring hibernate

当我注册时,它将进入成功页面。但数据不会保存到数据库中。请帮我解决这个错误。 orcale的这段代码 这也没有显示任何错误。我完全被这个错误打了一天

User.java

    package com.dash.dto;
    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.SequenceGenerator;
    import javax.persistence.Table;

    @Entity (name = "user")
    @Table(name="DASHUSER" ,schema = "JTS")
    public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    //@SequenceGenerator(name = "DashSeq", sequenceName = "DASH_SEQ")
    //@GeneratedValue(generator = "DashSeq")
    @Column(name="USERID",unique = true, nullable = false)
    private long id;        

    @Column(name="USERNAME",unique=true )
    private String username;

    @Column (name="PASSWORD")
    private String password;

    @Column(name="FIRSTNAME")
    private String firstname;

    @Column(name="LASTNAME")
    private String lastname;

    /**
    * @return the id
    */
    public long getId() {
    return id;
    }
   /**
    * @param id the id to set
    */
    public void setId(long id) {
    this.id = id;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the firstname
     */
    public String getFirstname() {
        return firstname;
    }

    /**
     * @param firstname the firstname to set
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    /**
     * @return the lastname
     */
    public String getLastname() {
        return lastname;
    }

    /**
     * @param lastname the lastname to set
     */
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

}

RegisterationController.java

package com.dash.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.dash.dto.User;
import com.dash.services.Service;


@SuppressWarnings("deprecation")
public class RegistrationController extends SimpleFormController {

    protected Service service;

    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, 
                                    Object command, 
                                    BindException errors)
    throws Exception {

        User user = (User)command;



        user = service.signup(user);



        String welcomeMsg = "Welcome " + user.getFirstname() + " " + user.getLastname();

                System.out.println("userName"+user.getFirstname() +"lastName"+user.getLastname());

        ModelAndView model = new ModelAndView(getSuccessView());
        model.addObject("message", welcomeMsg);
        return model;
    }

    /**
     * @param service the service to set
     */
    public void setService(Service service) {
        this.service = service;
    }

}

Service.java

package com.dash.services;
import com.dash.dto.User;
public interface Service {
    User login(User user);
    User signup(User user);

}

ServiceImpl.java

<code>    
package com.dash.services;
import java.util.List;
import org.hibernate.exception.ConstraintViolationException;
import com.dash.dao.UserDao;
import com.dash.dto.User;
public class ServiceImpl implements Service {
    protected UserDao userDao;

    /**
     * @param userDao the userDao to set
     */
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    ////////////////////////////////////////////////////////////////////////////
    ///                             Service Methods                          ///
    ////////////////////////////////////////////////////////////////////////////




    @Override
    public User login(User user) {

        List<User> uzers = userDao.findUser(new String[]{"username", "password"}, 
                 new Object[]{user.getUsername(), user.getPassword()});

        if(uzers.isEmpty())
            throw new RuntimeException("Username / Password Does not exist please try again....");

        User loginuser = (User)uzers.get(0);


        return loginuser;
    }



    @Override
    public User signup(User user) {

        try {
                            System.out.println("ServiceIMPLll Entered...");
            userDao.createUser(user);

        } catch(ConstraintViolationException e){
            throw new RuntimeException("There is already a user with the username " + user.getUsername());
        }
        return user;
    }
}

 </code>

UserDAO.java


<code>    
package com.dash.dao;
import java.util.List;
import com.dash.dto.User;

public interface UserDao {


    void createUser(User user);
}

UserDAOImpl.java

package com.dash.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.dash.dto.User;
import org.springframework.transaction.annotation.Transactional;


public class UserDaoImpl implements UserDao{

    private SessionFactory sessionFactory;

    public UserDaoImpl(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }

    protected Session getSession()
    {

            return sessionFactory.openSession();//.getCurrentSession();
    }

    @Override
    public void createUser(User user) {
                System.out.println("Create User in DAOIMPL.."); 
        getSession().saveOrUpdate("user",user);
                getSession().close();
                System.out.println("Updated or Created........."+user.getFirstname());
    }

}

0 个答案:

没有答案