не создаётся таблица в бд
В базе данных создаются все таблицы из этих Java классов кроме User, даже таблица user_role создаётся которая находится внутри класса User, расскажите в чём ошибка
@Data
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(unique = true)
private String username;
@Column(nullable = false)
private String lastname;
@Column(unique = true)
private String email;
@Column(columnDefinition = "text")
private String bio;
@Column(length = 3000)
private String password;
@ElementCollection(targetClass = ERole.class)
@CollectionTable(name = "user_role",
joinColumns = @JoinColumn(name = "user_id"))
private Set<ERole> roles = new HashSet<>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user", orphanRemoval = true)
private List<Post> posts = new ArrayList<>();
@JsonFormat(pattern = "yyyy-mm-dd HH:mm:ss")
@Column(updatable = false)
private LocalDateTime createdDate;
@Transient
private Collection<? extends GrantedAuthority> autorities;
public User() { }
@PrePersist
protected void onCreate() {
this.createdDate = LocalDateTime.now();
}
}
public enum ERole {
ROLE_USER,
ROLE_ADMIN
}
@Data
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Post post;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private Long userId;
@Column(columnDefinition = "text", nullable = false)
private String messege;
@Column(updatable = false)
private LocalDateTime createdDate;
@PrePersist
protected void onCreate() {
this.createdDate = LocalDateTime.now();
}
}
@Data
@Entity
public class ImageModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Lob
@Column(columnDefinition = "BYTEA")
private byte[] imageBytes;
@JsonIgnore
private Long userId;
@JsonIgnore
private Long postId;
}
@Data
@Entity
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String caption;
private String location;
private Integer likes;
@Column
@ElementCollection(targetClass = String.class)
private Set<String> likedUsers = new HashSet<>();
@ManyToOne(fetch = FetchType.LAZY)
private User user;
@OneToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, mappedBy = "post", orphanRemoval = true)
private List<Comment> posts = new ArrayList<>();
@Column(updatable = false)
private LocalDateTime createdDate;
@PrePersist
protected void onCreate() {
this.createdDate = LocalDateTime.now();
}
}
@SpringBootApplication
public class InstazooApplication {
public static void main(String[] args) {
SpringApplication.run(InstazooApplication.class, args);
}
}
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:postgresql://localhost:5432/Sprngbt?useSSL=false
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Ответы (1 шт):
Автор решения: kn0W W
→ Ссылка
Проблема решилась, таблица создалась. В Postgresql user
- служебное слово, и таблица с таким именем не может быть создана.