---
title: "Hiberante Interceptor"
author: "absolute"
date: "2007-09-20T14:16:24Z"
tags: []
slug: "hiberante-interceptor"
draft: false
lastmod: "2007-09-20T14:16:24Z"
id: 125642
url: "https://absolute.iteye.com/blog/125642"
---

java 代码

01. public class CryptoInterceptor extends EmptyInterceptor {
02. public boolean onLoad(Object entity, Serializable id, Object\[] state,
03. String\[] propertyNames, Type\[] types) {
04. if (entity instanceof Worker) {
05. for (int i = 0; i &lt; propertyNames.length; i++) {
06. if ("wkrPhone".equals(propertyNames\[i])) {
07. String phone = (String) state\[i];
08. if (phone != null && !"".equals(phone)) {
09. state\[i] = CryptoUtils.decrypt(phone);
10. }
11. }
12. }
13. }
14. return true;
15. }
16. public boolean onFlushDirty(Object entity, Serializable id, Object\[] state,
17. String\[] propertyNames, Type\[] types) {
18. if (entity instanceof Worker) {
19. for (int i = 0; i &lt; propertyNames.length; i++) {
20. if ("wkrPhone".equals(propertyNames\[i])) {
21. String phone = (String) state\[i];
22. if (phone != null && !"".equals(phone)) {
23. state\[i] = CryptoUtils.encrypt(phone);
24. }
25. }
26. }
27. }
28. return true;
29. }
30. public boolean onSave(Object entity, Serializable id, Object\[] state,
31. String\[] propertyNames, Type\[] types) {
32. if (entity instanceof Worker) {
33. for (int i = 0; i &lt; propertyNames.length; i++) {
34. if ("wkrPhone".equals(propertyNames\[i])) {
35. String phone = (String) state\[i];
36. if (phone != null && !"".equals(phone)) {
37. state\[i] = CryptoUtils.encrypt(phone);
38. }
39. }
40. }
41. }
42. return true;
43. }
44. public int\[] findDirty(Object entity, Serializable id,
45. Object\[] currentState, Object\[] previousState,
46. String\[] propertyNames, Type\[] types) {
47. boolean forceUpdate = false;
48. int\[] results = null;
49. int count = 0;
50. if (entity instanceof Worker) {
51. for (int i = 0; i &lt; types.length; i++) {
52. if (propertyNames\[i].equalsIgnoreCase("wkrPhone")) {
53. String currentPhone = (String) currentState\[i];
54. String previusPhone = (String) previousState\[i];
55. if (currentPhone != null && !"".equals(currentPhone)) {
56. if (!currentPhone.equals(previusPhone)) {
57. forceUpdate = true;
58. currentState\[i] = CryptoUtils.encrypt(currentPhone);
59. }
60. }
61. }
62. if (results == null) {
63. results = new int\[types.length];
64. }
65. results\[count++] = i;
66. }
67. }
68. if (count == 0) {
69. return null;
70. } else {
71. int\[] trimmed = new int\[count];
72. System.arraycopy(results, 0, trimmed, 0, count);
73. if (forceUpdate) {
74. return trimmed;
75. } else {
76. return null;
77. }
78. }
79. }
80. }