Spring MVC with jdbc “table is not mapped”

允我心安 提交于 2019-12-25 02:57:27

问题


So I´m trying to make an application with all http request get, put, post, delete.I'm making first a login with mysql and spring 3. So here I got this:

login.jsp:

                    <form:form id="myForm" method="post"
                        class="bs-example form-horizontal" commandName="UsersLogin">

signup.jsp

<form:form id="myForm" method="post"
                        class="bs-example form-horizontal" commandName="Users">

persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="{http://java.sun.com/xml/ns/persistence} {http://java.sun.com/xml/ns/persistence_2_0.xsd}"
    version="2.0">

    <persistence-unit name="punit">
    </persistence-unit>

</persistence>

Here is my jpaContext.xml:

<context:annotation-config />

    <jpa:repositories base-package="com.portuzona.repository" />

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="punit" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
            </bean>
        </property>
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
                <entry key="hibernate.hbm2ddl.auto" value="validate" />
                <entry key="hibernate.format_sql" value="true" />
            </map>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/portuzona?autoReconnect=true&amp;createDatabaseIfNotExist=true&amp;" />
        <property name="username" value="root" />
        <property name="password" value="1234" />
    </bean>
</beans>

com.portuzona.model

Users model:

@Entity
@Table(name="users")
public class Users {

    @Id
    @GeneratedValue
    private Long id;

    @NotEmpty
    @Size(min=4, max=20)
    private String userName;

    @NotEmpty
    private String firstName;

    @NotEmpty
    private String lastName;

    @NotEmpty
    @Size(min=4, max=8)
    private String password;

    @NotEmpty
    private String status;


    @NotEmpty
    @Email
    private String emailAddress;


    @NotNull
    @Past
    @DateTimeFormat(pattern="MM/dd/yyyy")
    private Date dateOfBirth;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }



    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }   
}

USersLogin model:

public class UsersLogin {

    @NotEmpty
    @Size(min=4, max=20)
    private String userName;

    @NotEmpty
    @Size(min=4, max=8)
    private String password;

    public String getPassword() {
        return password;
    }

    public String getUserName() {
        return userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }   
}

com.portuzona.service

public interface UsersService {
    Users save(Users users);
    boolean findByLogin(String userName, String password);
    boolean findByUserName(String userName);

}

Implementation:

@Service("UsersService")
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersRepository usersRepository;

    @Transactional
    public Users save(Users users) {
        return usersRepository.save(users);
    }

    public boolean findByLogin(String userName, String password) {  
        Users stud = usersRepository.findByUserName(userName);

        if(stud != null && stud.getPassword().equals(password)) {
            return true;
        } 

        return false;       
    }

    public boolean findByUserName(String userName) {
        Users use = usersRepository.findByUserName(userName);

        if(use != null) {
            return true;
        }

        return false;
    }

}

com.portuzona.repository

@Repository("UsersRepository")
public interface UsersRepository extends JpaRepository<Users, Long> {

    @Query("SELECT s FROM users s WHERE s.userName = :userName")
    Users findByUserName(@Param("userName") String userName);

}

com.portuzona.controller

@Controller
@SessionAttributes("users")
public class UsersController {

    @Autowired
    private UsersService userService;

    @RequestMapping(value="/signup", method=RequestMethod.GET)
    public String signup(Model model) {
        Users users = new Users();      
        model.addAttribute("users", users);     
        return "signup";
    }

    @RequestMapping(value="/signup", method=RequestMethod.POST)
    public String signup(@Valid @ModelAttribute("users") Users users, BindingResult result, Model model) {      
        if(result.hasErrors()) {
            return "signup";
        } else if(userService.findByUserName(users.getUserName())) {
            model.addAttribute("message", "User Name exists. Try another user name");
            return "signup";
        } else {
            userService.save(users);
            model.addAttribute("message", "Saved users details");
            return "redirect:login.html";
        }
    }

    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String login(Model model) {          
        UsersLogin usersLogin = new UsersLogin();       
        model.addAttribute("UsersLogin", usersLogin);
        return "login";
    }

    @RequestMapping(value="/login", method=RequestMethod.POST)
    public String login(@Valid @ModelAttribute("UsersLogin") UsersLogin usersLogin, BindingResult result) {
        if (result.hasErrors()) {
            return "login";
        } else {
            boolean found = userService.findByLogin(usersLogin.getUserName(), usersLogin.getPassword());
            if (found) {                
                return "success";
            } else {                
                return "failure";
            }
        }

    }



}

I got this error when trying to access to /login or /signup where I have my .jsp with my forms:

sep 24, 2015 11:14:35 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet usersHibernateServlet
org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped [SELECT s FROM users s WHERE s.userName = :userName]

I have been with this for two days, looking for answers here but no kind of good results from my side... just gives me the same error, any idea?

来源:https://stackoverflow.com/questions/32774828/spring-mvc-with-jdbc-table-is-not-mapped

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!