Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA ManyToMany Join Table has all attributes as PK
    primarykey
    data
    text
    <p>I'm using Hibernate 3.3.1 and am following along in modelling <a href="http://www.javaworld.com/javaworld/jw-01-2008/images/datamodel.gif" rel="nofollow noreferrer">this sample table structure</a>, but I'm having trouble creating a join table with extra attributes. </p> <p>It's the many-to-many relationship between the <code>Order</code> and <code>Product</code> table. The join table is the <code>Order Detail</code> table. I followed the approach mentioned <a href="http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Additional_columns_in_join_table." rel="nofollow noreferrer">here</a>.</p> <p>Now I have the entities</p> <pre><code>@Entity @Table(name = "Orders") public class Order { @OneToMany(mappedBy="order") private List&lt;OrderDetail&gt; orderItems; } </code></pre> <p>and</p> <pre><code>@Entity @Table(name="PRODUCTS") public class Product { @OneToMany(mappedBy="product") private List&lt;OrderDetail&gt; orderItems; } </code></pre> <p>and</p> <pre><code>@Entity @IdClass(OrderDetail.class) @Table(name = "ORDER_DETAIL") public class OrderDetail implements Serializable { @Id @Column(name="ORDER_ID") private Long orderId; @Id @Column(name="PRODUCT_ID") private Long productId; @Column(name = "PRICE") private double price; @Column(name = "LAST_UPDATED_TIME") private Date lastUpdatedTime; @ManyToOne @JoinColumn(name = "ORDER_ID") private Order order; @ManyToOne @JoinColumn(name = "PRODUCT_ID") private Product product; } </code></pre> <p>and</p> <pre><code>public class OrderDetailId implements Serializable { private Long orderId; private Long productId; } </code></pre> <p>I used Apache Derby to do the test, but I'm having trouble with the generated table structure.</p> <pre><code>CREATE TABLE ORDER_DETAIL ( PRODUCT_ID BIGINT NOT NULL, ORDER_ID BIGINT NOT NULL, LAST_UPDATED_TIME TIMESTAMP NOT NULL, PRICE DOUBLE NOT NULL ); CREATE INDEX SQL120323142938020 ON ORDER_DETAIL (PRODUCT_ID ASC); CREATE UNIQUE INDEX SQL120323142937810 ON ORDER_DETAIL (PRODUCT_ID ASC, ORDER_ID ASC, LAST_UPDATED_TIME ASC, PRICE ASC); ALTER TABLE ORDER_DETAIL ADD CONSTRAINT SQL120323142937810 PRIMARY KEY (PRODUCT_ID, ORDER_ID, LAST_UPDATED_TIME, PRICE); ALTER TABLE ORDER_DETAIL ADD CONSTRAINT FK4A94AA82CC6D989A FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS (PROD_ID); </code></pre> <p>It seems that it has created all of my columns as the primary key. Why is this so?</p>
    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