1

I'm really new in Java world and scpecificly in Spring. I try to read a lot of tutoriel but I'm really confused. I create some entity mapped by annotation, I succed to persist it when I load my Application Context in a main class but I can't do it in my controller.

@Entity
@Table(name = "User")
public class User implements Serializable{

    @Id
    @GeneratedValue
    @Column(name = "id_user")
    private Long idUser;

    @Column(name = "first_name", nullable=false)
    private String firstName;

    @Column(name = "last_name", nullable=false)
    private String lastName;

    @Column(name = "email", nullable=false)
    private String email;

    @Column(name = "password", nullable=false)
    private String password;

    @Column(name = "birth")
    private Date birth;

    @Column(name = "avatar")
    private String avatar;

...



}

I have make sur to use javax.persistence.

In my dispachtcher servlet I load my spring-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.manager.spring" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/view/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>


    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:annotation-driven />

    <import resource="classpath*:spring-config.xml" />

</beans>

Spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      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">

    <!-- This sescion tells Spring context where to look for bean classes with appropriate annotations -->
      <context:component-scan base-package="com.manager.spring"></context:component-scan>


      <!-- Configure JDBC Connection-->
      <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/calendar" />
            <property name="username" value="root" />
            <property name="password" value="pass" />
      </bean>

      <!-- Configure Hibernate 4 Session Facotry -->
      <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.hbm2ddl.auto">update</prop>
                </props>
          </property>
          <property name="annotatedClasses">
                <list>
                      <value>com.manager.spring.entity.Event</value>
                      <value>com.manager.spring.entity.User</value>
                      <value>com.manager.spring.entity.DroitEvent</value>


       </list>
      </property>
</bean>

I create a Service for User

@Service("userSvc")
public class UserService {

       @Autowired
       private UserDAO userDAO;

       public void setUserDAO(UserDAO userDAO) {
             this.userDAO = userDAO;
       }

       public void create(User user){
           userDAO.create(user);
       }
}

And my UserDAO

@Component("userDAO")//This the id by which it will be auto wired
public class UserDAO extends BaseDAO{

       public void create(User user){
           try{
                getSession().save(user);
            }
            catch(HibernateException e){
                System.out.println("Error occured while saving data"+e.getMessage());
            }
       }


}

BaseDAO :

public class BaseDAO {

    @Resource(name="sessionFactory")
    protected SessionFactory sessionFactory;

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

    protected Session getSession(){
           return sessionFactory.openSession();
    }     
}

And this config is working when I create a user in a main class

 ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"spring-config.xml"});

  UserService userSvc = (UserService) ctx.getBean("userSvc");
  userSvc.create(user);

But when I try to create a user in my controller, I get an exception

Error occured while saving dataUnknown entity: com.manager.spring.entity.User

I really hope someone can help me, I'm not sur to have understand everything and I'm very sorry for my poor english ! Thx

4
  • You show the Event entity but it complains about your User entity. Commented Mar 27, 2014 at 21:09
  • Oh damn ! Sorry, I have edit it. Commented Mar 27, 2014 at 21:15
  • Is spring-config.xml meant to be Spring-context.xml, or vice versa? I'd also like to see BaseDAO. Commented Mar 27, 2014 at 21:45
  • I don't really know... I sid I was very confused with Spring... I add BaseDao on the question. Commented Mar 27, 2014 at 21:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.