Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 17th, 2013  |  syntax: None  |  size: 4.52 KB  |  views: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. @Entity
  2. @Converter(name = "uuidConverter", converterClass = UUIDConverter.class)
  3. public class Person {
  4.  
  5.     private UUID mId;
  6.     private String mLogin;
  7.  
  8.     @Id
  9.     @UuidGenerator(name = "uuid")
  10.     @GeneratedValue(generator = "uuid")
  11.     @Column(name = "id", nullable = false)
  12.     @Convert("uuidConverter")
  13.     public UUID getId() {
  14.         return mId;
  15.     }
  16.  
  17.     @Column(name = "login", nullable = false)
  18.     public String getLogin() {
  19.         return mLogin;
  20.     }
  21.  
  22.     public Person setId(UUID id) {
  23.         mId = id;
  24.         return this;
  25.     }
  26.  
  27.     public Person setLogin(String login) {
  28.         mLogin = login;
  29.         return this;
  30.     }
  31. }
  32.        
  33. @IdClass(PersonPreference.Pk.class)
  34. @Table(name = "person_preference")
  35. @Entity
  36. @Converter(name = "uuidConverter", converterClass = UUIDConverter.class)
  37. public class PersonPreference {
  38.  
  39.     private Person mPerson;
  40.     private String mComponentUid;
  41.     private String mComponentProperties;
  42.  
  43.     @SuppressWarnings("UnusedDeclaration")
  44.     static class Pk implements Serializable {
  45.  
  46.         private String mComponentUid;
  47.         private UUID mPerson;
  48.  
  49.         public String getComponentUid() {
  50.             return mComponentUid;
  51.         }
  52.  
  53.         public void setComponentUid(String componentUid) {
  54.             mComponentUid = componentUid;
  55.         }
  56.  
  57.         @Convert("uuidConverter")
  58.         public UUID getPerson() {
  59.             return mPerson;
  60.         }
  61.  
  62.         public void setPerson(UUID person) {
  63.             mPerson = person;
  64.         }
  65.  
  66.         @Override
  67.         public int hashCode() {
  68.             return Objects.hashCode(mComponentUid, mPerson);
  69.         }
  70.  
  71.         @Override
  72.         public boolean equals(Object obj) {
  73.             if (obj == null) {
  74.                 return false;
  75.             }
  76.             if (getClass() != obj.getClass()) {
  77.                 return false;
  78.             }
  79.             final Pk other = (Pk) obj;
  80.             return Objects.equal(this.mComponentUid, other.mComponentUid) && Objects.equal(this.mPerson, other.mPerson);
  81.         }
  82.     }
  83.  
  84.     @Id
  85.     @ManyToOne(cascade = CascadeType.ALL)
  86.     @JoinColumn(name = "person_id", nullable = false)
  87.     public Person getPerson() {
  88.         return mPerson;
  89.     }
  90.  
  91.     @Id
  92.     @Column(name = "component_uid", nullable = false)
  93.     public String getComponentUid() {
  94.         return mComponentUid;
  95.     }
  96.  
  97.     @Column(name = "component_properties", nullable = false)
  98.     public String getComponentProperties() {
  99.         return mComponentProperties;
  100.     }
  101.  
  102.     public PersonPreference setPerson(Person person) {
  103.         mPerson = person;
  104.         return this;
  105.     }
  106.  
  107.     public PersonPreference setComponentUid(String componentUid) {
  108.         mComponentUid = componentUid;
  109.         return this;
  110.     }
  111.  
  112.     public PersonPreference setComponentProperties(String componentProperties) {
  113.         mComponentProperties = componentProperties;
  114.         return this;
  115.     }
  116.  
  117.     @Override
  118.     public int hashCode() {
  119.         return Objects.hashCode(mPerson, mComponentUid, mComponentProperties);
  120.     }
  121.  
  122.     @Override
  123.     public boolean equals(Object obj) {
  124.         if (obj == null) {
  125.             return false;
  126.         }
  127.         if (getClass() != obj.getClass()) {
  128.             return false;
  129.         }
  130.         final PersonPreference other = (PersonPreference) obj;
  131.         return Objects.equal(this.mPerson, other.mPerson)
  132.             && Objects.equal(this.mComponentUid, other.mComponentUid)
  133.             && Objects.equal(this.mComponentProperties, other.mComponentProperties);
  134.     }
  135. }
  136.        
  137. Person person = new Person()
  138.         .setLogin("PersonPreferencePersistenceTestLogin");
  139.  
  140.     PersonPreference personPreference = new PersonPreference()
  141.         .setPerson(person)
  142.         .setComponentUid("4028808C3AA49ABB013AA49ABB2B0000")
  143.         .setComponentProperties("{123}");
  144.  
  145.     mPersonPreferenceService.save(personPreference);
  146.  
  147.     Optional<PersonPreference> newPersonPreference = mPersonPreferenceService.getByPersonAndComponentUid(
  148.         person,
  149.         "4028808C3AA49ABB013AA49ABB2B0000"
  150.     );
  151.  
  152.     Assert.assertEquals(personPreference.getComponentProperties(), newPersonPreference.get().getComponentProperties());
  153.        
  154. --INSERT INTO PERSON (id, login) VALUES (?, ?)
  155.     bind => [f2ce518c-8f37-4fac-bf5b-c8225d228b28, PersonPreferencePersistenceTestLogin]
  156. --INSERT INTO person_preference (component_uid, component_properties, person_id) VALUES (?, ?, ?)
  157.     bind => [4028808C3AA49ABB013AA49ABB2B0000, {123}, f2ce518c-8f37-4fac-bf5b-c8225d228b28]
  158. --SELECT component_uid, component_properties, person_id FROM person_preference WHERE ((person_id = ?) AND (component_uid = ?))
  159.     bind => [null, 4028808C3AA49ABB013AA49ABB2B0000]
clone this paste RAW Paste Data