Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:32:06+00:00 2026-05-27T08:32:06+00:00

I have a ‘best practice’ question for a scenario. Scenario: Multiple entities in a

  • 0

I have a ‘best practice’ question for a scenario.

Scenario:
Multiple entities in a DB, for example, Document, BlogPost, Wiki can be shared by individuals. Instead of creating a share table for each entity, a single Share table is created. The issue is, how to map the share table with different entities?

I have three options, please advise which option is best, and if there is a better option.

Option1:
Create table Shares as:

SHARES  
id (unique)
entityId (non DB enforced FK to DOCUMENTS, WIKIS, POSTS etc.)
entityType
sharedBy
sharedWith
sharedDate

Here, entityId will be a FK to documentId, wikiId, postId etc. etc. and entityType will identity what type the entityId is.

This has issues in Hibernate modelling, when creating Share to entity mapping, such as share.getDocument() or share.getWiki() etc.

Option 2:
Create table Shares which only holds share information, and then create resolution tables that tie the share to the entity.

SHARES
id(PK)
sharedBy
sharedWith
sharedDate
shareType (helper field for searches)

SHARES_DOCUMENTS
share_id (unique ID and FK, one to one with SHARES)
document_id (FK to DOCUMENTS)

SHARES_POST
share_id (unique ID and FK, one to one with SHARES)
post_id (FK to POSTS)

more share tables here.

So, hibernate wise, Share can have one to one for each of the share types (like share.getDocument(), share.getPost(), and shareType will identify which relationship is ‘active’ )

Option 3
Similar to option 1, but create individual columns instead of entity id

SHARES
id (unique ID)
documentId (FK to DOCUMENTS, nullable)
postId (FK to POSTS, nullable)
wikiId (FK to WIKIS, nullable)
sharedBy
sharedWith
sharedDate
sharedType

Here, each column could be mapped to respective entity, but they are nullable. sharedType can identify which relationship is ‘active’.

So, the question is , which practice is best, both database wise as well as hibernate mapping (and eventual querying, performance wise).

Thanks
M. Rather

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:32:06+00:00Added an answer on May 27, 2026 at 8:32 am

    As suggested by TheStijn, after looking into different ways to setup inheritance relationships, I went with ‘Single Table per class hierarchy’ approach, and ended up with the table like:

    SHARES
    ---------
    id PK
    shared_by FK to User
    shared_with FK to User
    shared_Date
    document_id nullable FK to Document
    post_id nullable FK to Posts
    ... more ids here to link to more entities
    type_discriminator (values, DOCUMENT, POST ... )
    

    On Hibernate/Java side,
    One Share abstract class as…

    @Entity
    @Table(name="SHARES")
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name="TYPE_DISCRIMINATOR", discriminatorType=DiscriminatorType.STRING)
    public abstract class Share {
        @Id
        @Column( name="ID", nullable=false )
        @GeneratedValue(generator="system-uuid")
        @GenericGenerator(name="system-uuid", strategy = "uuid")
        private String id;
    
        @ManyToOne
        @JoinColumn( name="SHARED_BY", nullable=false )
        private User sharedBy;
    
        @ManyToOne
        @JoinColumn( name="SHARED_WITH", nullable=false )
        private User sharedWith;
    
        @Column(name="SHARED_DATE", columnDefinition="TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP", nullable=false)
        @Temporal(TemporalType.TIMESTAMP)
        private Date sharedDate;        
        ...
    
    }
    

    And two normal classes..

    @Entity
    @DiscriminatorValue("DOCUMENT")
    public class SharedDocument extends Share { 
        @ManyToOne
        @JoinColumn( name="DOCUMENT_ID", nullable=true )
        private Document document;
        ....
    
    }
    
    @Entity
    @DiscriminatorValue("POST")
    public class SharedPost extends Share {
        @ManyToOne
        @JoinColumn( name="POST_ID", nullable=true )
        private Post post;
        ....
    
    }
    

    As for usage, use the concrete classes only as:

    @Test
    public void saveNewDocumentShare(){
        SharedDocument sharedDocument = new SharedDocument();
        sharedDocument.setDocument(document1);
        sharedDocument.setSharedBy(teacher1);
        sharedDocument.setSharedWith(teacher2);
        sharedDocument.setSharedDate(new Date());
    
        sharedDocument.setCreatedBy("1");
        sharedDocument.setCreatedDate(new Date());
        sharedDocument.setModifiedBy("1");
        sharedDocument.setModifiedDate(new Date());
    
    
        SharedDocument savedSharedDocument = dao.saveSharedDocument(sharedDocument);
    
        assertNotNull(savedSharedDocument);
        assertThat(savedSharedDocument.getId(),notNullValue());
    }
    
    @Test
    public void saveNewPostShare(){
        SharedPost sharedWikiPage = new SharedWikiPage();
        sharedPost.setPost(post1);
        sharedPost.setSharedBy(teacher1);
        sharedPost.setSharedWith(teacher2);
        sharedPost.setSharedDate(new Date());
    
        sharedPost.setCreatedBy("1");
        sharedPost.setCreatedDate(new Date());
        sharedPost.setModifiedBy("1");
        sharedPost.setModifiedDate(new Date());
    
    
        SharedPost savedSharedPost = dao.saveSharedPost(sharedPost);
    
        assertNotNull(savedSharedPost);
        assertThat(savedSharedPost.getId(),notNullValue());
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have seen multiple posts on this but I can't see any which answer my
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
Have a rather abstract question for you all. I'm looking at getting involved in
Have a look at this url: http://www.trirand.com/jqgridwiki/doku.php?id=wiki:jquery_ui_methods#column_chooser I have got the Column Chooser dialog
Have a photography site that I want to prevent image copying from. How can
Have the following scenario. I have a few form, which essentially have a few
Have an app that can use tts to read text messages. It can also
Have such a problem, hope you'll help me.. Can't find anywhere. Here is the
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.