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 7726825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:18:35+00:00 2026-06-01T05:18:35+00:00

Firstly, I am somewhat new with Hibernate. To get to know the technology I

  • 0

Firstly, I am somewhat new with Hibernate. To get to know the technology I am using it in a project. I am trying to map the following database:

Campaign
  campaignId(+)
  name

Promotion
  campaignId(+)
  discount(+)
  product
  message

I’ve indicated the primary key in both cases with a (+). The ‘campaignId’ in Promotion is a foreign key to Campaign to model the 1:m mapping (A Campaign has many Promotions). Using annotations I am stuck on how to do this.

I do not really want to add a promotionId in the Promotion table as it makes working with the data cumbersome. This of course, makes the bridging table a bit tricky. I also have problems working with a foreign key that is also part of the primary key.

Is a mapping for this possible at all?


Ok, I got it working. Sort of. Have to check if persistence actually work. I did the following:

@Entity
@Table(name = "CAMPAIGNS")
@Audited
public class CampaignEntity {
    private int campaignId;
    private String name;
    private List<PromotionEntity> promotions;

    public CampaignEntity(int campaignId, String name) {
        this.campaignId = campaignId;
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cmp_id")
    public int getCampaignId() {
        return campaignId;
    }

    public void setCampaignId(int campaignId) {
        this.campaignId = campaignId;
    }

    // Campaign name here... left out to save space

    @OneToMany
    @JoinColumn(name = "cmp_id")
    public List<PromotionEntity> getPromotions() {
        return promotions;
    }

    public void setPromotions(List<PromotionEntity> promotions) {
        this.promotions = promotions;
    }
}

Promotion is a vanilla mapping (not using embedded after all), with the fields: campaignId, discount, message. (It also does not have a @ManyToOne annotation.)

Does that make sense?

Lastly, and this will be first prize: as you can see I’m using Envers to audit the whole thing. The above creates a rather ugly “CampaignEntity_PromotionEntity_AUD” table. I understand that it is needed, but how can I rename it to CAMPAIGN_PROMOTION_AUD rather?

Thanks guys!


I got an answer on a lonely website deeply hidden away in far-corners of the Hibernate’s Jira error tracking website: https://hibernate.onjira.com/browse/HHH-3729.

The answer is to use @AuditJoinTable(name = “CAMPAIGN_PROMOTION_AUD”) of course.

  • 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-01T05:18:36+00:00Added an answer on June 1, 2026 at 5:18 am

    This is what I am now using. It works well and Hibernate handles the PKs of the Promotions for me. Thanks again.

    @Entity
    @Table(name = "CAMPAIGNS")
    @Audited
    public class CampaignEntity {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id", nullable = false)
        private Integer campaignId;
    
        @Column(name = "name", nullable = false, unique = true)
        private String campaignName;
    
         @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
         @JoinTable(name = "CAMPAIGN_PROMOTIONS", 
                    joinColumns = { @JoinColumn(name = "campaign_id") }, 
                    inverseJoinColumns = { @JoinColumn(name = "promotion_id") })
         private Set<PromotionEntity> promotions;
    
         ...
    }
    

    and then, PromotionEntity:

    @Entity
    @Table(name = "PROMOTIONS")
    @Audited
    public class PromotionEntity implements Comparable<PromotionEntity> {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
    
        @Column(name = "discount", nullable = false)
        private Integer discount;
    
        @Column(name = "message", nullable = false)
        private String message;
    
        ...
    }
    

    I also prefer annotating the fields rather than the getters as it is more compact and reads easier.

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

Sidebar

Related Questions

Firstly anyone else trying to get onto forum.hibernate.org. I have been trying for a
Firstly I'm very new to PHP, I'm trying trial and error method to get
Firstly, I know I should be using proper Threading techniques (Threadpool, BeginInvoke, etc.) to
Firstly to show you what I am trying to achieve in the view, a
Firstly some background,.. IVe started a new job as PHP developer with a company
Firstly, I know this [type of] question is frequently asked, so let me preface
Firstly, I am new to Python so please go easy on me... Secondly, I've
Firstly: I get the mvc and php of magento, but I'm not fully versed
Firstly, I know there is a lot of íssues about creating sessionFactory bean, but
Firstly, thanks in advance. I am trying to make a drill down style report

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.