UserRepositoryJava 类型的 save(User) 方法未定义(67108964)

问题描述 投票:0回答:2

我是 JAVA 和 Spring boot 的新手。 我创建了一个 Spring Boot 项目,我使用以下代码创建了一个实体类:

package com.example.TEST_WEB;
import javax.persistence.*;
@Entity
@Table(name="users")
public class User {
    @Id
    //Strategy = GenerationType. IDENTITY : La génération de la clé primaire se fera à partir d’une Identité propre au SGBD.
    // Il utilise un type de colonne spéciale à la base de données.
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
     
    @Column(nullable = false, unique = true, length = 45)
    private String email;
     
    @Column(nullable = false, length = 64)
    private String password;
     
    @Column(name = "first_name", nullable = false, length = 20)
    private String firstName;
     
    @Column(name = "last_name", nullable = false, length = 20)
    private String lastName;
     
    // getters and setters are not shown 

    public String getEmail() {
        return this.email;
    }

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

    public String getPassword() {
        return this.password;
    }

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

    public String getFirstName() {
        return this.firstName;
    }

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

    public String getLastName() {
        return this.lastName;
    }

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

}

然后我用这段代码创建了一个界面:

import com.example.TEST_WEB.User;
import org.springframework.data.jpa.repository.JpaRepository;
 
public interface UserRepository extends JpaRepository<User, Long> {
 
}

为了测试我的代码,我创建了一个此类:

package com.example.TEST_WEB;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.annotation.Rollback;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

 

@DataJdbcTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
//une transaction gérée par un test doit être annulée une fois la méthode de test terminée
@Rollback(false)

public class UserRepositoryTests {
 
    @Autowired
    private UserRepository repo;

    @Autowired
    private TestEntityManager entityManager;
     
   
    // test methods go below
    @Test
    public void testCreatUser(){
    User user = new User();
    user.setEmail("[email protected]");
    user.setPassword("ravi2020");
    user.setFirstName("Ravi");
    user.setLastName("Kumar");
     
    User savedUser = repo.save(user);
     
    User existUser = entityManager.find(User.class, savedUser.getId());
     // assert c'est pour la vérification
    assertThat(user.getEmail()).isEqualTo(existUser.getEmail());

    }
}

--> 但这给了我 2 个错误:

  • UserRepositoryJava 类型未定义方法 save(User) (67108964) 和
  • UserJava 类型的 getId() 方法未定义(67108964)
java spring-boot maven jpa
2个回答
2
投票
  1. 您必须将

    UserRepository
    移至包裹中
    com.example.TEST_WEB

  2. 那么您必须从

    UserRepository
     中删除 
    src/test/java

  3. 最后将

    getId()
    setId()
    方法添加到 User 类


0
投票
package com.example.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.entity.Product;
import com.example.demo.repository.ProductRepository;

@Service
public class ProductService {
    @Autowired
    private ProductRepository repository;
    public Product saveProduct(Product product) {
        return repository.save(product);
    }
    
    
    public List<Product> saveProducts(List<Product> products)
    {
        
        return repository.saveAll(products);
    }

        
    public List<Product> getProducts()
    {
        return repository.findAll();    
        
    }
    
    public Product getProductById(int id){
        return repository.findById(id).orElse(null);    
        
    }
    
    public Product getProductByName(String name){
        return repository.findByName(name); 
        
    }
     
    public String deleProduct(int id) {
        repository .deleteById(id);
        return "product removed !! "+id;
        
    }
    public Product updateProduct(Product product) {
        Product existingProduct=repository.findById(product.getId()).orElse(null);
        existingProduct.setName(product.getName());

        existingProduct.setQuantity(product.getQuantity());
        existingProduct.setPrice(product.getPrice());
        return repository.save(product);

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.