mardi 14 avril 2015

Error creating bean with name 'transactionManager' defined in ServletContext resource Cannot unwrap to requested type [javax.sql.DataSource]

Please, help me fix this problem.


[RMI TCP Connection(3)-127.0.0.1] WARN org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]



my mvc-dispatcher-servlet.xml

<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:context="http://ift.tt/GArMu7"
xmlns:tx="http://ift.tt/OGfeU2"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/1jdM0fG
http://ift.tt/GArMu7
http://ift.tt/1jdLYo7
http://ift.tt/OGfeU2
http://ift.tt/18tm2Tg">

<context:component-scan base-package="com.springapp.mvc"/>

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

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

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

</beans>


hibernate.cfg.xml



<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://ift.tt/1fnOghG">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/company</property>
<property name="hibernate.connection.username">Kate</property>
<property name="hibernate.connection.password">Ant0987M@+</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">false</property>
<mapping class="com.springapp.mvc.User"/>
</session-factory>
</hibernate-configuration>


User.java



package com.springapp.mvc;

import javax.persistence.*;

@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;

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

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

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

public User() {
}

public Long getId() {
return id;
}

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

public String getFirstName() {
return firstName;
}

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

public String getLastName() {
return lastName;
}

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

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}


UserController.java



package com.springapp.mvc;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Controller
public class UserController {

@Autowired
private UserRepository userRepository;

static final Logger logger = LogManager.getLogger("MyLogger");

@RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.HEAD})
public String listUsers(ModelMap model) {

logger.info("in meth listUsers");
logger.trace("trace in listUsers");

model.addAttribute("user", new User());
model.addAttribute("users", userRepository.findAll());
return "users";
}

@RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.HEAD})
public String addUser(@ModelAttribute("user") User user, BindingResult result) {

logger.info("in meth addUser");

userRepository.save(user);

return "redirect:/"/* + userRepository.getClass().getName()*/;
}

@RequestMapping("/delete/{userId}")
public String deleteUser(@PathVariable("userId") Long userId) {

logger.info("in meth deleteUser");

userRepository.delete(userRepository.findOne(userId));

return "redirect:/";
}

@RequestMapping(value = "/api/users", method = {RequestMethod.GET, RequestMethod.HEAD})
public
@ResponseBody
String listUsersJson(ModelMap model) throws JSONException {
JSONArray userArray = new JSONArray();
for (User user : userRepository.findAll()) {
JSONObject userJSON = new JSONObject();
userJSON.put("id", user.getId());
userJSON.put("firstName", user.getFirstName());
userJSON.put("lastName", user.getLastName());
userJSON.put("email", user.getEmail());
userArray.put(userJSON);
}
return userArray.toString();
}
}


UserRepository.java



package com.springapp.mvc;


import org.springframework.data.jpa.repository.JpaRepository;


import org.springframework.data.repository.Repository;


import java.util.List;


public interface UserRepository extends Repository {


public S save(S entity);



public User findOne(Long aLong);

public List<User> findAll();

public void delete(User entity);


}


Aucun commentaire:

Enregistrer un commentaire