jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  情景为,投票程序.游客可以对一个作品进行投票的,它们之间有点象多对多的关系,投票时,每个单独的IP每天
只能投某个作品一次,要投第2次的话只能等第2天了.数据库结构表为:
ip表
id   自动递增
ip   ip
votetime  投票时间
pid          该投票是投哪一个作品,关联photo表的pid字段

那么分别看vote类的domain设计
  

public class Vote {

 private int id;
   private int voteid;
   private String ip;
   private String votetime;
   private Photo photo;

////省略getter/setter;


public class photo
{
 private Set ip;
//其他省略.

vote.hbm.xml:
  

<hibernate-mapping>
 <class table="ip" name="liao.domain.Vote">
 <id name="id" column="id" type="integer">
   <generator class="increment" />
  </id>
 

  <property name="ip"/>
 
  <property name="votetime"/>
   <many-to-one column="pid" name="Photo" not-null="true" class="liao.domain.Photo"/>
   
 </class>
</hibernate-mapping>

photo.hbm.xml
 其中关联的部分为
<set cascade="none" name="ip" inverse="true" lazy="false">
   <key column="pid" />
   <one-to-many class="liao.domain.Vote" />
  </set>


好了,上面四者都有的话,下面有个场景,很实际,是查询的,要查询某个PID的作品是否已经有IP投过票了,应该这样搞.
Object[] params=new Object[]{pid};
  

 List dataList = this.getHibernateTemplate().find("from Vote as i where i.Photo.pid=?",pid);
// List dataList=this.getHibernateTemplate().find("select ip from Vote ip where ip.pid=?",pid);
         if (dataList.size() == 0) {
    return null;
         }
         else
         {
       
           System.out.println(dataList.get(0).toString());
         return (Vote)dataList.get(0);

  

   要特别注意 List dataList = this.getHibernateTemplate().find("from Vote as i where i.Photo.pid=?",pid);
的写法,这样比较好.
posted on 2008-07-13 23:18  jackyrong的世界  阅读(376)  评论(0编辑  收藏  举报