映射问题 Spring Model 类自动生成列并返回 null 作为列值

Mapping issue Spring Model class automatic generating column and returned null as column value

提问人:deepparmar 提问时间:11/6/2023 更新时间:11/6/2023 访问量:14

问:

这是我的MySQL表,名为Employee定义


CREATE TABLE `employee` (
  `EmployeeId` int NOT NULL,
  `LastName` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `FirstName` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `Title` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `ReportsTo` int DEFAULT NULL,
  `BirthDate` datetime DEFAULT NULL,
  `HireDate` datetime DEFAULT NULL,
  `Address` varchar(70) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `City` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `State` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `Country` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `PostalCode` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `Phone` varchar(24) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `Fax` varchar(24) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `Email` varchar(60) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  PRIMARY KEY (`EmployeeId`),
  KEY `IFK_EmployeeReportsTo` (`ReportsTo`),
  CONSTRAINT `FK_EmployeeReportsTo` FOREIGN KEY (`ReportsTo`) REFERENCES `employee` (`EmployeeId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

这是我的模型类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Table(name = "employee")
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Employee_Model {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "EmployeeId")
    Integer id;

    @Column(name = "FirstName")
    String FirstName;

    @Column(name = "LastName")
    String LastName;

    @Column(name = "Title")
    String Title;

    @Column(name = "ReportsTo")
    Integer ReportsTo;

    @Column(name = "BirthDate")
    LocalDateTime BirthDate;

    @Column(name = "HireDate")
    LocalDateTime HireDate;

    @Column(name = "Address")
    String Address;

    @Column(name = "City")
    String City;
    @Column(name = "State")
    String State;
    @Column(name = "Country")
    String Country;

    @Column(name = "PostalCode")
    String PostalCode;
    @Column(name = "Phone")
    String Phone;
    @Column(name = "Fax")
    String Fax;
    @Column(name = "Email")
    String Email;
}

当我点击端点时,它给了我以下输出

 {
        "id": 0,
        "state": "AB",
        "address": "11120 Jasper Ave NW",
        "country": "Canada",
        "lastName": null,
        "firstName": null,
        "city": "Edmonton",
        "postalCode": null,
        "phone": "+1 (780) 428-9482",
        "fax": "+1 (780) 428-3457",
        "email": "[email protected]",
        "reportsTo": null,
        "title": "General Manager",
        "birthDate": null,
        "hireDate": null
    }

我不明白为什么它在某些字段中给我 null,尽管数据在数据库中可用并且还更改了数据库表结构,如下所示

CREATE TABLE `employee` (
  `EmployeeId` int NOT NULL,
  `LastName` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `FirstName` varchar(20) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
  `Title` varchar(30) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `ReportsTo` int DEFAULT NULL,
  `BirthDate` datetime DEFAULT NULL,
  `HireDate` datetime DEFAULT NULL,
  `Address` varchar(70) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `City` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `State` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `Country` varchar(40) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `PostalCode` varchar(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `Phone` varchar(24) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `Fax` varchar(24) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `Email` varchar(60) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
  `employee_id` int NOT NULL,
  `birth_date` datetime DEFAULT NULL,
  `first_name` varchar(255) DEFAULT NULL,
  `hire_date` datetime DEFAULT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  `postal_code` varchar(255) DEFAULT NULL,
  `reports_to` int DEFAULT NULL,
  `id` int NOT NULL,
  PRIMARY KEY (`EmployeeId`),
  KEY `IFK_EmployeeReportsTo` (`ReportsTo`),
  CONSTRAINT `FK_EmployeeReportsTo` FOREIGN KEY (`ReportsTo`) REFERENCES `employee` (`EmployeeId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 

但是当我将@Column字段注释的 name 属性的值更改为小写字母时,它像这样工作正常

@Column(name = “birthdate”) LocalDateTime 出生日期;

@Column(name = “名字”) 字符串 FirstName;

你能解释一下它是如何工作的吗?

Hibernte 命名策略如何工作? 为什么它在 certin 字段中给我 null 值? 如果我将这些列名更改为小写,为什么它可以正常工作?

java mysql spring-boot hibernate spring-data-jpa

评论


答: 暂无答案