我使用的是spring数据jpa。目标是从我的条目表中仅获取列";标题";以及";creation_date";。显然,这是可能的东西,称为界面投影。所以我创建了接口,并将返回类型更改为接口,接口会被返回,但当我调用get方法/return it时,值只为null。
这是我的实体类:
import java.time.Instant;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.sun.istack.NotNull;
@Entity
public class Entry {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(columnDefinition="timestamptz")
Instant creationDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="userId")
@NotNull
private User user;
@Column(columnDefinition = "varchar(256)")
String title;
@Column(columnDefinition = "text")
String text;
public Entry() {
creationDate = Instant.now();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Instant getCreationDate() {
return creationDate;
}
public void setCreationDate(Instant creationDate) {
this.creationDate = creationDate;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
这是我的界面:
import java.time.Instant;
public interface EntryTitleDateAndUserIdDTO {
public String getTitle();
public Instant getCreationDate();
//User getUser();
}
这是我的存储库:
public interface EntryRepository extends JpaRepository<Entry, Long>{
@Query("SELECT e.title, e.creationDate FROM Entry e WHERE e.user = ?1")
ArrayList<EntryTitleDateAndUserIdDTO> getAllEntriesOfUser(User user);
@Query("SELECT e FROM Entry e WHERE e.id = ?1 AND e.user = ?2")
Entry getEntryOfUser(Long entryId, Long userId );
@Query("DELETE FROM Entry e WHERE e.id = ?1 AND e.user = ?2")
Entry deleteEntry(Long entryId, Long userId );
}
这是我的控制器:
@RestController
@RequestMapping("/User")
public class UserController {
@Autowired
UserRepository userRepo;
@Autowired
EntryRepository entryRepo;
@GetMapping("/{userId}/Entries")
private ArrayList<EntryTitleDateAndUserIdDTO> getAllEntries(@PathVariable Long userId) {
User u = userRepo.findById(userId).get();
ArrayList<EntryTitleDateAndUserIdDTO> e = entryRepo.getAllEntriesOfUser(u);
return e;
}
这是浏览器中的输出:
[{"title":null,"creationDate":null}]
这是数据库中的安全内容:Postgresql条目表
这是Spring生成的查询:
select
user0_.id as id1_1_0_,
user0_.email as email2_1_0_,
user0_.join_date as join_dat3_1_0_,
user0_.password as password4_1_0_,
user0_.username as username5_1_0_
from
public.user user0_
where
user0_.id=?
2021-11-04 18:13:35.417 TRACE 5104 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1]
2021-11-04 18:13:35.447 DEBUG 5104 --- [nio-8080-exec-1] org.hibernate.SQL :
select
entry0_.title as col_0_0_,
entry0_.creation_date as col_1_0_
from
public.entry entry0_
where
entry0_.user_id=?
2021-11-04 18:13:35.448 TRACE 5104 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [1]
如果有人能帮忙,我们将不胜感激,提前表示感谢。
尝试对所选字段进行混叠,以完全匹配投影接口方法getTitle
和getCreationDate
@Query("SELECT e.title as title, e.creationDate as creationDate FROM Entry e WHERE e.user = ?1")
由于您使用的是Spring Data JPA,因此可以使用派生查询,而不是注释所有查询。这也将消除对别名的需要。
public interface EntryRepository extends JpaRepository<Entry, Long>{
ArrayList<EntryTitleDateAndUserIdDTO> findByUser(User user);
Entry findByIdAndUserId(Long id, Long userId );
void deleteByIdAndUserId(Long id, Long userId );
}