开发者

How hard is to make shoppping cart with HIbernate annotaion

I am learning spring mvc and hibernate.

i have just finished simple site with CRUD operation usin hibernate annotat开发者_高级运维ions 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜