Skip to content

Instantly share code, notes, and snippets.

@banterCZ
Last active December 20, 2015 16:58
Show Gist options
  • Select an option

  • Save banterCZ/6164789 to your computer and use it in GitHub Desktop.

Select an option

Save banterCZ/6164789 to your computer and use it in GitHub Desktop.
JPA abstract entity - verbose
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;
}
}
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