Java Hibernate仅检索一列,另一列为null
我在mysql中有表类别(cat_id,categoryName),我正在春季使用hibernate检索它的记录 但它只是检索cat_id,而不是categoryName 这是映射到表的my category类Java Hibernate仅检索一列,另一列为null,java,mysql,spring,hibernate,spring-data-jpa,Java,Mysql,Spring,Hibernate,Spring Data Jpa,我在mysql中有表类别(cat_id,categoryName),我正在春季使用hibernate检索它的记录 但它只是检索cat_id,而不是categoryName 这是映射到表的my category类 @Entity @Table(name="categories") public class Category { @Id @Column(name="cat_id") @GeneratedValue(strategy=Ge
@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";
}
}
公共类主控制器{
私人分类服务与分类服务;
@自动连线
公共主控制器(类别服务a2CategoryService)
{
aCategoryService=a2CategoryService;
}
@GetMapping(“/”)
公共字符串主页(模型模型)
{
列出category=aCategoryService.findall();
System.out.println(类别);
添加属性(“类别”,即类别);
返回“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);
}
}
@服务
公共类CategoryServiceImpl实现CategoryService{
私人分类报告分类报告;
@自动连线
公共类别ServiceImpl(CategoryRepo theCategoryRepo)
{
categoryRepository=CategoryRepo;
}
@凌驾
公共列表findall(){
//TODO自动生成的方法存根
return categoryRepository.findAll();
}
@凌驾
公共类别findById(int theId){
//TODO自动生成的方法存根
可选结果=categoryRepository.findById(theId);
类别类别=空;
if(result.isPresent()){
theCategory=result.get();
}
否则{
//我们没有找到那个雇员
抛出新的RuntimeException(“未找到类别id-”+theId);
}
返回类别;
}
@凌驾
公共作废保存(类别){
//TODO自动生成的方法存根
分类保存(分类);
}
@凌驾
公共void deleteById(intheid){
//TODO自动生成的方法存根
categoryRepository.deleteById(theId);
}
}
另外,我还不熟悉Java/Spring/Hibernate
我能做些什么来解决这个问题
控制台日志
休眠:从类别类别0中选择类别0。类别id作为类别id 1\u 0,类别0。类别名称作为类别2\u 0_
[Category[id=1,categoryName=null],Category[id=2,categoryName=null]您的数据库列名是蛇形的,而您的in-entity类是驼色的 所以只需更改以下代码
@Column(name="categoryName")
private String categoryName;
与
您能添加表格创建脚本吗?您是指用于创建表格的SQL脚本吗?我从您的SQL中了解到,您必须在@column中添加列名。您在列名中使用了uu,但在类的属性中没有使用。
@Column(name="category_name")
private String categoryName;