-1

我在 mysql 中有表类别(cat_id,categoryName),我在 Spring 中使用 hibernate 检索它的记录。

但它只是检索 cat_id 而不是 categoryName

这是我的类别类映射到表

@Entity
@Table(name="categories")
public class Category {
    @Id
    @Column(name="cat_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    
    
    
    @Column(name="categoryName")
    private String categoryName;
    
    public Category() {
        
    }
    public Category(String categoryName) {
        super();
        this.categoryName = categoryName;
    }

    @Override
    public String toString() {
        return "Category [id=" + id + ", categoryName=" + categoryName + "]";
    }

    public int getId() {
        return id;
    }

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

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
    
}

这是我用来链接到前端的控制器类

public class mainController {

    private CategoryService aCategoryService;
    
    @Autowired
    public mainController(CategoryService a2CategoryService)
    {
        aCategoryService= a2CategoryService;
    }
    
    @GetMapping("/")
    public String homePage(Model theModel)
    {
        List<Category> theCategory= aCategoryService.findall();
        System.out.println(theCategory);
        theModel.addAttribute("categories",theCategory);
        return "index2";
    }
}

这是与存储库交互的类别服务实现

@Service
public class CategoryServiceImpl implements CategoryService {

    private CategoryRepo categoryRepository;
    @Autowired
    public CategoryServiceImpl(CategoryRepo theCategoryRepo)
    {
        categoryRepository= theCategoryRepo;
    }
    @Override
    public List<Category> findall() {
        // TODO Auto-generated method stub
        return  categoryRepository.findAll();
        
    }

    @Override
    public Category findById(int theId) {
        // TODO Auto-generated method stub
        Optional<Category> result = categoryRepository.findById(theId);
        Category theCategory= null;
        if (result.isPresent()) {
            theCategory = result.get();
            }
            else {
            // we didn't find the employee                  
            throw new RuntimeException("Did not find category id - " + theId);                  
            }                                   
            return theCategory;             
    }

    @Override
    public void save(Category theCategory) {
        // TODO Auto-generated method stub
        categoryRepository.save(theCategory);
    }

    @Override
    public void deleteById(int theId) {
        // TODO Auto-generated method stub
        categoryRepository.deleteById(theId);
    }

}

另外,我是 Java/Spring/Hibernate 的新手,我能做些什么来解决这个问题

控制台日志 Hibernate:从类别 category0_ [Category [id=1, categoryName=null], Category [id=2, categoryName=null]] 中选择 category0_.cat_id 作为 cat_id1_0_, category0_.category_name 作为 category2_0_

4

1 回答 1

0

你的数据库列名是蛇形的,而你的实体类是骆驼式的。

所以只需更改以下代码

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

@Column(name="category_name")
private String categoryName;
于 2021-02-13T13:27:29.580 回答