0

I have Compnay and Branch with one-to-many relationship

Company

@Entity 
@Table(name = "tbl_company")
public class Company {

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

    ...

    @OneToMany(mappedBy = "company", cascade = CascadeType.ALL, fetch = LAZY)
    private Set<Branch> branches;

   ...
}  

Branch:

@Entity
@Table(name = "tbl_branch")
public class Branch {

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

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="branchcompany_id")
    private Company company;


    @Override
    public int hashCode() {
       int hash = 3;
       hash = 59 * hash + (int) (this.branchId ^ (this.branchId >>> 32));
       return hash;
    }

    @Override
    public boolean equals(Object obj) {
       if (this == obj) {
           return true;
       }
       if (obj == null) {
           return false;
       }
       if (getClass() != obj.getClass()) {
           return false;
       }
       final Branch other = (Branch) obj;
       if (this.branchId != other.branchId) {
           return false;
       }
       return true;
    }

}

I get following JSON for adding company + branch which does not have ID (my equals and hash is based upon ID). When I try to attach below 2 branches to company's branches set, I get only 1 branch because by default ID is 0 for both.

{
"companyName": "Cynosure Company",
"address": "Nashik",
"branches": [
  {
    "branchName": "my branch"
  },
  {
    "branchName": "my second branch"
  }
 ]
}

Some options may be 1) Convert into list (not possible in my case) 2) Save branches separately after saving company (no cascade) - not preferable

Else 3) Remove equals/hashcode - Currently there is no detachment and re-attachment. There are multiple users using the website and making simultaneous POST/PUT requests but no multiple session for one single user. So is it safe to go with option 3? would like to understand the risks involved.

Thanks in advance.

user2869612
  • 607
  • 2
  • 10
  • 32
  • You can simplify (and remove) your last `if` statement by using one return statement: `return this.branchId == other.branchId;` – Michael Nov 21 '16 at 11:55
  • My personal conclusion with Entities, hashCode and equals, is not to include anything that's in a relationship field. So basically I do as Michael has suggested above, or use another field or field combination that are unique in the DB. – garfield Nov 21 '16 at 13:00

1 Answers1

0

Well, this equals and hashcode for JPA entities is an old issue that still these days we face up into projects.

I would suggest you take a look on this stackoverflow answer.

Basically, what the article defends is to not relies on database generated fields for your equals and hashcode (aka the entity id), because is far common the need of working with data in memory before persisting it (as it is in your case), so then, you still don't have one identifier generated at the database.

Some people recommends you have a identifier that you could control yourself for the equals and hashcode, (but not get rid of the PK, as it is important for indexing the database).

But well, just read the answer I have pointed, and take your own conclusion on how to solve your issue properly!

Cheers, Nikolas

Community
  • 1
  • 1