1

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
}
5
  • 1
    You're running the wrong code. Commented Jul 29, 2019 at 11:58
  • @Zorglube I am running the right code. Just to be clear "assignmentTo" and "assignmentBy" whre my initial names of variable and later changed it to "assignTo" and "assignBy" Commented Jul 29, 2019 at 12:24
  • Did you try invoking the GET endpoint from the browser, it might be some sort of cache issue. Commented Jul 29, 2019 at 12:34
  • @EduardoEljaiek Tried that too, still its the same. The only entity that's working is "remarks" all else are picking up previous variable names. Not the current. As i didn't make changes to " remarks " , POST is working only on " remarks " . Commented Jul 29, 2019 at 12:41
  • If assignmentTo & assignmentBy are the initial names of variable and you changed to assignTo & assignBy and your code still produce assignmentTo & assignmentBy, you had change your code but you are running the wrong code !! (The wrong bitecode if you wanna be accurate) Commented Jul 29, 2019 at 12:53

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Changed the getter and setter to getAssignTo() and setAssignTo(). Thank you!
I got the similar auto-renaming problem, And it seems because my auto generated getter and setter are behave "as it wants". . In my code, I have variable called isAllowed but then the autogenerated getter and setter by my intellij are setAllowed and getAllowed, it removes the IS word. Thus makes the serialization output behave strangely. Just like this thread. It returns allowed instead of isAllowed. The problem spread up to my mapper class. . Now I change the getter and setter manualy to setIsAllowed and getIsAllowed and then it works perfectly well. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.