1
回答
Spring data elasticsearch注解@Filed和@ManytoMany同时使用,搜索结果取到属性为null
华为云实践训练营,热门技术免费实践!>>>   

搜索结果返回值带@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;
    }
}



举报
顶部