有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate仅检索一列,另一列为null

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

但它只是检索cat_id,而不是categoryName

这是映射到表的my category类

@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 我能做些什么来解决这个问题

控制台日志 休眠:选择类别0。类别id为类别1\u 0\u的类别id。类别名称为类别2\u 0\u来自类别0_ [类别[id=1,categoryName=null],类别[id=2,categoryName=null]]


共 (1) 个答案

  1. # 1 楼答案

    数据库列名的大小写是snake,而实体类的大小写是camel

    所以只需更改以下代码

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

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