From 9c1c0622658f5912782ee949f63b191430bfead4 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 12 Oct 2015 19:00:18 +0200 Subject: [PATCH] No more @OneToMany as this is confusing, better @OneToOne and easy named queries :) --- .../addressbook/entry/AddressbookEntry.java | 9 +-- .../entry/UserAddressbookEntry.java | 75 ++++++++++--------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/org/mxchange/addressbook/model/addressbook/entry/AddressbookEntry.java b/src/org/mxchange/addressbook/model/addressbook/entry/AddressbookEntry.java index 6984d6f..930fbb3 100644 --- a/src/org/mxchange/addressbook/model/addressbook/entry/AddressbookEntry.java +++ b/src/org/mxchange/addressbook/model/addressbook/entry/AddressbookEntry.java @@ -18,7 +18,6 @@ package org.mxchange.addressbook.model.addressbook.entry; import java.io.Serializable; import java.util.Calendar; -import java.util.List; import org.mxchange.addressbook.model.addressbook.Addressbook; import org.mxchange.jcontacts.contact.Contact; import org.mxchange.jcontactsbusiness.BusinessContact; @@ -92,21 +91,21 @@ public interface AddressbookEntry extends Serializable { *

* @return User who has added this entry */ - public List getAddressbookEntryUserAddedList (); + public User getAddressbookEntryUserSharer (); /** * Setter for user who has added this entry to his/her addressbook *

* @param addressbookEntryUserAdded User who has added this entry */ - public void setAddressbookEntryUserAddedList (final List addressbookEntryUserAdded); + public void setAddressbookEntryUserSharer (final User addressbookEntryUserAdded); /** * Getter for user who has initially created the address book entry *

* @return User who has created initially this address book entry */ - public List getAddressbookEntryUserOriginList (); + public User getAddressbookEntryUserOwner (); /** * Setter for user who has initially created the address book entry @@ -114,7 +113,7 @@ public interface AddressbookEntry extends Serializable { * @param addressbookEntryUserOrigin User who has created initially this * address book entry */ - public void setAddressbookEntryUserOriginList (final List addressbookEntryUserOrigin); + public void setAddressbookEntryUserOwner (final User addressbookEntryUserOrigin); /** * Getter for address book diff --git a/src/org/mxchange/addressbook/model/addressbook/entry/UserAddressbookEntry.java b/src/org/mxchange/addressbook/model/addressbook/entry/UserAddressbookEntry.java index 4e39de3..4ad8484 100644 --- a/src/org/mxchange/addressbook/model/addressbook/entry/UserAddressbookEntry.java +++ b/src/org/mxchange/addressbook/model/addressbook/entry/UserAddressbookEntry.java @@ -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 { /** @@ -55,12 +60,11 @@ public class UserAddressbookEntry implements AddressbookEntry, Comparable 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 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 getAddressbookEntryUserAddedList () { - return this.addressbookEntryUserAddedList; + public User getAddressbookEntryUserSharer () { + return this.addressbookEntryUserSharer; } @Override - public void setAddressbookEntryUserAddedList (final List addressbookEntryUserAddedList) { - this.addressbookEntryUserAddedList = addressbookEntryUserAddedList; + public void setAddressbookEntryUserSharer (final User addressbookEntryUserSharer) { + this.addressbookEntryUserSharer = addressbookEntryUserSharer; } @Override - public List getAddressbookEntryUserOriginList () { - return this.addressbookEntryUserOriginList; + public User getAddressbookEntryUserOwner () { + return this.addressbookEntryUserOwner; } @Override - public void setAddressbookEntryUserOriginList (final List addressbookEntryUserOriginList) { - this.addressbookEntryUserOriginList = addressbookEntryUserOriginList; + public void setAddressbookEntryUserOwner (final User addressbookEntryUserOwner) { + this.addressbookEntryUserOwner = addressbookEntryUserOwner; } @Override -- 2.39.5