hibernate mysql视图操作

hibernate对视图操作,首先建立数据库视图

视图v_invite:

create 
view pintu.v_invite as
select 
cp.user_id as be_user_id,
cai.activity_invite_id,
cai.person_id,
cai.person_name,
cai.be_person_id,
cai.activity_id,
cai.invite_time
from 
ptcom.com_person cp, 
ptcom.com_activity_invite cai
where 
cp.person_id = cai.be_person_id

生成hibernate视图mapping映射

<hibernate-mapping>
    <class name="com.test.pojo.entity.VComActivityInvite" table="v_com_activity_invite" catalog="pintu">
        <composite-id name="id" class="com.test.pojo.entity.VComActivityInviteId">
            <key-property name="beUserId" type="java.lang.String">
                <column name="be_user_id" length="32" />
            </key-property>
            <key-property name="activityInviteId" type="java.lang.Long">
                <column name="activity_invite_id" />
            </key-property>
            <key-property name="personId" type="java.lang.Long">
                <column name="person_id" />
            </key-property>
            <key-property name="personName" type="java.lang.String">
                <column name="person_name" length="45" />
            </key-property>
            <key-property name="bePersonId" type="java.lang.Long">
                <column name="be_person_id" />
            </key-property>
            <key-property name="activityId" type="java.lang.Long">
                <column name="activity_id" />
            </key-property>
            <key-property name="inviteTime" type="java.util.Date">
                <column name="invite_time" length="19" />
            </key-property>
        </composite-id>
    </class>
</hibernate-mapping>

对性实体POJO:VComActivityInvite

public class VComActivityInvite implements java.io.Serializable {

// Fields

private VComActivityInviteId id;

// Constructors


public VComActivityInvite() {
}


public VComActivityInvite(VComActivityInviteId id) {
   this.id = id;
}

// Property accessors

public VComActivityInviteId getId() {
   return this.id;
}

public void setId(VComActivityInviteId id) {
   this.id = id;
}

}

对应视图数据体:VComActivityInviteId

public class VComActivityInviteId implements java.io.Serializable {

// Fields

private String beUserId;
private Long activityInviteId;
private Long personId;
private String personName;
private Long bePersonId;
private Long activityId;
private Date inviteTime;

// Constructors


public VComActivityInviteId() {
}


public VComActivityInviteId(Long activityInviteId) {
   this.activityInviteId = activityInviteId;
}


public VComActivityInviteId(String beUserId, Long activityInviteId,
    Long personId, String personName, Long bePersonId, Long activityId,
    Date inviteTime) {
   this.beUserId = beUserId;
   this.activityInviteId = activityInviteId;
   this.personId = personId;
   this.personName = personName;
   this.bePersonId = bePersonId;
   this.activityId = activityId;
   this.inviteTime = inviteTime;
}

// Property accessors

public String getBeUserId() {
   return this.beUserId;
}

public void setBeUserId(String beUserId) {
   this.beUserId = beUserId;
}

public Long getActivityInviteId() {
   return this.activityInviteId;
}

public void setActivityInviteId(Long activityInviteId) {
   this.activityInviteId = activityInviteId;
}

public Long getPersonId() {
   return this.personId;
}

public void setPersonId(Long personId) {
   this.personId = personId;
}

public String getPersonName() {
   return this.personName;
}

public void setPersonName(String personName) {
   this.personName = personName;
}

public Long getBePersonId() {
   return this.bePersonId;
}

public void setBePersonId(Long bePersonId) {
   this.bePersonId = bePersonId;
}

public Long getActivityId() {
   return this.activityId;
}

public void setActivityId(Long activityId) {
   this.activityId = activityId;
}

public Date getInviteTime() {
   return this.inviteTime;
}

public void setInviteTime(Date inviteTime) {
   this.inviteTime = inviteTime;
}

public boolean equals(Object other) {
   if ((this == other))
    return true;
   if ((other == null))
    return false;
   if (!(other instanceof VComActivityInviteId))
    return false;
   VComActivityInviteId castOther = (VComActivityInviteId) other;

   return ((this.getBeUserId() == castOther.getBeUserId()) || (this
     .getBeUserId() != null
     && castOther.getBeUserId() != null && this.getBeUserId()
     .equals(castOther.getBeUserId())))
     && ((this.getActivityInviteId() == castOther
       .getActivityInviteId()) || (this.getActivityInviteId() != null
       && castOther.getActivityInviteId() != null && this
       .getActivityInviteId().equals(
         castOther.getActivityInviteId())))
     && ((this.getPersonId() == castOther.getPersonId()) || (this
       .getPersonId() != null
       && castOther.getPersonId() != null && this
       .getPersonId().equals(castOther.getPersonId())))
     && ((this.getPersonName() == castOther.getPersonName()) || (this
       .getPersonName() != null
       && castOther.getPersonName() != null && this
       .getPersonName().equals(castOther.getPersonName())))
     && ((this.getBePersonId() == castOther.getBePersonId()) || (this
       .getBePersonId() != null
       && castOther.getBePersonId() != null && this
       .getBePersonId().equals(castOther.getBePersonId())))
     && ((this.getActivityId() == castOther.getActivityId()) || (this
       .getActivityId() != null
       && castOther.getActivityId() != null && this
       .getActivityId().equals(castOther.getActivityId())))
     && ((this.getInviteTime() == castOther.getInviteTime()) || (this
       .getInviteTime() != null
       && castOther.getInviteTime() != null && this
       .getInviteTime().equals(castOther.getInviteTime())));
}

public int hashCode() {
   int result = 17;

   result = 37 * result
     + (getBeUserId() == null ? 0 : this.getBeUserId().hashCode());
   result = 37
     * result
     + (getActivityInviteId() == null ? 0 : this
       .getActivityInviteId().hashCode());
   result = 37 * result
     + (getPersonId() == null ? 0 : this.getPersonId().hashCode());
   result = 37
     * result
     + (getPersonName() == null ? 0 : this.getPersonName()
       .hashCode());
   result = 37
     * result
     + (getBePersonId() == null ? 0 : this.getBePersonId()
       .hashCode());
   result = 37
     * result
     + (getActivityId() == null ? 0 : this.getActivityId()
       .hashCode());
   result = 37
     * result
     + (getInviteTime() == null ? 0 : this.getInviteTime()
       .hashCode());
   return result;
}

}

在操作中,对视图进行查询,引入查询条件:

String hql = "select count(*) from VComActivityInvite vca where vca.id.beUserId = ‘" + userId+"‘";
   return ((Number)vComActivityInviteDao.createQuery(hql).uniqueResult()).intValue();

父类 id 对应 <composite-id name="id" class="com.test.pojo.entity.VComActivityInviteId">

hibernate3.2对应统计数量返回LONG型,需转化如下

((Number)vComActivityInviteDao.createQuery(hql).uniqueResult()).intValue

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。