code_junkie

Do you have a common base class for Hibernate entities?

Nov 11th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. @MappedSuperclass()
  2. public class BaseEntity {
  3.  
  4. private Long id;
  5. private Long version;
  6. ...
  7.  
  8. @Id @GeneratedValue(strategy = GenerationType.AUTO)
  9. public Long getId() {return id;}
  10.  
  11. public void setId(Long id) {this.id = id;}
  12.  
  13. @Version
  14. public Long getVersion() {return version;}
  15. ...
  16.  
  17. // Common properties
  18. @Temporal(TemporalType.TIMESTAMP)
  19. public Date creationDate() {return creationDate;}
  20. ...
  21. }
  22.  
  23. @Entity
  24. public class Customer extends BaseEntity {
  25. private String customerName;
  26. ...
  27. }
  28.  
  29. public abstract class BaseEntity implements Serializable {
  30.  
  31. public abstract Long getId();
  32. public abstract void setId(Long id);
  33.  
  34. /**
  35. * @see java.lang.Object#hashCode()
  36. */
  37. @Override
  38. public int hashCode() {
  39. final int prime = 31;
  40. int result = 1;
  41. result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
  42. return result;
  43. }
  44.  
  45. /**
  46. * @see java.lang.Object#equals(Object)
  47. */
  48. @Override
  49. public boolean equals(Object obj) {
  50. if (this == obj)
  51. return true;
  52. if (obj == null)
  53. return false;
  54. if (getClass() != obj.getClass())
  55. return false;
  56. BaseEntity other = (BaseEntity) obj;
  57. if (getId() == null) {
  58. if (other.getId() != null)
  59. return false;
  60. } else if (!getId().equals(other.getId()))
  61. return false;
  62. return true;
  63. }
  64.  
  65. /**
  66. * @see java.lang.Object#toString()
  67. */
  68. @Override
  69. public String toString() {
  70. return new StringBuilder(getClass().getSimpleName()).append(":").append(getId()).toString();
  71. }
  72.  
  73. /**
  74. * Prints complete information by calling all public getters on the entity.
  75. */
  76. public String print() {
  77.  
  78. final String EQUALS = "=";
  79. final String DELIMITER = ", ";
  80. final String ENTITY_FORMAT = "(id={0})";
  81.  
  82. StringBuffer sb = new StringBuffer("{");
  83.  
  84. PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(this);
  85. PropertyDescriptor property = null;
  86. int i = 0;
  87. while ( i < properties.length) {
  88.  
  89. property = properties[i];
  90. sb.append(property.getName());
  91. sb.append(EQUALS);
  92.  
  93. try {
  94. Object value = PropertyUtils.getProperty(this, property.getName());
  95. if (value instanceof BaseEntity) {
  96. BaseEntity entityValue = (BaseEntity) value;
  97. String objectValueString = MessageFormat.format(ENTITY_FORMAT, entityValue.getId());
  98. sb.append(objectValueString);
  99. } else {
  100. sb.append(value);
  101. }
  102. } catch (IllegalAccessException e) {
  103. // do nothing
  104. } catch (InvocationTargetException e) {
  105. // do nothing
  106. } catch (NoSuchMethodException e) {
  107. // do nothing
  108. }
  109.  
  110. i++;
  111. if (i < properties.length) {
  112. sb.append(DELIMITER);
  113. }
  114. }
  115.  
  116. sb.append("}");
  117.  
  118. return sb.toString();
  119. }
  120. }
Add Comment
Please, Sign In to add comment