Add domain for users, posts, comments
This commit is contained in:
parent
f9caf17a18
commit
b1dfb6322e
@ -1,13 +1,21 @@
|
|||||||
package com.cubetiqs.demo;
|
package com.cubetiqs.demo;
|
||||||
|
|
||||||
|
import com.cubetiqs.demo.domain.UserEntity;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class DemoApplication {
|
public class DemoApplication implements CommandLineRunner {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(DemoApplication.class, args);
|
SpringApplication.run(DemoApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String... args) throws Exception {
|
||||||
|
UserEntity userEntity = new UserEntity();
|
||||||
|
userEntity.setEmail("a@gm.com");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
47
src/main/java/com/cubetiqs/demo/domain/BaseEntity.java
Normal file
47
src/main/java/com/cubetiqs/demo/domain/BaseEntity.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Persistable;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@MappedSuperclass
|
||||||
|
public class BaseEntity<ID extends Serializable> implements Serializable, Persistable<ID> {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private ID id;
|
||||||
|
|
||||||
|
@Column(name = "created_date")
|
||||||
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
|
private Date createdDate;
|
||||||
|
|
||||||
|
public void setId(ID id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ID getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isNew() {
|
||||||
|
return id == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreatedDate() {
|
||||||
|
return createdDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatedDate(Date createdDate) {
|
||||||
|
this.createdDate = createdDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PrePersist
|
||||||
|
public void beforeSave() {
|
||||||
|
if (createdDate == null) {
|
||||||
|
createdDate = new Date();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
src/main/java/com/cubetiqs/demo/domain/CommentEntity.java
Normal file
27
src/main/java/com/cubetiqs/demo/domain/CommentEntity.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "comments")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class CommentEntity extends BaseEntity<Long> {
|
||||||
|
@Column(columnDefinition = "TEXT")
|
||||||
|
private String contents;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
|
@JoinColumn(name = "post_id")
|
||||||
|
private PostEntity post;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
|
@JoinColumn(name = "user_id")
|
||||||
|
private UserEntity user;
|
||||||
|
}
|
29
src/main/java/com/cubetiqs/demo/domain/PostEntity.java
Normal file
29
src/main/java/com/cubetiqs/demo/domain/PostEntity.java
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "posts")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class PostEntity extends BaseEntity<Long> {
|
||||||
|
@Column
|
||||||
|
private String title;
|
||||||
|
@Column(columnDefinition = "TEXT")
|
||||||
|
private String contents;
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.REFRESH, CascadeType.DETACH})
|
||||||
|
@JoinColumn(name = "user_id")
|
||||||
|
private UserEntity user;
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "post", fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval = true)
|
||||||
|
private Collection<CommentEntity> comments;
|
||||||
|
}
|
21
src/main/java/com/cubetiqs/demo/domain/UserEntity.java
Normal file
21
src/main/java/com/cubetiqs/demo/domain/UserEntity.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package com.cubetiqs.demo.domain;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "users")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class UserEntity extends BaseEntity<Long> {
|
||||||
|
@Column(name = "email", length = 100, unique = true, nullable = false)
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@Column(length = 100)
|
||||||
|
private String password;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user