Spring data elasticsearch注解@Filed和@ManytoMany同时使用,搜索结果取到属性为null

发布于 2022-09-04 02:40:41 字数 4445 浏览 11 评论 0

搜索结果返回值带@manytomany注解的属性(如:role、tags、users)都为null,代码如下:

package cn._1lift.edianti.domain.information;
 
import cn._1lift.edianti.domain.AbstractAuditingEntity;
import cn._1lift.edianti.domain.Role;
import cn._1lift.edianti.domain.User;
import cn._1lift.edianti.model.enums.InformationSource;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.annotations.Setting;
 
import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
 
/**
 * Created by Damon on 2016/10/26.
 * 咨询信息
 */
@Entity
@Table(name = "information")
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "information")
@Setting(settingPath = "/settings.json")
public class Information extends AbstractAuditingEntity{
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
 
    @Column(name = "title")
    private String title;
 
    @Column(name = "content",nullable=false,columnDefinition="text")
    private String content;
 
    @ManyToOne
    @JoinColumn(name="type_id",insertable = false, updatable = false)
    private InformationType type;
 
    @Column(name = "type_id")
    private Long typeId;
 
    @ManyToMany
    @JoinTable(
            name = "information_tag",
            joinColumns = {@JoinColumn(name = "information_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "tag_id", referencedColumnName = "id")})
//    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @Field(type = FieldType.Auto,store = true)
    private Set<InformationTag> tags = new HashSet<>();
 
    public void setRoles(Collection<Role> roles) {
        this.roles = roles;
    }
 
    @ManyToMany
    @JoinTable(
            name = "information_role",
            joinColumns = {@JoinColumn(name = "information_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")})
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @Field(type = FieldType.Nested,store = true)
    private Collection<Role> roles;
 
    //已读用户列表
    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(
            name = "information_Read",
            joinColumns = {@JoinColumn(name = "information_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")})
    @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @Field(type = FieldType.Nested,store = true)
    private Set<User> users = new HashSet<>();
 
    /**
     * 消息发布来源
     */
    @Column(name = "source")
    private InformationSource source;
 
    public Long getId() {
        return id;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public String getContent() {
        return content;
    }
 
    public void setContent(String content) {
        this.content = content;
    }
 
    public InformationType getType() {
        return type;
    }
 
    public void setType(InformationType type) {
        this.type = type;
    }
 
    public Set<InformationTag> getTags() {
        return tags;
    }
 
    public void setTags(Set<InformationTag> tags) {
        this.tags = tags;
    }
 
    public InformationSource getSource() {
        return source;
    }
 
    public void setSource(InformationSource source) {
        this.source = source;
    }
 
    public Long getTypeId() {
        return typeId;
    }
 
    public void setTypeId(Long typeId) {
        this.typeId = typeId;
    }
 
    public Collection<Role> getRoles() {
        return roles;
    }
 
    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
 
    public Set<User> getUsers() {
        return users;
    }
 
    public void setUsers(Set<User> users) {
        this.users = users;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文