我在实体类中的变量名是assignToassignBy,上面在MySQL db中的列名分别是assigntoassignby

但是项目正在通过名称“ assignmentTo”和“ assignmentBy”创建它。

在删除.m2 / repository文件夹后,我删除了整个数据库并使用mvn clean install重新创建。

邮递员仍在GET API上返回“ assignmentTo”和“ assignmentBy”。

用“ assignTo”和“ assignBy”名称调用POST方法,仍然得到“ assignmentTo”和“ assignmentBy”。

类别LeadAssignment

@Entity
@Table(name = "lead_assignment")
public class LeadAssignment {

        @Id
        @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY )
        @Column(name = "laid", nullable = false, updatable = false)
        private Long id;

        @Column(name = "first_name", nullable = false)
        private String firstname;

        @Column(name = "last_name", nullable = false)
        private String lastname;

        @Column(name = "assignto" , nullable = false)
        private String assignTo;

        @Column(name = "assignby", nullable = false)
        private String assignBy;

        @Column(name = "requirement" , nullable = false)
        private String requirements;

        @Column(name = "remark" , nullable = false)
        private String remarks;


控制器类LeadAssignmentController

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping(value = "/leadassignment")
public class LeadAssignmentController {


    //@Autowired
    private LeadAssignmentDao leadAssignmentDao;

    LeadAssignmentController(LeadAssignmentDao leadAssignmentDao){
        this.leadAssignmentDao = leadAssignmentDao;
    }

    @GetMapping("/getall")
    List<LeadAssignment> getLeadAssignmentList() {
        System.out.println("inside lead adsdignment conntroller get mapping");
        return leadAssignmentDao.findAll();
    }

    @GetMapping("/get/{id}")
    Optional<LeadAssignment> getLeadAssignment(@PathVariable Long id) {
        return leadAssignmentDao.findById(id);
    }

     @GetMapping("/get/assignto/{assignTo}")
     LeadAssignment getLeadAssignmentAssignTo(@PathVariable String assignTo, @RequestParam Map<String, String> params){
            System.out.println("Inside start of lead assignment assign to");
            System.out.println(params);
            LeadAssignment result = leadAssignmentDao.findByAssignTo(assignTo);
            return result;
            //System.out.println("Inside end of get sales email");
    }

     @GetMapping("/get/assignby/{assignBy}")
     LeadAssignment getLeadAssignmentAssignBy(@PathVariable String assignBy, @RequestParam Map<String, String> params){
            System.out.println("Inside start of lead assignment by");
            System.out.println(params);
            LeadAssignment result = leadAssignmentDao.findByAssignBy(assignBy);
            return result;
            //System.out.println("Inside end of get sales email");
    }


    @DeleteMapping("/delete/{id}")
    public boolean deleteLeadAssignment(@PathVariable Long id) {
        leadAssignmentDao.deleteById(id);
        return true;
    }

    @PutMapping("/update/{id}")
    public LeadAssignment updateLeadAssignment(@RequestBody LeadAssignment leadAssignment, @PathVariable Long id) {
        System.out.println("Inside lead assignmet update method");
        Optional<LeadAssignment> found = leadAssignmentDao.findById(id);
        //if(!found.isPresent())
        leadAssignment.setId(id);
        leadAssignmentDao.save(leadAssignment);
        return found.get();
    }
    /*
    @PutMapping("/update/email/{email}")
    public LeadAssignment updateLeadAssignmentEmail(@RequestBody User user, @PathVariable String email ) {
        System.out.println("inside user email PUT method");
        User emailfind = userDao.findByEmail(email);
        user.setEmail(email);
        userDao.save(user);
        return emailfind;
    }
    */
    /*
    @PutMapping("/update/{id}")
    public User updateUser(@RequestBody User user) {
        return userDao.save(user);
    }
    */

    @PostMapping("/create")
    public LeadAssignment createLeadAssignment(@RequestBody LeadAssignment leadAssignment) {
        return leadAssignmentDao.save(leadAssignment);
    }
}


我的application.properties文件:

# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
# Connection url for the database "netgloo_blog"
#spring.datasource.url=jdbc:mysql://localhost:3306/lmsAngularSpring?createDatabaseIfNotExist=true
spring.datasource.url=jdbc:mysql://localhost:3306/lms
# Username and secret
spring.datasource.username=root
spring.datasource.password=test
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).
# Show or not log for each sql query
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

spring.jpa.properties.hibernate.current_session_context_class = org.springframework.orm.hibernate5.SpringSessionContext
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project

#line below was earlier un-commented
#spring.jpa.hibernate.ddl-auto=update

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.open-in-view=false
#spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy


邮递员预期产量:

  {
        "id": 1,
        "remarks": "demo3",
        "demanderRentLeadStatus": null,
        "demanderBuyLeadStatus": null,
        "supplierSellLeadStatus": null,
        "supplierRentLeadStatus": null,
        "sales": null,
        "lastname": "foo",
        "firstname": "bar",
        "assignTo": "david",
        "assignBy": "james",
        "requirements": "for rent out"
    }


邮递员的实际输出:

{
    "id": 1,
    "remarks": "demo3",
    "demanderRentLeadStatus": null,
    "demanderBuyLeadStatus": null,
    "supplierSellLeadStatus": null,
    "supplierRentLeadStatus": null,
    "sales": null,
    "lastName": null,
    "firstName": null,
    "assignmentTo": null,
    "assignmentBy": null,
    "requirments": null
}

最佳答案

重命名班级的getter和setter。使它像:
assignTo的getter和setter分别是getAssignTo()setAssignTo(String s)。并对assignBy执行相同操作。

关于java - Spring Boot变量重命名问题( hibernate ),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57253022/

10-12 16:39