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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:20:24+00:00 2026-05-20T11:20:24+00:00

I am learning spring mvc and hibernate. i have just finished simple site with

  • 0

I am learning spring mvc and hibernate.

i have just finished simple site with CRUD operation usin hibernate annotations and mysql. Now i have to build shopping cart site and i don’t know how should i proceed.

I am confused with @onetomany , @Embeddable @id annnotation and that too over many classes. Did anyone accomplished it with hibernate annotations

or i should chose plain JDBC , i don’t know how to start with it

  • 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-20T11:20:25+00:00Added an answer on May 20, 2026 at 11:20 am

    How hard something is, is very subjective but I hope I can help 🙂 I’ll try and answer all your questions…

    1) It sounds like you already understand more than the basics and have connected Spring, Java, Hibernate and MySQL together.

    The @OneToMany annotation might not be necessary but it depends on how you are modelling the relationships between your concepts. @OneToMany is used at the property level and can be bidirectional, unidirectional or unidirectional with a join table. This is an important distinction which can be researched further but for now let’s just go with the simplest variation (in my mind) – bidirectional. It is used to describe the relationship between Entities (in this case POJOs).

    The example relationship being modelled is “one stack overflow question has many comments”, the inverse of that is “many comments belong to one stack overflow question” and hence that is annotated by @ManyToOne. Note that only side “owns” the relationship and the mappedBy attribute must be used to specify the member in the class that owns the relationship.

    @Entity
    @Table(name = "question")
    public class StackOverflowQuestion {
        @Column(name = "the_question")
        private String question_text;
        public String getQuestionText() { return question_text; }
    
        @OneToMany(mappedBy="comment_text")
        private List<Comment> comments = new Vector<Comment>();
        public List<Comment> getComments() { return comments; }
    }
    
    @Entity
    @Table(name = "comment")
    public class Comment {
        @Column(name = "the_comment")
        private String comment_text;
        public String getComment() { return comment_text; }
    
        @ManyToOne
        private StackOverflowQuestion question;
        public Job getStackOverflowQuestion () { return question; }
    }
    

    This would translate into the database tables question and comment.

    question has 1 column – the_question

    comment has 2 columns – the_comment and question_question_text

    The second column is a join column created automatically by hibernate to maintain the mapping between questions and comments and by default is given the name which is:

    “the concatenation of the name of the
    relationship in the owner side, _
    (underscore), and the name of the
    primary key column in the owned side.”

    from here

    So if there was an @Id annotated column on the question table, giving each question a unique numeric (for example) identifier, then the join column would be created with the name question_id.

    You could add the following to the StackOverflowQuestion class:

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id", updatable = false, nullable = false)
        private Long id;
        public Long getId() { ... }
    

    In either case, each comment database row has some extra data persisted with it (the question_id) which makes retrieving the “comments for a given question” possible. Note you could override the automatic name generation with the @JoinColumn annotation.

    You will also need to look into what happens when instances of these classes are persisted by hibernate, specifically how the join column is updated. For example, once a Question is created and given an Id (i.e. it is persisted/saved) then each Comment on that question needs the question_id setting on the Comment instance before it is persisted.

    2) You would use @Embeddable (and its counterpart @Embedded) if the relationship between data does not warrant a separate table. There is a good example here of when that might be needed.

    3) Plain JDBC will require you to write more SQL yourself, as none of the above examples have required any SQL for CRUD. There are many sites that explain pros and cons of plain JDBC vs hibernate and there are situations where plain JDBC might be the better solution.

    I would continue with hibernate as it can result in a less complex solution, almost no hand-written SQL and a solid link between Java objects and the database without duplication of logic on the database and Java class. (Although some might argue that these are disadvantages!). Personally, I like hibernate annotations as it allows me to create a set of POJOs in a relationship from which my database is automatically created.

    I hope this helps. It’s late, I’m tired and I haven’t had a chance to test the example code 🙂

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

Sidebar

Related Questions

I am learning spring-mvc and I am doing a simple crud. I have a
I am learning Spring 3.x MVC. I now have a regular web page setup
I have been learning java spring hibernate MVC for 3 months and got pretty
I have a simple ASP.NET MVC 3 dummy app (just learning MVC coming from
I was learning how to make simple form validation using this tutorial: http://www.raistudies.com/spring/spring-mvc/form-validation-spring-mvc-3-hibernate-validator-jsr-303/ The
I have just started learning Spring MVC 3 and I have problem right at
Learning Spring (3.1.0) and Hibernate (4.1.1). Just wondering what most developers do when handling
I am learning MVC with Razor and have a simple question (I think). I
I have just started learning asp.net mvc and one reason of the primary reasons
Learning Spring MVC and Hibernate. import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity

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.