Permalink
Browse files

Add CompositeLongId

  • Loading branch information...
1 parent 8013c68 commit d5bb283d1eddc5926e74c3e3f92af6e79f9c6ae2 @jirutka committed Jun 16, 2012
Showing with 63 additions and 0 deletions.
  1. +1 −0 README.md
  2. +62 −0 src/main/java/cz/jirutka/commons/hibernate/CompositeLongId.java
View
@@ -8,6 +8,7 @@ My collection of reusable Java classes for Hibernate.
- Implementation of [GenericDAO](http://github.com/jirutka/commons-persistence/) for Hibernate
- Abstract decorator for _Criteria_ (with generic method chaining)
- Extended decorator for _Criteria_ that implements the Visitor pattern for easy extensibility
+- Embeddable class for composite identifier of two Long types
- HibernateUtils
@@ -0,0 +1,62 @@
+package cz.jirutka.commons.hibernate;
+
+import java.io.Serializable;
+import javax.persistence.Embeddable;
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
+
+/**
+ * Embeddable class for composite identifier of two Long types.
+ *
+ * @author Jakub Jirutka <jakub@jirutka.cz>
+ */
+@Embeddable
+public class CompositeLongId implements Serializable {
+
+ private Long firstId;
+ private Long secondId;
+
+
+ /**
+ * Default non-parametric constructor.
+ */
+ protected CompositeLongId() {
+ }
+ /**
+ * Create a new composite identifier.
+ *
+ * @param firstId
+ * @param secondId
+ */
+ public CompositeLongId(Long firstId, Long secondId) {
+ this.firstId = firstId;
+ this.secondId = secondId;
+ }
+
+
+ @Override
+ public String toString() {
+ return "(" + firstId + "," + secondId + ")";
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) return false;
+ if (getClass() != obj.getClass()) return false;
+
+ CompositeLongId other = (CompositeLongId) obj;
+ return new EqualsBuilder()
+ .append(firstId, other.firstId)
+ .append(secondId, other.secondId)
+ .isEquals();
+ }
+
+ @Override
+ public int hashCode() {
+ return new HashCodeBuilder(7, 83)
+ .append(firstId)
+ .append(secondId)
+ .toHashCode();
+ }
+
+}

0 comments on commit d5bb283

Please sign in to comment.