PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 19, 2022

[FIXED] How to provide an autoincrement ID from the client side, if said ID is the only unique value of the entity?

 May 19, 2022     hibernate, java, spring-boot, web-services     No comments   

Issue

I am using Java with Spring boot and Hibernate. I am mostly using auto increment primary keys for almost all my Entities. I have an entity that its primary key is autoincrement. That is the only value which is unique for said entity.

For context on what I am trying to achieve, I need a method that validates if the entity exists within the db table, I am also trying to implement the method to update or delete said entity. In this case an entity of the class Work Experience.

@Entity
@Table(name = "user")
public class User {

    //<editor-fold defaultstate="collapsed" desc="Variable Def">
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(name = "user_name", unique = true)
    @Getter
    @Setter
    private String user_name;

    @Column(name = "password")
    @Getter
    @Setter
    private String password;

    @Column(name = "reset_password_token")
    @Getter
    @Setter
    private String reset_password_token;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "person_id", referencedColumnName = "id")
    @Getter
    @Setter
    private Person person;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "role_id", referencedColumnName = "id")
    @Getter
    @Setter
    private Role role;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "business_profile_id", referencedColumnName = "id")
    @JsonBackReference
    @Getter
    @Setter
    private BusinessProfile business_profile;
    //</editor-fold>

    @OneToOne(mappedBy = "user")
    private UserRecord record;

    @OneToOne(mappedBy = "publisher")
    private JobHiredRecord job_hired_record_publisher;

    @OneToOne(mappedBy = "customer")
    private JobHiredRecord job_hired_record_customer;


}

This is my User class, I have no problem with user since User name is a unique value as well so I search by username.

I am able to add 1 Business Profile to User and within that business profile class, I can add a List of Work Experience. This is my Work Experience class:

@Entity
@Table(name = "work_experience")
public class WorkExperience {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(name = "title")
    @Getter
    @Setter
    private String title;

    @Column(name = "company_name")
    @Getter
    @Setter
    private String company_name;

    @Column(name = "start_date", columnDefinition = "DATE")
    @Getter
    @Setter
    private Date start_date;

    @Column(name = "end_date", columnDefinition = "DATE")
    @Getter
    @Setter
    private Date end_date;

    @Column(name = "description")
    @Getter
    @Setter
    private String description;

    @ManyToOne
    @JsonBackReference
    @JoinColumn(name = "business_profile_id", referencedColumnName = "id")
    @Getter
    @Setter
    private BusinessProfile business_profile;

}

Let's say I add 2 Work Experience entitys to the db with the same title, description and dates to the same business profile, basically exactly the same, except for the id which is autogenerated by the db. If I want to update one of those entities I would need to provide their Id either through the client side (which is the problem I am having, not sure how to achieve this), or to do something from the server-side (I am using Hibernate and spring boot). How can I go about this?


Solution

If you are using a JpaRepository function like save(), you can assume that the record was added to the DB as long as no exception is thrown, so you don't need to check if it exists. It would be a waste of time.



Answered By - coosta
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing