Hibernate manytomany对象过滤器

时间:2016-02-23 06:54:57

标签: java hibernate many-to-many

我是Java的新手,我遇到了一个简单的问题。我有一个名为UserProfile的类和另一个名为Company的类。两者都与ManyToMany关系有关。他们的定义如下:

// User Profile
@Entity
@Table(name="SEC_USER_PROFILES") 
@JsonSerialize(using=UserSerializer.class)
public class UserProfile implements Serializable{
    Long userId;
    .
    ...

    private List<Company> companiesList;


    public UserProfile() {
        this.companiesList = new ArrayList<Company>();
    }

    @ManyToMany(fetch=FetchType.EAGER)
    @JoinTable(
            name="SEC_USERS_COMPANIES",
            joinColumns=@JoinColumn(name="USERID"),
            inverseJoinColumns=@JoinColumn(name="COMPANYID")
            )
    //@JsonIgnore
    public List<Company> getCompaniesList() {
        return companiesList;
    }
    public void setCompaniesList(List<Company> companies) {
        this.companiesList = companies;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "USERID")
    public Long getUserId() {
        return userId;
    }
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    // other getters and setters
}

公司课程:

@Entity
@Table(name="SEC_COMPANIES")
public class Company implements Serializable{
    Long companyId;
    String companyName;
    String companyAddress;
    String companyDesc;


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "COMPANYID") 
    public Long getCompanyId() {
        return companyId;
    }
    public void setCompanyId(Long companyId) {
        this.companyId = companyId;
    }
    // other getters setters
}

现在我需要使用companyId = 3和其他条件(如firstName =“John”等)过滤userprofiles。我没有看到使用标准的任何明显的解决方案。您无需担心任何其他底层实现和设置。我只想要一种方法来轻松过滤userprofile。 我怎样才能做到这一点? 谢谢。

0 个答案:

没有答案
相关问题