如何在hibernate join中排除列

时间:2015-10-06 11:12:58

标签: java sql hibernate hql

我有以下Hibernate实体类,它工作正常但是ProfileBean的关联我想排除几个字段,如密码等,有没有办法进行条件连接并只选择几列?

@Root(name = "message")
@XmlRootElement
@Entity
@Table(name="app_messages")
public class MessageBean implements Serializable, DataModel {

    @Id
    @Column(name = "id")
    @GeneratedValue
    @Element
    private Long id;
    @Column(name = "from_id")
    @Element
    private Long fromUserId;
    @Column(name = "to_id")
    @Element
    private Long toUserId;
    @Column(name = "message")
    @Element
    private String message;
    @Column(name = "sent_date")
    @Element
    private Date sentDate;
    @Column(name = "read_date")
    @Element
    private Date readDate;
    @Column(name = "thread_id")
    @Element
    private Long threadId;

    @OneToOne(cascade=CascadeType.ALL)  
    @JoinTable(name="user_profiles",  
    joinColumns={@JoinColumn(name="user_id", referencedColumnName="to_id")})  
    private ProfileBean toUser;

    @OneToOne(cascade=CascadeType.ALL)  
    @JoinTable(name="user_profiles",  
    joinColumns={@JoinColumn(name="user_id", referencedColumnName="from_id")})  
    private ProfileBean fromUser;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "thread_id", nullable = false)
    private MessageThreadBean messageThreadBean;

    /**
     * @return the id public long getId() { return id; }
     *
     * /
     **
     * @param id the id to set
     */
    @XmlElement
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the fromUserId
     */
    public Long getFromUserId() {
        return fromUserId;
    }

    /**
     * @param fromUserId the fromUserId to set
     */
    @XmlElement
    public void setFromUserId(Long fromUserId) {
        this.fromUserId = fromUserId;
    }

    /**
     * @return the toUserId
     */
    public Long getToUserId() {
        return toUserId;
    }

    /**
     * @param toUserId the toUserId to set
     */
    @XmlElement
    public void setToUserId(Long toUserId) {
        this.toUserId = toUserId;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    @XmlElement
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the sentDate
     */
    public Date getSentDate() {
        return sentDate;
    }

    /**
     * @param sentDate the sentDate to set
     */
    @XmlElement
    public void setSentDate(Date sentDate) {
        this.sentDate = sentDate;
    }

    /**
     * @return the readDate
     */
    public Date getReadDate() {
        return readDate;
    }

    /**
     * @param readDate the readDate to set
     */
    @XmlElement
    public void setReadDate(Date readDate) {
        this.readDate = readDate;
    }

    /**
     * @return the threadId
     */
    public Long getThreadId() {
        return threadId;
    }

    /**
     * @param threadId the threadId to set
     */
    @XmlElement
    public void setThreadId(Long threadId) {
        this.threadId = threadId;
    }

    /**
     * @return the fromUser
     */
    public ProfileBean getFromUser() {
        return fromUser;
    }

    /**
     * @param fromUser the fromUser to set
     */
    public void setFromUser(ProfileBean fromUser) {
        this.fromUser = fromUser;
    }

    /**
     * @return the toUser
     */
    public ProfileBean getToUser() {
        return toUser;
    }

    /**
     * @param toUser the toUser to set
     */
    public void setToUser(ProfileBean toUser) {
        this.toUser = toUser;
    }

    /**
     * @return the messageThreadBean
     */
    public MessageThreadBean getMessageThreadBean() {
        return messageThreadBean;
    }

    /**
     * @param messageThreadBean the messageThreadBean to set
     */
    public void setMessageThreadBean(MessageThreadBean messageThreadBean) {
        this.messageThreadBean = messageThreadBean;
    }
}

toUserId和fromUserId与ProfileBean之间存在OneToOne关联。 fromUserId是发送消息的用户,toUserId是将要接收消息的用户。如果我们能得到一些hql解决方案,我们可以在某个地方传递注释会很棒。

2 个答案:

答案 0 :(得分:0)

如果排除某些字段,则无法接收实体MessageBean。但是在HQL中,您只能收集所需的字段。尝试编写这样的hql查询:

select m.message, m.readDate from MessageBean where sentDate between :d1 and :d2

答案 1 :(得分:0)

另一种选择是使用@PostLoad filters

所以这将是这样的:

@Root(name = "message")
@XmlRootElement
@Entity
@Table(name="app_messages")
public class MessageBean implements Serializable, DataModel {
....

@PostLoad
public void deleteSensitiveData() {
   this.password = null;
}
}