Note that there are some explanatory texts on larger screens.

plurals
  1. POManyToMany in Hibernate without composite keys
    primarykey
    data
    text
    <p>I am dealing with legacy database. And I am writing unit test using pojos &amp; hibernate &amp; <strong>HSQLDB</strong>. And I am getting the following error:</p> <pre>17:09:03,946 ERROR SchemaExport:349 - Attempt to define a second primary key in statement</pre> <p>Let's say I have <code>Post</code> and <code>Tag</code> entities (and of course their tables <code>posts</code> and <code>tags</code>). And there is another table to define many-to-many between <code>Post</code> and <code>Tag</code> called <code>post_tags</code>.</p> <p>Because <code>post_tags</code> contains some extra information about the relation, like <code>active</code> and <code>deleted</code> columns. I created another Entity called <code>PostTag</code> to handle this.</p> <p>To describe my problem here are the pseudo classes:</p> <pre><code>@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&lt;Post&gt; 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&lt;Tag&gt; tags; // setters and getters } </code></pre> <p>When I look at the statement that generates the error it seems Hibernate is trying to make a composite key using <code>PRIMARY KEY (post_id, tag_id)</code>, and also it tries to make <code>post_id</code> <code>identity</code>.</p> <p>Can anyone help me to fix my problem?</p> <p>UPDATE:</p> <p>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 <code>ALTER TABLE</code> it is a <code>CREATE TABLE</code>):</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload