如何在JPQL中的INNER JOIN FETCH-ed类中搜索DiscriminatorType?

时间:2013-11-22 11:02:45

标签: hibernate entity inner-join jpql single-table-inheritance

我有3个带有SINGLE_TABLE继承的java类,如你所见:

MessageTemplate父实体:

@Entity
@Table(name = "message_template")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="MESSAGE_TYPE", discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("message_template")
public class MessageTemplate {
    private String campaignName;
    private String subject;
    private String text;
    private List<Message> messages = new ArrayList<>();
    //gettersetter...
    @OneToMany(mappedBy = "template")
    public List<Message> getMessages() {
        return messages;
    }
}

EmailTemplate从MessageTemplate扩展:

@Entity
@DiscriminatorValue("EMAIL")
public class EmailTemplate extends MessageTemplate {
    //some properties
}

SmsTemplate从MessageTemplate扩展:

@Entity
@DiscriminatorValue("SMS")
public class SmsTemplate extends MessageTemplate{
    //some properties
}

你可以看到,有@ManyToOne(fetch = FetchType.LAZY)

我想创建一个jpql sql select,我可以搜索参数,还可以搜索类类型!我写了一些东西,但它没有奏效:

String sql = "SELECT msg FROM message INNER JOIN FETCH msg.template WHERE TYPE(msg.template) IN :class";
Query q = em.createQuery(sql.toString());
q.setParameter("class", EmailTemplate.class);

但是我收到了错误:

java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: class of: com.khb.rrm.entity.Message [SELECT msg FROM com.khb.rrm.entity.Message msg WHERE TYPE(msg.template) IN :class]

但我可以搜索其他属性,例如:

String sql = "SELECT msg FROM message INNER JOIN FETCH msg.template WHERE msg.template.subject LIKE :subject";
Query q = em.createQuery(sql.toString());
q.setParameter("subject", "%something subject%");

如何在内部联接提取类型中搜索类类型?谢谢!

0 个答案:

没有答案