Hibernate Projections设置<object> @ManyToMany

时间:2016-08-21 11:49:27

标签: hibernate criteria

我有2个实体类作为帐户和邮件,它们通过@ManyToMany相互连接。 帐户实体有许多邮件实体,而邮件实体有许多帐户实体。我需要从邮件中获取邮件的密件抄送。

我的标准如下;

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Mail.class, "t");
        criteria.setProjection(Projections.projectionList()
                  .add(Projections.property("mailId"), "mailId")
                  .add(Projections.property("title"), "title")
                ).setResultTransformer(Transformers .aliasToBean(Mail.class));
        List<Mail> Mails = (List<Mail>)criteria.list();

但是当我添加 .add(Projections.property(&#34;来自&#34;),&#34;来自&#34;) 此代码时,试图从&#34;,&#34; cc&#34;中获取Set&#34;或者&#34; bcc&#34;,我收到错误,我搜索得太多但我找不到成功的方法。

@Entity
@Table(name = "MAIL")
public class MAIL implements Serializable {

    private static final long serialVersionUID = -1196398352385977136L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "MAIL_ID")
    private Integer mailId;


@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "MAIL_FROM_ACCOUNT", joinColumns = { @JoinColumn(name = "MAIL_ID") }, inverseJoinColumns = {
        @JoinColumn(name = "ACCOUNT_ID") })
private Set<Account> from = new HashSet<Account>(0);

@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "MAIL_TO_ACCOUNT", joinColumns = { @JoinColumn(name = "MAIL_ID") }, inverseJoinColumns = {
        @JoinColumn(name = "ACCOUNT_ID") })
private Set<Account> to = new HashSet<Account>(0);


@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "MAIL_BCC_ACCOUNT", joinColumns = { @JoinColumn(name = "MAIL_ID") }, inverseJoinColumns = {
        @JoinColumn(name = "ACCOUNT_ID") })
private Set<Account> bcc = new HashSet<Account>(0);

和我的其他班级

@Entity
@Table(name = "ACCOUNT")
public class Account implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -6230400682491920993L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ACCOUNT_ID")
    private int accountId;

@ManyToMany(mappedBy = "from", fetch = FetchType.LAZY)
private Set<Mail> fromMails = new HashSet<Mail>(0);

@ManyToMany(mappedBy = "to", fetch = FetchType.LAZY)
private Set<Mail> toMails = new HashSet<Mail>(0);

@ManyToMany(mappedBy = "to", fetch = FetchType.LAZY)
private Set<Mail> bccMails = new HashSet<Mail>(0);

//getter and setters

java.lang.ArrayIndexOutOfBoundsException:2     在org.hibernate.loader.criteria.CriteriaLoader.getResultRow(CriteriaLoader.java:161)     在org.hibernate.loader.criteria.CriteriaLoader.getResultColumnOrRow(CriteriaLoader.java:141)     在org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:756)     在org.hibernate.loader.Loader.processResultSet(Loader.java:972)     在org.hibernate.loader.Loader.doQuery(Loader.java:930)     在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)     在org.hibernate.loader.Loader.doList(Loader.java:2617)     在org.hibernate.loader.Loader.doList(Loader.java:2600)     在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429)     在org.hibernate.loader.Loader.list(Loader.java:2424)     在org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)     在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1774)     在org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)

0 个答案:

没有答案