Compilation error: The method findOne (long) is undefined for the type UserCrud]

2

to see if someone can help me because I get an error in compiling the edit and delete and I do not know why it is

    The method findOne(long) is undefined for the type UsuarioCrud
] with root cause

java.lang.Error: Unresolved compilation problem: 
    The method findOne(long) is undefined for the type UsuarioCrud

at com.inezpre5.controlador.ControladorCrud.editar(ControladorCrud.java:63) ~[classes/:na]

I show you my classes user class

package com.inezpre5.modelo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Email;
//import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotEmpty;

@Entity
public class Usuario {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @NotEmpty
    @Length(min=5, max=24)
    private String nombre;
    @NotEmpty
    @Length(min=10, max=24)
    private String password;
    @NotEmpty
    @Email    
    private String email;

    public Usuario() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Usuario(String nombre, String password, String email) {
        this.nombre = nombre;
        this.password = password;
        this.email = email;
    }

    public long getId() {
        return id;
    }

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

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getPassword() {
        return password;
    }

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

    public String getEmail() {
        return email;
    }

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

}

Interface

package com.inezpre5.modelo;

import org.springframework.data.repository.CrudRepository;

public interface UsuarioCrud extends CrudRepository<Usuario, Long>{

}

controller

package com.inezpre5.controlador;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.inezpre5.modelo.Usuario;
import com.inezpre5.modelo.UsuarioCrud;


@Controller
@RequestMapping("/crud")
public class ControladorCrud {

@Autowired
private UsuarioCrud uc;

//aqui estarían listar y crear que funcionan perfectamente, el error me lo da el //findOne(id)

@RequestMapping(value="/editar/{id}", method=RequestMethod.GET)
public String editar(@PathVariable("id") long id, ModelMap mp){
    mp.put("usuario", uc.findOne(id));
    return "crud/editar";
}

@RequestMapping(value="/actualizar", method=RequestMethod.POST)
public String actualizar(@Valid Usuario usuario, BindingResult bindingResult, ModelMap mp){
    if(bindingResult.hasErrors()){
        mp.put("usuarios", uc.findAll());
    return "crud/lista";
    }
    Usuario user = uc.findOne(usuario.getId());
    user.setNombre(usuario.getNombre());
    user.setPassword(usuario.getPassword());
    user.setEmail(usuario.getEmail());
    uc.save(user);
    mp.put("usuario", user);
    return "crud/actualizado";
}
}

view

    <!doctype html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script src="https://code.jquery.com/jquery-3.3.1.min.js"
    integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
    crossorigin="anonymous"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
    integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
    crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
    integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
    crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
    integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
    crossorigin="anonymous"></script>
<title>Lista:Crud</title>
</head>

<body>
    <div class="container">
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3>Lista de Usuarios</h3>
            </div>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Nombre</th>
                        <th>Contraseña</th>
                        <th>Email</th>
                    </tr>
                </thead>
                <tbody>
                    <tr data-th-each="usuario :${usuarios}">
                        <td data-th-text="${usuario.id}"></td>
                        <td data-th-text="${usuario.nombre}"></td>
                        <td data-th-text="${usuario.password}"></td>
                        <td data-th-text="${usuario.email}"></td>
                        <td><a th:href="@{'/crud/editar/{id}'(id=${usuario.id})}">
                                <span class="fa-stack"><i
                                    class="glyphicon glyphicon-edit"></i></span>
                        </a></td>
                        <td><a th:href="@{'/crud/borrar/{id}'(id=${usuario.id})}">
                                <span class="fa-stack"><i
                                    class="glyphicon glyphicon-trash"></i></span>
                        </a></td>
                    </tr>
                </tbody>
            </table>
        </div>
        <br /> <a class="btn btn-info" href="/crud/nuevo">Nuevo Usuario</a>
    </div>
</body>

</html>

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.inezpre5</groupId>
    <artifactId>CrudSpringBoot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>CrudSpringBoot</name>
    <description>Crud Total con Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
    
asked by mantamusica 03.12.2018 в 13:05
source

1 answer

3

The findOne(id) method was replaced by findById(id) .
The new method returns a Optional that allows you to better manage the null , here This is the documentation .

With the new method, you can imitate the previous functionality in this way:

User user = uc.findById(usuario.getId()).orElse(null);

Using orElse() and similar methods you can alter what the method returns in case of null, or even throw an exception.

    
answered by 03.12.2018 / 13:30
source