]> git.mxchange.org Git - addressbook-lib.git/blobdiff - src/org/mxchange/addressbook/model/addressbook/entry/UserAddressbookEntry.java
No more @OneToMany as this is confusing, better @OneToOne and easy named queries :)
[addressbook-lib.git] / src / org / mxchange / addressbook / model / addressbook / entry / UserAddressbookEntry.java
index 4e39de3a120a216d3f666a03e0d56836aec65492..4ad8484b86d74b945d1b1e94a74c0ccaea928ec4 100644 (file)
@@ -17,7 +17,6 @@
 package org.mxchange.addressbook.model.addressbook.entry;
 
 import java.util.Calendar;
-import java.util.List;
 import javax.persistence.Basic;
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
@@ -26,7 +25,8 @@ import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
-import javax.persistence.ManyToOne;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
@@ -47,6 +47,11 @@ import org.mxchange.jusercore.model.user.User;
  */
 @Entity (name = "addressbook_entries")
 @Table (name = "addressbook_entries")
+@NamedQueries (
+               @NamedQuery (
+                               name = "AllAddressbookEntries",
+                               query = "SELECT e FROM addressbook_entries AS e WHERE e.addressbookId = :addressbook AND (e.addressbookEntryUserOwner = :owner OR e.addressbookEntryUserSharer = :sharer) ORDER BY e.addressbookEntryId ASC")
+)
 public class UserAddressbookEntry implements AddressbookEntry, Comparable<AddressbookEntry> {
 
        /**
@@ -55,12 +60,11 @@ public class UserAddressbookEntry implements AddressbookEntry, Comparable<Addres
        private static final long serialVersionUID = 178_581_768_581_960L;
 
        /**
-        * Id number
+        * Connection to table "business_contacts" (commercial contacts)
         */
-       @Id
-       @GeneratedValue (strategy = GenerationType.IDENTITY)
-       @Column (name = "entry_id", nullable = false, updatable = false)
-       private Long addressbookEntryId;
+       @JoinColumn (name = "entry_business_contact_id", updatable = false)
+       @OneToOne (targetEntity = CompanyContact.class, cascade = CascadeType.ALL)
+       private BusinessContact addressbookEntryBusinessContact;
 
        /**
         * When this address book entry has been created
@@ -71,39 +75,40 @@ public class UserAddressbookEntry implements AddressbookEntry, Comparable<Addres
        private Calendar addressbookEntryCreated;
 
        /**
-        * Connection to table "addressbooks"
+        * Id number
         */
-       @JoinColumn (name = "addressbook_id", nullable = false, updatable = false)
-       @OneToOne (targetEntity = UserAddressbook.class, optional = false, cascade = CascadeType.ALL)
-       private Addressbook addressbookId;
+       @Id
+       @GeneratedValue (strategy = GenerationType.IDENTITY)
+       @Column (name = "entry_id", nullable = false, updatable = false)
+       private Long addressbookEntryId;
 
        /**
-        * Connection to table "users" (who has initially created this entry
+        * Connection to table "contacts" (private contacts)
         */
-       @JoinColumn (name = "entry_origin_user_id", nullable = false, updatable = false)
-       @ManyToOne (targetEntity = LoginUser.class, optional = false, cascade = CascadeType.ALL)
-       private List<User> addressbookEntryUserOriginList;
+       @JoinColumn (name = "entry_private_contact_id", updatable = false)
+       @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.ALL)
+       private Contact addressbookEntryPrivateContact;
 
        /**
-        * Connection to table "users" (who has added this entry)
+        * Connection to table "users" (who has initially created this entry
         */
-       @JoinColumn (name = "entry_added_user_id", updatable = false)
-       @ManyToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL)
-       private List<User> addressbookEntryUserAddedList;
+       @JoinColumn (name = "entry_owner_user_id", nullable = false, updatable = false)
+       @OneToOne (targetEntity = LoginUser.class, optional = false, cascade = CascadeType.ALL)
+       private User addressbookEntryUserOwner;
 
        /**
-        * Connection to table "contacts" (private contacts)
+        * Connection to table "users" (who has added this entry)
         */
-       @JoinColumn (name = "entry_private_contact_id", updatable = false)
-       @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.ALL)
-       private Contact addressbookEntryPrivateContact;
+       @JoinColumn (name = "entry_sharer_user_id", updatable = false)
+       @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.ALL)
+       private User addressbookEntryUserSharer;
 
        /**
-        * Connection to table "business_contacts" (commercial contacts)
+        * Connection to table "addressbooks"
         */
-       @JoinColumn (name = "entry_business_contact_id", updatable = false)
-       @OneToOne (targetEntity = CompanyContact.class, cascade = CascadeType.ALL)
-       private BusinessContact addressbookEntryBusinessContact;
+       @JoinColumn (name = "addressbook_id", nullable = false, updatable = false)
+       @OneToOne (targetEntity = UserAddressbook.class, optional = false, cascade = CascadeType.ALL)
+       private Addressbook addressbookId;
 
        @Override
        public int compareTo (final AddressbookEntry addressbookEntry) {
@@ -151,23 +156,23 @@ public class UserAddressbookEntry implements AddressbookEntry, Comparable<Addres
        }
 
        @Override
-       public List<User> getAddressbookEntryUserAddedList () {
-               return this.addressbookEntryUserAddedList;
+       public User getAddressbookEntryUserSharer () {
+               return this.addressbookEntryUserSharer;
        }
 
        @Override
-       public void setAddressbookEntryUserAddedList (final List<User> addressbookEntryUserAddedList) {
-               this.addressbookEntryUserAddedList = addressbookEntryUserAddedList;
+       public void setAddressbookEntryUserSharer (final User addressbookEntryUserSharer) {
+               this.addressbookEntryUserSharer = addressbookEntryUserSharer;
        }
 
        @Override
-       public List<User> getAddressbookEntryUserOriginList () {
-               return this.addressbookEntryUserOriginList;
+       public User getAddressbookEntryUserOwner () {
+               return this.addressbookEntryUserOwner;
        }
 
        @Override
-       public void setAddressbookEntryUserOriginList (final List<User> addressbookEntryUserOriginList) {
-               this.addressbookEntryUserOriginList = addressbookEntryUserOriginList;
+       public void setAddressbookEntryUserOwner (final User addressbookEntryUserOwner) {
+               this.addressbookEntryUserOwner = addressbookEntryUserOwner;
        }
 
        @Override