riteshD
riteshD

Reputation: 63

Spring boot variable re-naming issue (Hibernate)

My variable names in entity class are assignTo and assignBy, The column name of the above in MySQL db are assignto and assignby respectively.

But project is creating it by name "assignmentTo" and "assignmentBy".

I have drop the whole databases and re-created with mvn clean install after deleting .m2/repository folder.

Postman is still returning "assignmentTo" and "assignmentBy" on GET API.

Called POST method with "assignTo" and "assignBy" names, still got "assignmentTo" and "assignmentBy".

Class 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;

Controller class 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);
    }
}

My application.properties file :

# ===============================
# = 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

Expected postman output:

  {
        "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"
    }

Actual output for postman:

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

Upvotes: 1

Views: 1116

Answers (1)

Null Pointer
Null Pointer

Reputation: 275

Rename getter and setter of your class. Make it like: getter and setter for assignTo are getAssignTo() and setAssignTo(String s) respectively. and do same for assignBy.

Upvotes: 2

Related Questions