Last active
December 20, 2015 16:58
-
-
Save banterCZ/6164789 to your computer and use it in GitHub Desktop.
JPA abstract entity - verbose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.persistence.MappedSuperclass; | |
| @MappedSuperclass | |
| public abstract class AbstractEntity<PK> { | |
| public abstract PK getId(); | |
| public abstract void setId(PK id); | |
| @Override | |
| public int hashCode() { | |
| if (getId() != null) { | |
| return getId().hashCode(); | |
| } | |
| return super.hashCode(); | |
| } | |
| @Override | |
| public boolean equals(Object obj) { | |
| if (this == obj) { | |
| return true; | |
| } | |
| if (obj == null) { | |
| return false; | |
| } | |
| if (getClass() != obj.getClass()) { | |
| return false; | |
| } | |
| AbstractEntity<?> other = (AbstractEntity<?>) obj; | |
| if (getId() == null || other.getId() == null) { | |
| return false; | |
| } | |
| if (!getId().equals(other.getId())) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import javax.persistence.*; | |
| @Entity | |
| public class User extends AbstractEntity<Long> { | |
| @Id | |
| @SequenceGenerator(name = "userSequence", sequenceName="SQ_USERS") | |
| @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "userSequence") | |
| @Column(name = "USR_ID") | |
| private Long id; | |
| @Column(name = "USR_NAME", nullable = false) | |
| private String name; | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| @Override | |
| public Long getId() { | |
| return id; | |
| } | |
| @Override | |
| public void setId(Long id) { | |
| this.id = id; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment