หน้าเว็บ

วันจันทร์ที่ 26 พฤศจิกายน พ.ศ. 2555

Map [Many to One and One to Many] : JPA

Table UserForum
...
...
...

@Table(name = "USER_FORUM")
@Entity
public class UserForum implements Serializable {

    @Id
    @TableGenerator(name = "UserForum_GEN", table = "WEBBOARD_SEQ", pkColumnName = "NAME", valueColumnName = "VALUE", pkColumnValue = "USER_FORUM")
    @GeneratedValue(generator = "UserForum_GEN", strategy = GenerationType.TABLE)
    @Column(name = "USER_FORUM_ID")
    private Integer id;
    @Version
    private Integer version;
    //information
    private String userName;
    private String password;
    private String fullName;
    private String email;
    private String image;
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date joinDate;
    //join
    @ManyToMany
    @JoinTable(name = "ANSWER",
    joinColumns = {
        @JoinColumn(name = "USER_FORUM", referencedColumnName = "USER_FORUM_ID")},
    inverseJoinColumns = {
        @JoinColumn(name = "CATEGORY", referencedColumnName = "CATEGORY_ID")})
    private List<Category> categories;
    @OneToMany(mappedBy = "owner")
    private List<Post> posts;
    @OneToMany(mappedBy = "owner")
    private List<Comment> comments;

    ...
    ...
    ...
}
Table CATEGORY
...
...
...

@Table(name = "CATEGORY")
@Entity
public class Category implements Serializable {

    @Id
    @TableGenerator(name = "Category_GEN", table = "WEBBOARD_SEQ", pkColumnName = "NAME", valueColumnName = "VALUE", pkColumnValue = "CATEGORY")
    @GeneratedValue(generator = "Category_GEN", strategy = GenerationType.TABLE)
    @Column(name = "CATEGORY_ID")
    private Integer id;
    @Version
    private Integer version;
    //information
    @Column(name = "CATEGORY_NAME")
    private String categoryName;
    //join
    @ManyToMany(mappedBy = "categories")
    private List<UserForum> answers;
    @OneToMany(mappedBy = "category")
    private List<Post> posts;

    ...
    ...
    ...
}
Table POST
..
..
..

@Table(name = "POST")
@Entity
public class Post implements Serializable {

    @Id
    @TableGenerator(name = "Post_GEN", table = "WEBBOARD_SEQ", pkColumnName = "NAME", valueColumnName = "VALUE", pkColumnValue = "POST")
    @GeneratedValue(generator = "Post_GEN", strategy = GenerationType.TABLE)
    @Column(name = "POST_ID")
    private Integer id;
    @Version
    private Integer version;
    //information
    private String title;
    private String content;
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date postDate;
    @ManyToOne
    private Category category;
    @OneToMany(mappedBy = "post")
    private List<Comment> comments;
    @ManyToOne
    private UserForum owner;
    private boolean hiddenPost;
    private boolean closePost;

    ...
    ...
    ...
}
Table COMMENT
...
...
...

@Table(name = "COMMENT")
@Entity
public class Comment implements Serializable {

    @Id
    @TableGenerator(name = "Comment_GEN", table = "WEBBOARD_SEQ", pkColumnName = "NAME", valueColumnName = "VALUE", pkColumnValue = "COMMENT")
    @GeneratedValue(generator = "Comment_GEN", strategy = GenerationType.TABLE)
    @Column(name = "COMMENT_ID")
    private Integer id;
    @Version
    private Integer version;
    //information
    private String content;
    @ManyToOne
    private Post post;
    @ManyToOne
    private UserForum owner;
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date commentDate;
   
    ...
    ...
    ...
}