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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:09:27+00:00 2026-05-31T23:09:27+00:00

I’m using Hibernate 3.3.1 and am following along in modelling this sample table structure

  • 0

I’m using Hibernate 3.3.1 and am following along in modelling this sample table structure, but I’m having trouble creating a join table with extra attributes.

It’s the many-to-many relationship between the Order and Product table. The join table is the Order Detail table. I followed the approach mentioned here.

Now I have the entities

@Entity
@Table(name = "Orders")
public class Order {
    @OneToMany(mappedBy="order")
    private List<OrderDetail> orderItems;
}

and

@Entity
@Table(name="PRODUCTS")
public class Product {
    @OneToMany(mappedBy="product")
    private List<OrderDetail> orderItems;
}

and

@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;
}

and

public class OrderDetailId implements Serializable {
    private Long orderId;
    private Long productId;
}

I used Apache Derby to do the test, but I’m having trouble with the generated table structure.

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);

It seems that it has created all of my columns as the primary key. Why is this so?

  • 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-31T23:09:29+00:00Added an answer on May 31, 2026 at 11:09 pm

    You use class of your entity as an argument to IdClass. That is not correct. Class of Id should be used. Additionally separate fields for id in join entity are not needed.

    Go for something like code below. I cannot guarantee that it works in such a old version of Hibernate, but works for sure in never ones. Worth of trying anyway. It would not hurt to update to at least 3.5.X version (or rather even fresher one) if you want to use JPA 2.0 features. Constructors/equals etc. are stripped away to save space.

    @Entity
    @Table(name = "Orders")
    public class Order {
        @Id Long id;
        @OneToMany(mappedBy="order")
        private List<OrderDetail> orderItems;
    }
    
    @Entity
    @Table(name="PRODUCTS")
    public class Product {
        @Id Long id;
        @OneToMany(mappedBy="product")
        private List<OrderDetail> orderItems;
    }
    
    @Entity
    @IdClass(OrderDetailId.class)
    @Table(name = "ORDER_DETAIL")
    public class OrderDetail implements Serializable {
        @Id @ManyToOne @JoinColumn(name = "ORDER_ID")
        private Order order;
    
        @Id @ManyToOne @JoinColumn(name = "PRODUCT_ID")
        private Product product;
    
        @Column(name = "PRICE") private double price;
        //Maybe you also want to use @TemporalType here
        @Column(name = "LAST_UPDATED_TIME") private Date lastUpdatedTime;
    }
    
    public class OrderDetailId implements Serializable {
        private Long order;
        private Long product;
    }
    

    UPDATE 15/08/2017
    In JPA 2.1 and above you don’t need to add a class for the composite Id and you can do it like this:

    @Entity
    @Table(name = "ORDER_DETAIL")
    public class OrderDetail implements Serializable {
        @Id @ManyToOne @JoinColumn(name = "ORDER_ID")
        private Order order;
    
        @Id @ManyToOne @JoinColumn(name = "PRODUCT_ID")
        private Product product;
    
        @Column(name = "PRICE") private double price;
        //Maybe you also want to use @TemporalType here
        @Column(name = "LAST_UPDATED_TIME") private Date lastUpdatedTime;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.