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

  • Home
  • SEARCH
  • 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 9104769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:56:29+00:00 2026-06-17T01:56:29+00:00

So, I have a model with three entities: Document , Template and Origin .

  • 0

So, I have a model with three entities: Document, Template and Origin.

The main source of my problem is that I need the Document‘s number to be unique for the Template‘s Source, but as the origin is property of Template (not of Document), I can’t create an unique constraint for it in a straight-forward manner.

Just to clarify, the model in code goes something like this:

Origin:

@Entity
public class Origin {
    @Id private Long id;
    // ... some other fields
}

Template:

@Entity
public class Template {
    @Id private Long id;
    @Column private Origin origin;
    // ... some other fields
}

Document:

@Entity
public class Document {
    @Id private Long id;
    @Column Template template;
    @Column Long number;
    // ... some other fields
}

So, Origins have several Templates associated, and you can create Documents for an Origin using a Template.

As I said, the source of my problem is that a document’s number must be unique for each source.
The only way I could thought for that to work is to use an additional field for the originId, and try to keep it updated.

So, I was trying to do something like this:

@Entity
@Table("documents",
     uniqueConstraints = @UniqueConstraint(columnNames = {"number", "originId"}))
public class Document {
    @Id private Long id;
    @Column Template template;
    @Column Long number;
    // ... some other fields

    // added extra field to use in the constraint:
    @Column Long originId;

    @PrePersist
    @PreUpdate
    private void updateConstraintValue() {
        this.originId = getTemplate().getId();
    }
}

Note: getters, setters and other boilerplate ommited for brevity

but that won’t always work with JPA, since the Template object isn’t required to be a managed entity at the time of the persist/update event (it’s only required to have an id set for the relationship to work).

Then I’m now thinking this may not be the best approach. So, my question is:

  • What other options do I have? Does the JPA (or Hibernate) API have a better way to deal with this? (I’m using JPA 2 and Hibernate 3.6.10, btw)
  • Should I just trust the client code to pass the Template object with an Origin object with the proper id (even though it could be different from what’s in the database)?
  • Would I be better mapping the Origin itself in Document, and trusting the client code to keep it synced with the Template?
  • 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-06-17T01:56:30+00:00Added an answer on June 17, 2026 at 1:56 am

    I might be missing something, but can you get rid of the origin ID altogether and simply use a generated ID for the Document rather than creating one manually? You already have an ID column there. It looks like all you need to do is specify that the ID be generated.

    @Entity
    @Table("documents" )
    public class Document {
        @Id 
        @GeneratedValue( strategy = GenerationType.IDENTITY )
        private Long id;
    
        @Column Template template;
    
        @Column Long number;
        // ... some other fields
    
    }
    

    Note: I’ve added the GeneratedValue annotation and removed the origin ID

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have three models that are coming together to create one view model and
I'm using Doctrine 2 ORM and I have this problem. I have three Entities,
I have a domain model where many of the entities have content that needs
I have a data model setup with three entities, Course, Student, TestScores. They are
I have a data model with three entities (Purchase, Items, Image) the purchase is
I have a Core Data model with three entities: Notification , Group and Customer
I have a Core Data model which has three entities: A, B, and C.
Lets say I have a entity model with three entities Item, ItemType and ItemPart.
I have a Workspace and Document entities, with the idea that a workspace can

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.