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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:26:02+00:00 2026-05-11T19:26:02+00:00

I am dealing with legacy database. And I am writing unit test using pojos

  • 0

I am dealing with legacy database. And I am writing unit test using pojos & hibernate & HSQLDB. And I am getting the following error:

17:09:03,946 ERROR SchemaExport:349 - Attempt to define a second primary key in statement

Let’s say I have Post and Tag entities (and of course their tables posts and tags). And there is another table to define many-to-many between Post and Tag called post_tags.

Because post_tags contains some extra information about the relation, like active and deleted columns. I created another Entity called PostTag to handle this.

To describe my problem here are the pseudo classes:

@Entity
@Table(name="post_tags")
public class PostTag {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @ManyToOne
    private Post post;

    @ManyToOne
    private Tag tag;

    // setters and getters
}

@Entity
@Table(name = "tags")
public class Tag {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @ManyToMany(mappedBy = "tags",cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Set<Post> posts;

    // setters and getters
}

@Entity
@Table(name = "posts")
public class Post {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @ManyToMany
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    @JoinTable(name = "post_tags", joinColumns = { @JoinColumn(name = "post_id") }, inverseJoinColumns = { @JoinColumn(name = "tag_id") })
    private Set<Tag> tags;

    // setters and getters
}

When I look at the statement that generates the error it seems Hibernate is trying to make a composite key using PRIMARY KEY (post_id, tag_id), and also it tries to make post_id identity.

Can anyone help me to fix my problem?

UPDATE:

Because I am dealing with a legacy database this was just an example to demonstrate the problem. But I will try to translate the actual statement according to the example (PS: It is not ALTER TABLE it is a CREATE TABLE):

create table post_tags (id integer not null, post integer, tag integer, post_id integer generated by default as identity (start with 1), tag_id integer not null, primary key (post_id, tag_id)
  • 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-11T19:26:02+00:00Added an answer on May 11, 2026 at 7:26 pm

    The problem is the @Id in the class PostTag: It tells hibernate that you want a generated identity colum. At the same time, you’re saying “I want a composite key” with the mapping of the tags field in the class Posts.

    Which one do you want? Hibernate can’t tell.

    There is a very good example on page 304 in the book “Java Persistence with Hibernate”. It’s on Google books.

    If you don’t want the composite key, then you must treat the link table as real objects and use PostTag in the class Posts (i.e. Set<PostTag> postTags instead of Set<Tag> tags)

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

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think there is a class-level equivalent, but you… May 12, 2026 at 8:46 am
  • Editorial Team
    Editorial Team added an answer Processes are much more lightweight under UNIX variants. Windows processes… May 12, 2026 at 8:46 am
  • Editorial Team
    Editorial Team added an answer Because you added an extraneous / after search. Try this… May 12, 2026 at 8:46 am

Related Questions

Thank you all for your help. A number of you posted (as I should
I have the following FNH mapping fragment: HasManyToMany((c) => c.Plaintiffs) .LazyLoad() .WithTableName(invoicePlantiff) .WithChildKeyColumn(PersonReferenceID) .WithParentKeyColumn(invoiceID)
Warning - I am very new to NHibernate. I know this question seems simple
I want to know if it is possible to let compiler issue a warning/error

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.