1 package example; 2 3 import javax.persistence.*; 4 5 import javax.naming.InitialContext ; 6 import javax.naming.Context ; 7 8 22 @Entity 23 @Table(name="amber_session_user") 24 public class User { 25 @Id 26 @Column(name="id") 27 @GeneratedValue 28 private int _id; 29 30 @Basic 31 @Column(name="name") 32 private String _name; 33 34 @Basic 35 @Column(name="quest") 36 private String _quest; 37 38 @Basic 39 @Column(name="color") 40 private String _color; 41 42 public User() 43 { 44 } 45 46 public User(String name, String quest, String color) 47 { 48 _name = name; 49 _quest = quest; 50 _color = color; 51 } 52 53 56 public int getId() 57 { 58 return _id; 59 } 60 61 public void setId(int id) 62 { 63 _id = id; 64 } 65 66 69 public String getName() 70 { 71 return _name; 72 } 73 74 77 public String getQuest() 78 { 79 return _quest; 80 } 81 82 85 public String getColor() 86 { 87 return _color; 88 } 89 90 93 private Object writeReplace() 94 { 95 return new UserHandle(_id); 96 } 97 98 static class UserHandle { 99 private int _id; 100 101 UserHandle(int id) 102 { 103 _id = id; 104 } 105 106 109 private Object readResolve() 110 { 111 try { 112 Context ic = new InitialContext (); 114 115 EntityManager manager 116 = (EntityManager) ic.lookup("java:comp/env/persistence/PersistenceContext/example"); 117 118 return manager.find(User.class, _id); 120 } catch (Exception e) { 121 throw new RuntimeException (e); 122 } 123 } 124 } 125 } 126 | Popular Tags |