Add JPA entities
This commit is contained in:
56
src/main/java/com/seanstarkey/notesvault/entity/Note.java
Normal file
56
src/main/java/com/seanstarkey/notesvault/entity/Note.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Note.java
|
||||
*
|
||||
* JPA entity representing a user-authored note.
|
||||
*/
|
||||
package com.seanstarkey.notesvault.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.UuidGenerator;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.annotation.LastModifiedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "notes")
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class Note {
|
||||
|
||||
@Id
|
||||
@UuidGenerator
|
||||
@Column(updatable = false, nullable = false)
|
||||
private UUID id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "owner_id", nullable = false)
|
||||
private User owner;
|
||||
|
||||
@Column(nullable = false, length = 255)
|
||||
private String title;
|
||||
|
||||
@Column(nullable = false, columnDefinition = "TEXT")
|
||||
private String content;
|
||||
|
||||
/** Timestamp set once when the record is first persisted. */
|
||||
@CreatedDate
|
||||
@Column(nullable = false, updatable = false)
|
||||
private Instant createdAt;
|
||||
|
||||
@LastModifiedDate
|
||||
@Column(nullable = false)
|
||||
private Instant updatedAt;
|
||||
|
||||
@OneToMany(mappedBy = "note", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
private List<NoteShare> shares = new ArrayList<>();
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* NoteShare.java
|
||||
*
|
||||
* JPA entity representing a read-only share grant between a note and a user.
|
||||
*/
|
||||
package com.seanstarkey.notesvault.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.UuidGenerator;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(
|
||||
name = "note_shares",
|
||||
uniqueConstraints = @UniqueConstraint(
|
||||
name = "uq_note_shares_note_user",
|
||||
columnNames = {"note_id", "shared_with_user_id"}
|
||||
)
|
||||
)
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class NoteShare {
|
||||
|
||||
@Id
|
||||
@UuidGenerator
|
||||
@Column(updatable = false, nullable = false)
|
||||
private UUID id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "note_id", nullable = false)
|
||||
private Note note;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||
@JoinColumn(name = "shared_with_user_id", nullable = false)
|
||||
private User sharedWith;
|
||||
|
||||
@CreatedDate
|
||||
@Column(nullable = false, updatable = false)
|
||||
private Instant createdAt;
|
||||
}
|
||||
41
src/main/java/com/seanstarkey/notesvault/entity/User.java
Normal file
41
src/main/java/com/seanstarkey/notesvault/entity/User.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* User.java
|
||||
*
|
||||
* JPA entity representing an application user.
|
||||
*/
|
||||
package com.seanstarkey.notesvault.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.UuidGenerator;
|
||||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
@EntityListeners(AuditingEntityListener.class)
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@UuidGenerator
|
||||
@Column(updatable = false, nullable = false)
|
||||
private UUID id;
|
||||
|
||||
@Column(nullable = false, unique = true, length = 100)
|
||||
private String username;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String passwordHash;
|
||||
|
||||
@CreatedDate
|
||||
@Column(nullable = false, updatable = false)
|
||||
private Instant createdAt;
|
||||
}
|
||||
Reference in New Issue
Block a user