package relations.one2many;

import jakarta.persistence.*;
import java.time.LocalDateTime;

/**
 * ============================================================================
 * 关系情形 ①：一对多 1:N —— 多端（Comment）
 * ============================================================================
 *
 * 【关系 / Relationship】
 * EN — The "many" side owns the relationship: it holds the foreign key column post_id.
 *      @ManyToOne is the owning side, so it is the one that writes to the database.
 * 中 — “多”的一侧是拥有端：外键列 post_id 在这里。@ManyToOne 是拥有端，只有它才写库。
 *
 * 【常见错误 / Common mistake】
 * EN — Adding @JoinColumn on BOTH sides creates two foreign keys (or a join table) and the
 *      data stops matching. Write @JoinColumn only on the @ManyToOne side.
 * 中 — 两端都写 @JoinColumn 会生成两个外键（或一张中间表），数据对不上。
 *      只在 @ManyToOne 这一端写 @JoinColumn。
 */
@Entity
public class Comment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String content;
    private LocalDateTime createdAt;

    @ManyToOne(optional = false)
    @JoinColumn(name = "post_id", nullable = false)
    private Post post;

    public Comment() { }
    public Comment(String content, LocalDateTime createdAt) {
        this.content = content;
        this.createdAt = createdAt;
    }

    public Long getId() { return id; }
    public String getContent() { return content; }
    public void setContent(String content) { this.content = content; }
    public LocalDateTime getCreatedAt() { return createdAt; }
    public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
    public Post getPost() { return post; }
    public void setPost(Post post) { this.post = post; }
}
