dawidklos
dawidklos

Reputation: 922

Hibernate MySQL

Assume I have POJOs like this and i want to insert articles into MySQL database.

@Entity
@Table(name = "article")
public class Article{
    private String author;
    private String body;
    private int commentCount;
    private List<EntryComment> comments;


@Entity
@Table(name = "comments")
public class Comment{
    private String author;
    private String body;
    private int voteCount;

I want to put comments in another table without deleting field comments from `Article. Is it possible to put Comments in separate table using hibernate annotations?

Upvotes: 0

Views: 90

Answers (1)

Matthias Steinbauer
Matthias Steinbauer

Reputation: 1776

Of course it is. I suggest that you use annotations on all elements you have @Column for the strings and int elements however for the comments just look at the code down there:

Article Class:

@Entity
@Table(name = "article")
public class Article{
    private String author;
    private String body;
    private int commentCount;

    @OneToMany(mappedBy = "article")
    @LazyCollection(LazyCollectionOption.TRUE)
    private List<EntryComment> comments;

Comments Class:

@Entity
@Table(name = "comments")
public class Comment{

    @ManyToOne(fetch = FetchType.LAZY)
    private Article article;

    private String author;
    private String body;
    private int voteCount;

Upvotes: 2

Related Questions