我有一个附件类:
@Entity
@Table(name = "ATTACHMENT")
public class Attachment implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_gen")
@SequenceGenerator(name = "seq_gen", sequenceName = "attachment_seq", allocationSize = 1)
@Column(name = "ATTACHMENT_ID", nullable = false)
private Long id;
@ManyToOne
@JoinColumn(name = "CREATED_BY", nullable = false)
private User createdBy;
@Column(name = "FILE_NAME", nullable = false)
private String fileName;
@Column(name = "CONTENT_TYPE", length = 100, nullable = false)
private String contentType;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATE_DATE", nullable = false)
private Date createDate;
我想用一个额外的字段扩展这个类(也在这个表上):
@Entity
public class AttachmentWithContent extends Attachment {
@Column(name = "FILE_CONTENT")
@Lob
private byte[] fileContent;
然后我想使用JpaRepository查询:
List<AttachmentWithContent> findByIdIn(List<Long> attachmentsIds);
但是出现了错误
'Attachment' domain type or valid projection interface expected here.
我需要能够一次查询Attachment类,再次查询AttachmentWithContent。
我已尝试使用@Inheritance和@MappedSuperClass,但它无法正常工作。
答案 0 :(得分:1)
我认为该消息只是IDE的警告(我假设您使用IntelliJ),因为它通过方法名称和存储库类型推断出您应该返回该查询的Attachment对象列表。 我尝试使用示例弹簧启动项目描述的实体模型,它适用于我。 但是,如果您有这样的AttachmentRepository:
public interface AttachmentRepository implements JpaRepository<Attachment,Long>{
List<AttachmentWithContent> findByIdIn(List<Long> attachmentsIds);
}
这里的 findByIdIn 方法只会找到AttachmentWithContent实体;如果您输入属于简单附件的ID,则会产生空结果。 如果您将返回类型更改为附件(如警告所示),它将返回包含或不包含内容的所有附件:
public interface AttachmentRepository implements JpaRepository<Attachment,Long>{
List<Attachment> findByIdIn(List<Long> attachmentsIds);
}