Код: Выделить всё
@Entity
@Table(name = "articles")
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "title")
private String title;
@Column(name = "content", length = 65535)
private String content;
@Column(name = "created_at")
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;
@Column(name = "updated_at")
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;
@ManyToOne
@JoinColumn(name = "author_id", referencedColumnName = "id", nullable = false)
private User author;
// ...
public interface ArticleSummary {
Long getId();
String getTitle();
Date getCreatedAt();
Date getUpdatedAt();
User getAuthor();
}
}
Код: Выделить всё
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "username", nullable = false, unique = true)
private String username;
@Column(name = "password", nullable = false)
private String password;
@OneToMany(mappedBy = "userId", cascade = CascadeType.ALL, orphanRemoval = true)
private List roles;
@OneToMany(mappedBy = "author", cascade = CascadeType.REMOVE)
private List articles;
// ...
}
Код: Выделить всё
@Repository
public interface ArticleRepository extends CrudRepository {
Collection findAll();
Collection findByAuthor_Username(String username);
@Query(value = "select a.id, a.title, a.created_at, a.updated_at, u.* from articles a left join users u on a.author_id=u.id", nativeQuery = true)
Page findAll(Pageable pageable);
}
Код: Выделить всё
Request processing failed: java.lang.NullPointerException: Cannot invoke "arkadisahakyan.authenticationwithspring.model.User.getId()" because "author" is null
Подробнее здесь: https://stackoverflow.com/questions/788 ... -annotatio