-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Coding the Dao for a many-to-many coded as two many-to-one
PostPosted: Thu Dec 16, 2010 4:57 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hello,

I'm trying to fix a Dao method on a many to many relationship coded as two many-to-one relationships.

I have a test case that goes like this:
Code:
   @Test
   public void testFindByMailList() {
      mailList0 = new MailList();
      mailList0.setName("A list");
      mailList0 = mailListDao.makePersistent(mailList0);
      MailListUser mailListUser0 = new MailListUser(mailList0, user0);
      mailListUser0 = mailListUserDao.makePersistent(mailListUser0);
      MailListUser mailListUser1 = new MailListUser(mailList0, user1);
      mailListUser1 = mailListUserDao.makePersistent(mailListUser1);
      List<User> users = userDao.findByMailMailList(mailList0);
      assertEquals(2, users.size());
      assertEquals(user1.getFirstname(), users.get(0).getFirstname());
      assertEquals(user0.getFirstname(), users.get(0).getFirstname());
   }


And so, I first tried to go with this dao method:
Code:
   @SuppressWarnings("unchecked")
   public List<User> findByMailMailList(MailList mailList) {
      // TODO fix this query
      Criteria criteria = getSession().createCriteria(getPersistentClass()).createCriteria("mailListUser", "mlu").createAlias("user", "u", CriteriaSpecification.INNER_JOIN);
      criteria.add(Restrictions.eq("mlu.mailListId", mailList.getId()));
      criteria.addOrder(Order.asc("u.firstname")).addOrder(Order.asc("u.lastname")).addOrder(Order.asc("u.email"));
      return criteria.list();
   }


The mappings are:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   <!-- Generated Apr 18, 2010 1:03:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
   <class name="com.thalasoft.learnintouch.core.domain.User" table="user"
      dynamic-insert="true" dynamic-update="true">
      <id name="id" type="java.lang.Integer">
         <column name="id" />
         <generator class="identity" />
      </id>
      <version name="version" type="int">
         <column name="version" not-null="true" />
      </version>
      <many-to-one name="address"
         class="com.thalasoft.learnintouch.core.domain.Address" cascade="all" fetch="join">
         <column name="address_id" unique="true" />
      </many-to-one>
      <property name="firstname" type="string">
         <column name="firstname" not-null="true" />
      </property>
      <property name="lastname" type="string">
         <column name="lastname" not-null="true" />
      </property>
      <property name="organisation" type="string">
         <column name="organisation" />
      </property>
      <property name="email" type="string">
         <column name="email" not-null="true" unique="true" />
      </property>
      <property name="fax" type="string">
         <column name="fax" length="20" />
      </property>
      <property name="homePhone" type="string">
         <column name="home_phone" length="20" />
      </property>
      <property name="workPhone" type="string">
         <column name="work_phone" length="20" />
      </property>
      <property name="mobilePhone" type="string">
         <column name="mobile_phone" length="20" />
      </property>
      <property name="password" type="string">
         <column name="password" length="100" />
      </property>
      <property name="passwordSalt" type="string">
         <column name="password_salt" length="50" />
      </property>
      <property name="readablePassword" type="string">
         <column name="readable_password" length="50" />
      </property>
      <property name="validUntil"   type="dateTime">
         <column name="valid_until" />
      </property>
      <property name="profile" type="text">
         <column name="profile" length="65535" />
      </property>
      <property name="image" type="string">
         <column name="image" length="50" />
      </property>
      <property name="mailSubscribe" type="boolean">
         <column name="mail_subscribe" />
      </property>
      <property name="smsSubscribe" type="boolean">
         <column name="sms_subscribe" />
      </property>
      <property name="creationDateTime" type="dateTime">
         <column name="creation_datetime" not-null="true" />
      </property>
   </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2010 1:03:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.thalasoft.learnintouch.core.domain.MailList" table="mail_list" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <version name="version" type="int">
            <column name="version" not-null="true" />
        </version>
        <property name="name" type="string">
            <column name="name" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 18, 2010 1:03:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="com.thalasoft.learnintouch.core.domain.MailListUser" table="mail_list_user" dynamic-insert="true" dynamic-update="true">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>
        <version name="version" type="int">
            <column name="version" not-null="true" />
        </version>
        <many-to-one name="user" class="com.thalasoft.learnintouch.core.domain.User" fetch="join">
            <column name="user_id" not-null="true" />
        </many-to-one>
        <many-to-one name="mailList" class="com.thalasoft.learnintouch.core.domain.MailList" fetch="join">
            <column name="mail_list_id" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>


The model class of the relationship is:
Code:
package com.thalasoft.learnintouch.core.domain;

public class MailListUser implements java.io.Serializable {

   private Integer id;
   private int version;
   private User user;
   private MailList mailList;

   public MailListUser() {
   }

   public MailListUser(MailList mailList, User user) {
      this.user = user;
      this.mailList = mailList;
   }

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

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

   public int getVersion() {
      return this.version;
   }

   public void setVersion(int version) {
      this.version = version;
   }

   public User getUser() {
      return this.user;
   }

   public void setUser(User user) {
      this.user = user;
   }

   public MailList getMailList() {
      return this.mailList;
   }

   public void setMailList(MailList mailList) {
      this.mailList = mailList;
   }

}


But I get an exception at build time:
Quote:
testFindByMailList(com.thalasoft.learnintouch.core.dao.UserDaoTest) Time elapsed: 0.021 sec <<< ERROR!
org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: mailListUser of: com.thalasoft.learnintouch.core.domain.User; nested exception is org.hibernate.QueryException: could not resolve property: mailListUser of: com.thalasoft.learnintouch.core.domain.User


And so, I'm wondering how to code that Dao method with the Criteria API.

I guess I could try to do it with a regular HQL, but before going that slope, I'd like to know if it's possible or advised to do it with a Criteria.

Stephane


Top
 Profile  
 
 Post subject: Re: Coding the Dao for a many-to-many coded as two many-to-one
PostPosted: Thu Dec 16, 2010 12:12 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
For the time being, I solved the issue with an HQL:

Code:
      Query query = getSession().createQuery("select u from User u, MailList ml, MailListUser mlu where u.id = mlu.user.id and mlu.mailList.id = ml.id and ml.id = ? order by u.firstname, u.lastname, u.email");
      query.setLong(0, mailList.getId());
      return query.list();


But I still hope to make it work the same with the Criteria API instead of the HQL one.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.